1.安装gcc、gcc-c++编译器
yum -y install gcc gcc-c++
2.查看80端口占用情况
netstat -tunlp
使用kill pid结束掉占用80端口的进程
3.分析nginx服务默认安装目录
yum install nginx
systemctl status nginx
查到的服务路径:/etc/systemd/system/multi-user.target.wants/nginx.service, 复制此文件内的全部文本备用,我们自定义服务时创建路径与此相同的服务
yum remove nginx
4.编译安装nginx
创建根目录env
cd /
mkdir env
cd /env
下载稳定版nginx源文件
wget http://nginx.org/download/nginx-1.20.2.tar.gz
tar zxvf nginx-1.20.2.tar.gz
参考http://nginx.org/en/docs/configure.html
./configure --prefix=/env/nginx/1.20.2
make & make install
make clean
cd /env/nginx/1.20.2
目录说明
conf -----配置文件
html -----网页文件
logs -----日志文件
sbin -----主要二进制程序
5.测试nginx
cd /env/nginx/1.20.2
./sbin/nginx
浏览网页显示如下,则nginx工作正常
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
关闭nginx
./sbin/nginx -s stop
6.systemctl添加自定义nginx服务
vim /etc/systemd/system/multi-user.target.wants/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/env/nginx/1.20.2/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.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPost=/bin/sleep 0.1
ExecStartPre=/usr/bin/rm -f /env/nginx/1.20.2/logs/nginx.pid
ExecStartPre=/env/nginx/1.20.2/sbin/nginx -t
ExecStart=/env/nginx/1.20.2/sbin/nginx
ExecReload=/env/nginx/1.20.2/sbin/nginx -s reload
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target
添加权限
chmod 754 /etc/systemd/system/multi-user.target.wants/nginx.service
7.启动nginx服务
systemctl daemon-reload
systemctl status nginx
systemctl start nginx
8.将nginx添加到环境变量PATH
echo 'export PATH=$PATH:/env/nginx/1.20.2/sbin' > /etc/profile.d/nginx.sh