枫叶飞翔 发表于 2018-11-16 09:15:57

CentOS7安装及配置Nginx服务

  Nginx是CentOS中最常用的HTTP服务程序之一,以下针对CentOS7下Nginx二进制包安装、编译安装两个场景分别予以说明。
  一、二进制包安装方式
  
  在安装CentOS7时选择同时安装Nginx或者后来通过yum命令安装Nginx应用,得到的Nginx的安装路径、默认配置都是一样的,并且已经配置好Nginx服务,可通过如命令查看Nginx相关进程:
ps -ef | grep nginx  并可通过systemctl xx命令管理Nginx服务(xx命令可以是:enable-开机自动启动,disable-开机不自动启动,start-启动,status-查看状态,stop-停止),例如启动Nginx服务:
systemctl start nginx  查看版本,执行:
nginx -V  查看Nginx的服务信息,包括二进制、配置文件路径,执行如下命令:
systemctl status nginx  结果显示:
● nginx.service - The nginx HTTP and reverse proxy server  
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
  
Active: active (running) since Wed 2018-07-04 22:13:33 CST; 50min ago
  
Process: 964 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  
Process: 914 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  
Process: 909 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
  
Main PID: 966 (nginx)   CGroup: /system.slice/nginx.service
  
├─966 nginx: master process /usr/sbin/nginx
  
├─967 nginx: worker process
  
└─968 nginx: worker process
  
Jul 04 22:13:33 centos.vm0 systemd: Starting The nginx HTTP and reverse proxy server...
  
Jul 04 22:13:33 centos.vm0 nginx: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  
Jul 04 22:13:33 centos.vm0 nginx: nginx: configuration file /etc/nginx/nginx.conf test is successful
  
Jul 04 22:13:33 centos.vm0 systemd: Started The nginx HTTP and reverse proxy server.
  只查看配置文件路径,可通过命令:
nginx -t  结果显示:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok  
nginx: configuration file /etc/nginx/nginx.conf test is successful
  
  二、编译安装方式
  1、准备源码包到服务器目录
  可从Nginx的网站(http://nginx.org/en/download.html) 下载与当前CentOS7配套的源码包(例如nginx-1.14.0.tar.gz),然后上传到CentOS7服务器,并解压,例如:
tar -zxvf nginx-1.14.0.tar.gz  2、检查gcc
  执行如下命令:
gcc --version  如果报错,说明未安装gcc,需先通过如下命令安装gcc
yum install gcc  3、检查PCRE
  进入到解压后的源码目录:
cd nginx-1.14.0/  执行configure命令:
./configure  如果configure失败,如果是缺少目录,需根据目录名并补充创建缺少的目录,如果是缺少PCRE库,通过如下命令安装PCRE依赖库:
yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel  4、正常编译及安装Nginx
  在解压后的源码目录,为了确保执行正常,请以root身份依次执行如下命令:
./configure  
make
  
make install
  以上操作后Nginx将被安装在/usr/local/nginx目录下
  5、普通方式启动Nginx程序
  进入/usr/local/nginx/sbin目录下:
cd /usr/local/nginx/sbin  执行:
./nginx -c /usr/local/nginx/conf/nginx.conf  注:上述可选参数-c后面指定了/usr/local/nginx/conf/nginx.conf为配置文件
  6、普通方式停止Nginx程序
  进入/usr/local/nginx/sbin目录后,如立即停止Nginx并退出,可执行:
./nginx -s stop  如优雅停止Nginx并退出,可执行
./nginx -s quit  7、重新加载nginx.conf配置文件
  进入/usr/local/nginx/sbin目录后,执行:
./nginx -s reload  8、将Nginx配置服务可以通过systemctl操作的服务
  CentOS7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分,需要开机不登陆就能运行的程序,存在系统服务里,即:/usr/lib/systemd/system目录下.
  CentOS7的每一个服务以.service结尾,一般会分为3部分:、和,对Nginx服务,通过vi命令打开服务配置:
vi /usr/lib/systemd/system/nginx.service  配置文件nginx.service的内容示例如下:
  
Description=The nginx HTTP and reverse proxy server
  
After=network.target remote-fs.target nss-lookup.target
  

  

  
Type=forking
  
PIDFile=/run/nginx.pid
  
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
  
# SELinux context. This might happen when running `nginx -t` from the cmdline.
  
#
  
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
  
ExecStartPre=/usr/sbin/nginx -t
  
ExecStart=/usr/sbin/nginxExecReload=/bin/kill -s HUP $MAINPID
  
KillSignal=SIGQUIT
  
TimeoutStopSec=5
  
KillMode=process
  
PrivateTmp=true
  

  

  
WantedBy=multi-user.target
  建议在修改之前,如果旧的文件已经存在,可以先做好备份,例如:
cp /usr/lib/systemd/system/nginx.service /usr/local/nginx/nginx.service.bak  之后将nginx.service内容中的路径修改为正确的路径即可,例如:
  
Description=The nginx HTTP and reverse proxy server
  
After=network.target remote-fs.target nss-lookup.target
  

  

  
Type=forking
  
PIDFile=/usr/local/nginx/logs/nginx.pid
  
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
  
# SELinux context. This might happen when running `nginx -t` from the cmdline.
  
#
  
ExecStartPre=/usr/bin/rm -f /usr/local/nginx/logs/nginx.pid
  
ExecStartPre=/usr/local/nginx/sbin/nginx -t
  
ExecStart=/usr/local/nginx/sbin/nginx
  
ExecReload=/bin/kill -s HUP $MAINPID
  
KillSignal=SIGQUITTimeoutStopSec=5
  
KillMode=process
  
PrivateTmp=true
  

  
WantedBy=multi-user.target
  注:pid文件的路径,在编译安装的情况下,默认位置一般为/usr/local/nginx/logs/nginx.pid(即nginx.conf中不设置pid路径时的情况),在二进制包安装方式下,默认位置一般为/run/nginx.pid(相应的nginx.conf中有一行pid /run/nginx.pid;),这个位置在/usr/lib/systemd/system/nginx.service中配置需与nginx.conf文件中的保持一致;
  之后需要检查Nginx目录下的相关权限:
# cd /usr/local/nginx  
# ls
  
client_body_tempconffastcgi_temphtmllogsproxy_tempsbinscgi_tempuwsgi_temp
  
# ll
  
total 4
  
drwx------. 2 nobody root    6 Jul4 22:43 client_body_temp
  
drwxr-xr-x. 2 root   root 4096 Jul5 00:45 conf
  
drwx------. 2 nobody root    6 Jul4 22:43 fastcgi_temp
  
drwxr-xr-x. 2 root   root   40 Jul4 22:43 html
  
drwxr-xr-x. 2 root   root   58 Jul5 01:11 logs
  
drwx------. 2 nobody root    6 Jul4 22:43 proxy_temp
  
dr-xr-xr-x. 2 root   root   19 Jul4 22:43 sbin
  
drwx------. 2 nobody root    6 Jul4 22:43 scgi_temp
  
drwx------. 2 nobody root    6 Jul4 22:43 uwsgi_temp
  
# ll sbin
  
total 3660
  
-rwxr-xr-x. 1 root root 3746616 Jul4 22:43 nginx
  为了配置服务自动启动,执行
systemctl enable nginx  然后重启CentOS,重启完成后,参考一中的命令检查Nginx的运行情况:
ps -ef |grep nginx  
systemctl status nginx


页: [1]
查看完整版本: CentOS7安装及配置Nginx服务