去官网http://nginx.org/下载对应的nginx包,推荐使用稳定版本
安装gcc环境
yum install gcc-c++
安装PCRE库,用于解析正则表达式
yum install -y pcre pcre-devel
zlib压缩和解压缩依赖
yum install -y zlib zlib-devel
SSL 安全的加密的套接字协议层,用于HTTP安全传输,也就是https
yum install -y openssl openssl-devel
tar -zxvf nginx-1.24.0.tar.gz
编译之前,先创建nginx临时目录,如果不创建,在启动nginx的过程中会报错
mkdir /var/temp/nginx -p
在nginx目录,输入如下命令进行配置,目的是为了创建makefile文件
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/lib/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \
--with-http_ssl_module --with-stream
## ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream
/var/lib/nginx/nginx.pid 配置 在这里放置重启 丢失pid文件
make & make install
进入目录:cd /usr/local/nginx/sbin
启动:./nginx
停止:./nginx -s stop
重新加载:./nginx -s reload
添加防火墙:
firewall-cmd --zone=public --add-port=80/tcp --permanent
重新载入 firewall-cmd --reload
上面命令执行完成之后 安装地址如下:
/usr/local/nginx/conf
也可使用 find / |grep nginx 来寻找安装位置
设置自启动:
- 在 /etc/systemd/system/ 目录下创建一个新的服务文件,例如命名为
/etc/systemd/system/
[Unit]
Description=nginx service
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
- 保存文件后,通过以下命令使服务生效:
sudo systemctl enable nginx.service
sudo systemctl start nginx.service
禁用命令:
sudo systemctl disable nginx.service
nginx 常用配置:
// nginx常用命令
cd usr/local/nginx/sbin
./nginx // 启动
./nginx -s stop // 停止
./nginx -s quit // 安全退出
./nginx -s reload // 重新加载配置文件
ps aux|grep nginx // 查看nginx进程