Centos安装Nginx

106 阅读1分钟

一、查看Centos版本

本文使用的机器为Centos8,查看centos版本的命令如下:

cat /etc/redhat-release

二、安装依赖

yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel

二、下载Nginx

本文使用是Nginx-1.27.0版本,文件后缀为.tar.gz,下载NVM的链接如下:

  1. 官网:nginx.org/download/
  2. wget nginx.org/download/ng…

三、安装

  1. 将下载的NVM文件上传到/opt安装目录,然后解压,解压后的文件夹名字为:nginx-1.27.0
tar -zxvf nginx-1.27.0.tar.gz
  1. 进入Nginx目录,配置SSL:
cd nginx-1.27.0
# 不需要SSL认证
./configure --prefix=/opt/nginx
# 需要SSL认证
./configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module
  1. 编译安装:
make
make install
  1. 启动Nginx:
/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
  1. 重启Nginx:
/opt/nginx/sbin/nginx -s reload

四、开机自动启动

  1. 进入到/lib/systemd/system目录:
cd /lib/systemd/system
  1. vim创建nginx.service文件:
[Unit]
Description=nginx service
After=network.target 
    
[Service] 
Type=forking 
ExecStart=/opt/nginx/sbin/nginx
ExecReload=/opt/nginx/sbin/nginx -s reload
ExecStop=/opt/nginx/sbin/nginx -s quit
PrivateTmp=true
    
[Install] 
WantedBy=multi-user.target
  1. 加入开机自启动:
systemctl enable nginx
  1. 关闭开机自启动:
systemctl disable nginx
  1. 启动Nginx:
systemctl start nginx.service
  1. 停止Nginx:
systemctl stop nginx.service