【CentOS7】【Nginx】CentOS7源码编译并安装nginx

更新yum

sudo yum update -y

安装必要的编译工具和依赖项

sudo yum install -y gcc pcre-devel openssl-devel zlib-devel

下载Nginx源代码

wget http://nginx.org/download/nginx-1.20.1.tar.gz

解压源代码包

tar -zxvf nginx-1.20.1.tar.gz

进入解压后的目录

cd nginx-1.20.1

配置编译选项

./configure

编译和安装

make
sudo make install

设置开机自启动

sudo vi /etc/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/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

保存并关闭文件

设置Nginx服务为开机自启动

sudo systemctl enable nginx

启动Nginx服务

sudo systemctl start nginx

你可能感兴趣的:(nginx,运维,服务器)