设为首页 收藏本站
查看: 880|回复: 0

[经验分享] CentOS7安装及配置Nginx服务

[复制链接]

尚未签到

发表于 2018-11-16 09:15:57 | 显示全部楼层 |阅读模式
  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[1]: Starting The nginx HTTP and reverse proxy server...
  
Jul 04 22:13:33 centos.vm0 nginx[914]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  
Jul 04 22:13:33 centos.vm0 nginx[914]: nginx: configuration file /etc/nginx/nginx.conf test is successful
  
Jul 04 22:13:33 centos.vm0 systemd[1]: 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部分:[Unit]、[Service]和[Install],对Nginx服务,通过vi命令打开服务配置:
vi /usr/lib/systemd/system/nginx.service  配置文件nginx.service的内容示例如下:
[Unit]  
Description=The nginx HTTP and reverse proxy server
  
After=network.target remote-fs.target nss-lookup.target
  

  
[Service]
  
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
  

  
[Install]
  
WantedBy=multi-user.target
  建议在修改之前,如果旧的文件已经存在,可以先做好备份,例如:
cp /usr/lib/systemd/system/nginx.service /usr/local/nginx/nginx.service.bak  之后将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
  
# 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
  

  
[Install]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目录下的相关权限:
[root@centos hnl]# cd /usr/local/nginx  
[root@centos nginx]# ls
  
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
  
[root@centos nginx]# ll
  
total 4
  
drwx------. 2 nobody root    6 Jul  4 22:43 client_body_temp
  
drwxr-xr-x. 2 root   root 4096 Jul  5 00:45 conf
  
drwx------. 2 nobody root    6 Jul  4 22:43 fastcgi_temp
  
drwxr-xr-x. 2 root   root   40 Jul  4 22:43 html
  
drwxr-xr-x. 2 root   root   58 Jul  5 01:11 logs
  
drwx------. 2 nobody root    6 Jul  4 22:43 proxy_temp
  
dr-xr-xr-x. 2 root   root   19 Jul  4 22:43 sbin
  
drwx------. 2 nobody root    6 Jul  4 22:43 scgi_temp
  
drwx------. 2 nobody root    6 Jul  4 22:43 uwsgi_temp
  
[root@centos nginx]# ll sbin
  
total 3660
  
-rwxr-xr-x. 1 root root 3746616 Jul  4 22:43 nginx
  为了配置服务自动启动,执行
systemctl enable nginx  然后重启CentOS,重启完成后,参考一中的命令检查Nginx的运行情况:
ps -ef |grep nginx  
systemctl status nginx



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-635613-1-1.html 上篇帖子: 整合tomcat和nginx 下篇帖子: keepalived实现nginx调度器高可用(二)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表