CentOS安装Nginx

1、安装相关组件

yum install -y openssl*
yum -y install ncurses-devel
yum -y install gcc-c++
yum -y install gcc gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel

2、编译安装Nginx

#获取nginx
wget https://nginx.org/download/nginx-1.21.3.tar.gz
  
#解压显示解压文件
tar zxf nginx-1.21.3.tar.gz
  
#进入nginx源文件目录
cd nginx-1.21.3
  
#编译
#默认https没有打开,需要添加 --with-http_ssl_module
./configure --with-http_ssl_module
  
#安装Nginx
make && make install

3、添加环境变量
i.打开文件

vim /etc/profile

ii.最后添加

export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin

iii.重载资源

source /etc/profile

4、启动Nginx

nginx

5、设置开机自动启动

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=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

重载并设置自启

systemctl daemon-reload
systemctl enable nginx.service

命令

# 启动服务
systemctl start nginx.service
# 重新加载nginx服务
systemctl reload nginx.service
# 停止nginx服务
systemctl stop nginx.service

部分命令

1、重启

nginx –s reload

2、停止

nginx –s stop

3、测试

nginx –t

4、强制关闭

pkill nginx

你可能感兴趣的:(CentOS安装Nginx)