一、在centos环境下安装下载并安装nginx,由于nginx需要依赖一些环境才能安装,主要依赖g++、gcc、openssl-devel、pcre-devel和zlib-devel这些环境,首先得先安装这些
yum install gcc-c++ yum install pcre pcre-devel yum install zlib zlib-devel yum install openssl openssl--devel这些依赖环境安装好了之后就需要检测系统是否已经安装了nginx应用find / -name "nginx*" 查找根目录下以nginx开头的文件或者文件夹所有内容,如果没有之后可以进行下载安装nginxwget http://nginx.org/download/nginx-1.12.2.tar.gz 下载之后解压到当前文件tar -zxvf nginx-1.12.2.tar.gz 进入文件夹执行编译安装nginxmake PREFIX=/usr/local/nginx install安装执行之后可以查找nginx安装位置whereis nginx
二、成功安装nginx之后可以进入安装目录下执行并启动nginx
进入安装nginx目录 cd /usr/local/sbin/ 启动nginx服务 ./nginx 查看nginx应用启动详情 ps aux | grep nginx三、配置nginx开机自启动
进入到/etc/init.d/目录下,创建文件nginx cd /etc/init.d/ vim nginxnginx文件具体配置信息如下:#!/bin/bash# nginx Startup script for the Nginx HTTP Server# it is v.0.0.2 version.# chkconfig: - 85 15# description: Nginx is a high-performance web and proxy server.# It has a lot of features, but it's not for everyone.# processname: nginx# pidfile: /var/run/nginx.pid# config: /usr/local/nginx/conf/nginx.confnginxd=/usr/local/nginx/sbin/nginx # 你的nginx真实启动文件路径nginx_config=/usr/local/nginx/conf/nginx.conf # nginx相关配置文件路径nginx_pid=/var/run/nginx.pid RETVAL=0prog="nginx"# Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ ${NETWORKING} = "no" ] && exit 0[ -x $nginxd ] || exit 0# Start nginx daemons functions.start() { if [ -e $nginx_pid ];then echo "nginx already running...." exit 1 fi echo -n $"Starting $prog: " daemon $nginxd -c ${nginx_config} RETVAL=$? echo [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx return $RETVAL}# Stop nginx daemons functions.stop() { echo -n $"Stopping $prog: " killproc $nginxd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid}# reload nginx service functions.reload() { echo -n $"Reloading $prog: " #kill -HUP `cat ${nginx_pid}` killproc $nginxd -HUP RETVAL=$? echo}# See how we were called. case "$1" instart) start ;;stop) stop ;;reload) reload ;;restart) stop start ;;status) status $prog RETVAL=$? ;;*) echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1esacexit $RETVAL保存nginx文件并赋权chmod 755 nginx 将nginx权限设置为自己可以read、write、exec,其他用户只能有read、exec权限,没有write权限为nginx加上service相关命令权限chkconfig --add nginxchkconfig nginx on开启nginx的service命令
四、校验nginx的service命令是否成功
service nginx start 执行不报错表示nginx已经启动重启centos服务器再次验证是否nginx已经启动重启之前service nginx stop停止nginx服务,之后执行reboot,开机之后执行ps aux | grep nginx如果后台显示nginx已经启动,那么表示nginx的安装和开机自启动已经成功配置本文章旨在记录自己的平时学习过程,有不正确的和值得改进的真诚希望园友提出意见或建议,一起学习一起进步。