编译安装 Nginx-Linux篇

310 阅读1分钟

服务器环境

  • CentOS7.4

1.安装 PCRE 和 zlib 库

#安装 PCRE 和 zlib 库
yum -y install pcre pcre-devel
yum install -y zlib-devel

2.下载 Nginx

wget https://nginx.org/download/nginx-1.14.0.tar.gz
# 解压
tar -xvzf nginx-1.14.0.tar.gz

3.下载 OpenSSL

wget https://www.openssl.org/source/openssl-1.1.1-pre5.tar.gz
# 解压
tar -xvzf openssl-1.1.1-pre5.tar.gz
mv openssl-1.1.1-pre5 /usr/local/

4.编译Nginx

cd nginx-1.4
# 编译
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module --with-openssl=/usr/local/openssl-1.1.1-pre5
# 安装
make && make install

5.安装后的Nginx目录

/usr/local/nginx
# 启动
cd /usr/local/nginx/sbin
./nginx

常用命令

./nginx  # 启动
./nginx -t # 检查配置文件
./nginx -s reload # 重启
./nginx -s stop # 停止

附带一份开机自动启动服务

使用 systemctl 添加自定义服务

在/usr/lib/systemd/system目录添加 nginx.service

nginx.service

[Unit]
Description=nginx 
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

服务常用命令

#重载系统服务:
systemctl daemon-reload
#设置开机启动:
systemctl enable nginx.service
#启动服务:
systemctl start nginx.service
#停止服务:
systemctl stop nginx.service
#重启服务:
systemctl reload nginx.service