Centos7 安装 Nginx 的正确姿势 并设置开机自启 实践笔记

本文介绍在Centos7上安装Nginx的过程,并设置防火墙放行80端口,实现Nginx的开机自启动。文章详细记录了从安装依赖到编译安装Nginx的所有步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


title: Centos7 安装 Nginx 的正确姿势 并设置开机自启 实践笔记
categories: [Nginx,Centos 7]
tags: [Nginx,Centos 7]

我使用centos7X64最小化安装
CentOS-7-x86_64-Minimal-1708

挂在github上的个人博客:由hexo强力驱动 个人博客

1.配置防火墙:

关闭防火墙和加入放行端口二选一

1.1 直接关闭防火墙

systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)
[root@localhost ~]# firewall-cmd --state
not running
[root@localhost ~]#

1.2 加入放行端口

firewall-cmd --zone=public --add-port=80/tcp --permanent #添加放行端口(--permanent永久生效,没有此参数重启后失效)
firewall-cmd --reload #刷新防火墙 使其生效
firewall-cmd --zone=public --list-ports #查看防火墙放行端口列表
[root@localhost ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent #添加放行端口(--permanent永久生效,没有此参数重启后失效)
success
[root@localhost ~]# firewall-cmd --reload #刷新防火墙 使其生效
success
[root@localhost ~]# firewall-cmd --zone=public --list-ports #查看防火墙放行端口列表
80/tcp
[root@localhost ~]#

2.安装wget

yum -y install wget

3.安装nginx 编译环境

yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

4.安装nginx

官方查看最新稳定版本 nginx1.12.2官网链接直达
我现在最新稳定版本是:nginx-1.12.2

wget -c https://nginx.org/download/nginx-1.12.2.tar.gz

5.解压

tar -zxvf nginx-1.12.2.tar.gz
cd nginx-1.12.2

6.配置nginx

./configure
 > 输出记录方便查看 不要执行 不要执行 不要执行
  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"

7.编译安装

make

输出记录方便查看 不要执行 不要执行 不要执行
sed -e “s|%%PREFIX%%|/usr/local/nginx|”
-e “s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|”
-e “s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|”
-e “s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|”
< man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/root/nginx-1.12.2’

make install

输出记录方便查看 不要执行 不要执行 不要执行
make[1]: Entering directory /root/nginx-1.12.2' test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx' test -d '/usr/local/nginx/sbin' \ || mkdir -p '/usr/local/nginx/sbin' test ! -f '/usr/local/nginx/sbin/nginx' \ || mv '/usr/local/nginx/sbin/nginx' \ '/usr/local/nginx/sbin/nginx.old' cp objs/nginx '/usr/local/nginx/sbin/nginx' test -d '/usr/local/nginx/conf' \ || mkdir -p '/usr/local/nginx/conf' cp conf/koi-win '/usr/local/nginx/conf' cp conf/koi-utf '/usr/local/nginx/conf' cp conf/win-utf '/usr/local/nginx/conf' test -f '/usr/local/nginx/conf/mime.types' \ || cp conf/mime.types '/usr/local/nginx/conf' cp conf/mime.types '/usr/local/nginx/conf/mime.types.default' test -f '/usr/local/nginx/conf/fastcgi_params' \ || cp conf/fastcgi_params '/usr/local/nginx/conf' cp conf/fastcgi_params \ '/usr/local/nginx/conf/fastcgi_params.default' test -f '/usr/local/nginx/conf/fastcgi.conf' \ || cp conf/fastcgi.conf '/usr/local/nginx/conf' cp conf/fastcgi.conf '/usr/local/nginx/conf/fastcgi.conf.default' test -f '/usr/local/nginx/conf/uwsgi_params' \ || cp conf/uwsgi_params '/usr/local/nginx/conf' cp conf/uwsgi_params \ '/usr/local/nginx/conf/uwsgi_params.default' test -f '/usr/local/nginx/conf/scgi_params' \ || cp conf/scgi_params '/usr/local/nginx/conf' cp conf/scgi_params \ '/usr/local/nginx/conf/scgi_params.default' test -f '/usr/local/nginx/conf/nginx.conf' \ || cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf' cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default' test -d '/usr/local/nginx/logs' \ || mkdir -p '/usr/local/nginx/logs' test -d '/usr/local/nginx/logs' \ || mkdir -p '/usr/local/nginx/logs' test -d '/usr/local/nginx/html' \ || cp -R html '/usr/local/nginx' test -d '/usr/local/nginx/logs' \ || mkdir -p '/usr/local/nginx/logs' make[1]: Leaving directory/root/nginx-1.12.2’

以上操作后Nginx将被安装在/usr/local/nginx目录下

8.启动、停止、重启nginx

cd /usr/local/nginx/sbin/
./nginx -c /usr/local/nginx/conf/nginx.conf
./nginx  #默认读/usr/local/nginx/conf/nginx.conf配置文件 

./nginx -s reload #重新加载配置文件
./nginx -s quit #:推荐 优雅停止 待nginx进程处理任务完毕进行停止
./nginx -s stop #:先查出nginx进程id再使用kill命令强制杀掉进程。

9.查询nginx进程:

ps aux|grep nginx

10.开机自启动

10.1方式一

在rc.local增加启动代码就可以了。

vi /etc/rc.local

底部增加一行:

/usr/local/nginx/sbin/nginx

设置执行权限:

chmod 755 /etc/rc.local

10.2方式二

将Nginx配置服务注册为系统服务,可以通过systemctl管理

CentOS7的服务systemctl脚本可以存放在:/usr/lib/systemd/和/lib/systemd/system,有系统(system)和用户(user)之分,需要开机不登陆就能运行的程序,存在系统服务里,即:/usr/lib/systemd/system目录下.
CentOS7的每一个服务以.service结尾,一般会分为3部分:[Unit]、[Service]和[Install],对Nginx服务,通过vi命令打开服务配置:

vi /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
# 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文件中的保持一致;

开机自启

systemctl enable nginx
systemctl start nginx

查看nginx状态

ps -ef |grep nginx
systemctl status nginx
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值