centos8编译安装nginx

编译安装
1.准备

nginx的一些模块依赖一些lib库,所以在安装之前,先装依赖库,c++,gcc,openssl-devl,pcre-devel和zlib-devel

#编译环境
yum install gcc-c++

#rewrite模块需要pcre库
yum install pcre pcre-devel

#gzip模块需要zlib库
yum install zlib zlib-devel

#SSL功能需要openssl库
yum install openssl openssl-devel

2.上传gz安装包到/usr/local目录下下载

3.解压
tar zxvf nginx-1.16.0.tar.gz
4.进入nginx-1.16.0
cd nginx-1.16.0
5.编译安装

./configure
make
make install

#查看版本
/usr/local/nginx/sbin/nginx -v
6.启动 Nginx
cd /usr/local/nginx/sbin
./nginx
7.设置开机自动启动
Systemd服务文件以.service结尾,比如现在要建立nginx为开机启动,如果用yum install命令安装的,yum命令会自动创建nginx.service文件,直接用命令:
systemcel enable nginx.service

在系统服务目录里创建nginx.service文件
vi /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
重新centos后,nginx就自动启动了

nginx其他命令:
systemctl start nginx.service (启动nginx服务)
systemctl stop nginx.service (停止nginx服务)
systemctl enable nginx.service (设置开机自启动)
systemctl disable nginx.service (停止开机自启动)
systemctl status nginx.service (查看服务当前状态)
systemctl restart nginx.service (重新启动服务)
systemctl list-units --type=service (查看所有已启动的服务)

你可能感兴趣的:(服务器部署,nginx,centos)