源码方式安装
- 安装依赖
yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel
- 安装
# 下载
wget http://nginx.org/download/nginx-1.15.0.tar.gz
# 移动到/usr/local/src/解压
tar -zxvf nginx-1.15.0.tar.gz
cd nginx-1.15.0
./configure --prefix=/usr/local/nginx
make && make install
- 配置防火墙
# 临时关闭防火墙
systemctl stop firewalld.service
# 或者添加端口映射
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
- 测试安装是否成功
/usr/local/nginx/sbin/nginx -t
- 启动(使用默认配置)
/usr/local/nginx/sbin/nginx
image.png
- 配置开机启动
在/etc/rc.local追加:
/usr/local/nginx/sbin/nginx
- nginx常用命令
# 启动
/usr/local/nginx/sbin/nginx
# 关闭
/usr/local/nginx/sbin/nginx -s stop
# 重启
/usr/local/nginx/sbin/nginx -s reload
- 配置为系统服务
创建文件vim /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 reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
退出并保存文件,执行systemctl enable nginx.service使nginx开机启动
systemctl start nginx.service # 启动nginx
systemctl stop nginx.service # 结束nginx
systemctl restart nginx.service # 重启nginx
网友评论