Nginx 编译安装和守护进程
在CentOS 7中下载、编译和安装Nginx 1.20.1源代码需要以下步骤:
安装编译环境及相关依赖
sudo yum install -y gcc make pcre-devel zlib-devel openssl-devel
下载Nginx 1.20.1版本源代码
进入Nginx官网https://nginx.org/en/download.html 将Nginx 1.20.1的源代码链接复制到终端中进行下载,如下所示:
wget https://nginx.org/download/nginx-1.20.1.tar.gz
解压并进入nginx1.20.1源码目录
tar -zxvf nginx-1.20.1.tar.gz
cd nginx-1.20.1
配置并编译Nginx源代码
sudo ./configure --prefix=/usr/local/nginx --with-http_ssl_module
sudo make
sudo make install
启动nginx :sudo nginx
====================================================
可能会遇到报错:
[root@localhost sbin]# nginx
-bash: nginx: command not found
#该命令将会将 /usr/local/nginx/sbin
添加到 $PATH
环境变量中,以便在任何位置都可以访问 nginx
可执行文件,但只在当前会话:
export PATH=$PATH:/usr/local/nginx/sbin
#永久会话中生效:
vi /etc/profile
添加以下内容:
PATH=$PATH:/usr/local/nginx/sbin
export PATH
PS:~/.bashrc
文件只对当前用户有效,而 /etc/profile
文件对所有用户都有效。
====================================================
PS:systemctl 启动的程序不要用kill -9 杀进场。
PS:vim subscription-manager.conf :enabled=0 解决centos7 注册问题:
This system is not registered with an entitlement server. You can use subscription-manager to register
###################################################################################
PS:遇到一个问题,nginx页面可以访问,但是在systemctl status nginx 里却没有nginx.server
手动创建nginx系统服务:
1 vim /etc/systemd/system/nginx.service
输入以下内容:
-----------------------------------------------------------------------
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
-----------------------------------------------------------------------
运行以下命令来重新加载 systemd 守护程序并启动 Nginx 服务:
sudo systemctl daemon-reload
sudo systemctl start nginx
若要确保 Nginx 在系统启动时自动启动,运行以下命令:
systemctl status nginx
这将创建一个符号链接,将 Nginx 服务与启动目标关联起来,使得系统在引导过程中自动启动 Nginx。
###################################################################################
通过 supervisor 来将 Nginx 启动为守护进程。具体步骤如下:
安装 supervisor:(可能要重新下载和更新epel,这是centos7的扩展库)
yum remove epel-release
yum install epel-release
yum update epel-release
yum install -y supervisor
编写 supervisor 的配置文件 /etc/supervisord.d/nginx.ini,内容如下:
[program:nginx]
command=/usr/sbin/nginx -g "daemon off;"
autostart=true
autorestart=true
startsecs=5
startretries=10
user=root
stdout_logfile=/var/log/supervisor/nginx.log
stderr_logfile=/var/log/supervisor/nginx.log
其中,command 参数指定了启动 Nginx 的命令,并使用了 daemon off; 选项来避免将 Nginx 启动为守护进程,(systemctl里的nginx.server就失效了,systemctl 启动、停止或重启 Nginx 也就失效了)因为 supervisor 已经会将其启动为守护进程。autostart 和 autorestart 分别表示在启动 supervisor 时是否自动启动 Nginx,并在 Nginx 进程异常退出时是否自动重启。startsecs 和 startretries 分别表示启动 Nginx 的超时时间和尝试启动的次数。user 表示以哪个用户的权限启动 Nginx 进程。
在 supervisor 配置文件 /etc/supervisord.conf 中添加以下内容:
[include]
files = /etc/supervisord.d/*.ini
这样,在 supervisor 启动时会自动加载 /etc/supervisord.d/ 目录下的所有 .ini 文件,而不需要手动指定每个配置文件的路径。
启动 supervisor 和 nginx。
systemctl daemon-reload (如果修改了/etc/supervisord.conf,需要运行这个命令)
systemctl start supervisord
systemctl start nginx
经过这样的配置后,supervisor 将会启动 Nginx 并将其作为守护进程运行,即使 Nginx 进程异常退出或被意外终止,supervisor 也会自动重新启动它。
supervisorctl status可以看到启动的服务