安装Nginx
Nginx官网下载Nginx源码包
wget http://nginx.org/download/nginx-1.21.6.tar.gz
安装Nginx编译依赖包
Nginx依赖于pcre、zlib、openssl,在编译前配置时如果有问题
可以使用yum方式安装三个包(pcre-devel、zlib-devel、openssl-devel)
yum install -y pcre-devel zlib-devel openssl-devel wget gcc tree vim
解压Nginx源码包
tar -xzvf nginx-1.21.6.tar.gz
进入解压后的/opt/soft/nginx-1.21.6
目录进行编译前配置
cd /opt/soft/nginx-1.21.6
./configure --prefix=/usr/local/nginx --with-http_ssl_module
执行编译安装
make && make install
Nginx配置系统系统服务
- 在
/usr/lib/systemd/system
目录下添加nginx.service
,内容如下
vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx web service
Documentation=http://nginx.org/en/docs/
After=network.target
[Service]
Type=forking
#nginx pid文件位置
PIDFile=/usr/local/nginx/logs/nginx.pid
#-t 测试nginx配置文件 -c 指定nginx配置文件位置
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=default.target
- 添加完成后如果权限有问题需要进行权限设置
chmod 755 /usr/lib/systemd/system/nginx.service
- 使用系统命令来操作Nginx服务
启动:
systemctl start nginx
停止:systemctl stop nginx
重启:systemctl restart nginx
重新加载配置文件:systemctl reload nginx
查看nginx状态:systemctl status nginx
开机启动:systemctl enable nginx
Nginx命令配置到系统环境
- 修改/etc/profile文件
vi /etc/profile
在最后一行添加
export PATH=$PATH:/usr/local/nginx/sbin
- 使之立即生效
source /etc/profile
- 执行nginx命令
nginx -V