Centos7编译安装nginx并添加到系统启动服务

实验环境

centos 7.6
nginx-1.14.2

一、下载安装包

[root@nginx ~]# yum install -y wget
[root@nginx ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz

二、编译安装nginx

1、安装依赖

[root@nginx ~]# yum -y install gcc gcc-c++ zlib zlib-devel openssl openssl-devel pcre-devel

gcc:                   gcc-c++编译环境
gzip模块需要:    zlib 库
rewrite模块需要:pcre 库
ssl功能需要:      openssl库

2、创建nginx用户和组

[root@nginx ~]# groupadd nginx
[root@nginx ~]# useradd nginx -g nginx -s /sbin/nologin -M

3、配置选项

[root@nginx ~]# tar -zxvf nginx-1.14.2.tar.gz
[root@nginx nginx-1.14.2]#  ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module
--user=nginx(用户名)
--group=nginx(组)
--prefix=/usr/local/nginx(程序安装路径) 
--with-http_stub_status_module (网页状态查看)
--with-http_ssl_module (ssl模块)
--with-http_realip_module (后台Nginx服务器记录原始客户端的IP地址 )
--with-http_gzip_static_module (压缩模块)

如果不知道怎么看是否配置成功,可以使用echo $? (显示最后命令的退出状态,0表示没有错误,其他表示有错误)

[root@nginx nginx-1.14.2]# echo $?
0

4、编译安装

[root@nginx nginx-1.14.2]# make && make install
[root@nginx nginx-1.14.2]# echo $?
0

5、添加到环境变量,程序安装路径/usr/local/nginx

[root@nginx ~]# vim /etc/profile.d/nginx.sh 
export PATH=$PATH:/usr/local/nginx/sbin

6、使用说明(选看)

#查看选项说明
[root@nginx ~]# /usr/local/nginx/sbin/nginx -h
nginx version: nginx/1.14.2
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/nginx/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

#查看nginx版本号
[root@nginx ~]# /usr/local/nginx/sbin/nginx -v(小写v)
nginx version: nginx/1.14.2

#查看编译模块
[root@nginx ~]# /usr/local/nginx/sbin/nginx -V(大写V)
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module

#检查配置文件语法
[root@nginx ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

#下面系统服务启动脚本会用到的(重点)
[root@nginx ~]# /usr/local/nginx/sbin/nginx    启动nginx
[root@nginx ~]# /usr/local/nginx/sbin/nginx -s reload    #重载配置
[root@nginx ~]# /usr/local/nginx/sbin/nginx -s stop    #停止
[root@nginx ~]# /usr/local/nginx/sbin/nginx -s quit    #关闭


三、系统服务启动脚本

1、如果不知道怎么写,可以找一台直接使用yum安装nginx的服务器,参考别人的写法。例如下面查看的到脚本路径

2、拷贝到自己的nginx服务器,根据自己的安装路径修改参数

[root@nginx ~]# vim /usr/lib/systemd/system/nginx.service

二选一

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
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=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
[Unit]
Description=nginx
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
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
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

PIDFile:PID路径,如果是按照上面编译时候设置的路径,那就在/usr/local/nginx/logs/nginx.pid

ExecStartPre:在执行ExecStart之前的操作,先删除有关nginx的PID也就是停止nginx,然后再检查nginx -t配置文件是否正确

ExecStart:nginx启动操作,如果是按照上面编译时候设置的路径,那就在/usr/local/nginx/sbin/nginx

ExecReload:nginx重启操作,这里HUP是平滑重启,重新加载配置文件

ExecStop:nginx停止操作,实际是使用了QUIT从容关闭nginx

3、使用说明

[root@nginx ~]# systemctl start|stop|reload|restart|status nginx.service
[root@nginx ~]# systemctl enable nginx    #开机启动
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@nginx ~]# systemctl disable  nginx    #关闭开机启动
Removed symlink /etc/systemd/system/multi-user.target.wants/nginx.service.

四、整合连接php

1、php的php-fpm已经启动(前提是php已经安装好fastcgi,查看php安装篇)

[root@localhost ~]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      7448/php-fpm: maste 

2、修改配置文件,去掉注释

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
        location ~ \.php$ {
            root           /var/www/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;
            include        fastcgi_params;
        }


说明:
 root           /var/www/html;    #自定义php页面路径
 fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name; #自定义页面路径+ $fastcgi_script_name

2、查看结果

[root@nginx ~]# vim /var/www/html/index.php 
this is nginx_php
<?php
phpinfo();
?>

发布了132 篇原创文章 · 获赞 118 · 访问量 25万+

猜你喜欢

转载自blog.csdn.net/tladagio/article/details/102551173
今日推荐