首页 » 系统运维 » 正文

CentOS 7.5.1804下安装 Nginx 1.15.12 方法

下载安装包

下载地址:

http://nginx.org/en/download.html

解压

tar -zxvf nginx-1.15.12.tar.gz
cd nginx-1.15.12

安装依赖软件包

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

配置

执行配置

./configure

也可以自定义配置

./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

编译安装

执行命令编译和安装

make
make install

查找安装路径

whereis nginx

启动、停止、重启 nginx

启动

cd /usr/local/nginx/sbin/
./nginx

停止

./nginx -s stop  # 此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程
./nginx -s quit  # 此方式停止步骤是待nginx进程处理任务完毕进行停止

重启(重新加载配置文件)

./nginx -s reload

查看 nginx 进程

ps -ef|grep nginx

访问测试 nginx

设置开机自启动(安装服务)

在 /usr/lib/systemd/system 目录下创建 nginx.service 文件,内容如下:

[Unit]
Description=nginx
After=network.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 enable nginx.service

停止开机自启动

systemctl disable nginx.service

启动nginx服务

systemctl start nginx.service

重新启动服务

systemctl restart nginx.service

停止nginx服务

systemctl stop nginx.service

查看服务当前状态

systemctl status nginx.service

查看所有已启动的服务

systemctl list-units --type=service