Centos7编译安装nginx

Linux2022-08-26
Centos7编译安装nginx-下一朵云

一、安装环境准备

安装 gcc、PCRE pcre-devel、zlib、openssl

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

二、下载安装

1.直接下载.tar.gz安装包

官网下载:https://nginx.org/en/download.html

nginx-1.18.0下载:https://cloud.189.cn/t/RVvMFn7Vj6Fb

Centos7编译安装nginx-下一朵云

2.使用wget命令下载(推荐)

yum install -y weget && wget -c https://nginx.org/download/nginx-1.18.0.tar.gz

3.解压缩

tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0

4.配置

其实在 nginx-1.18.0 版本中你就不需要去配置相关东西,默认就可以了。当然,如果你要自己配置目录也是可以的。

1) 使用默认配置

./configure

注意:如要开启ssl请添加配置否则会报错,请查看 Nginx配置https,解决“parameter requires ngx_http_ssl_module”的问题

2) 自定义配置(不推荐)

./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/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

注:将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录

5.编译安装

make && make install

三、设置启动、重启、停止、自启动

1. 查找安装路径

whereis nginx
Centos7编译安装nginx-下一朵云

2.启动、停止nginx

cd /usr/local/nginx/sbin/
./nginx 
./nginx -s stop
./nginx -s quit
./nginx -s reload

注: ./nginx -s quit:此方式停止步骤是待nginx进程处理任务完毕进行停止
./nginx -s stop:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程

查询nginx进程

ps -ef | grep nginx

3.重启nginx

1)先停止再启动(推荐)

对 nginx 进行重启相当于先停止再启动,即先执行停止命令再执行启动命令。

./nginx -s quit
./nginx

2) 重新加载配置文件

nginx的配置文件nginx.conf 修改后,要想让配置生效需要重启 nginx,使用-s reload不用先停止 nginx再启动 nginx 即可将配置信息在 nginx 中生效

./nginx -s reload

启动成功后,关闭防火墙或开放80端口,在浏览器访问可以看到这样的页面

Centos7编译安装nginx-下一朵云

4.开机自启动

即在rc.local增加启动代码就可以了

vi /etc/rc.local

增加一行 /usr/local/nginx/sbin/nginx

Centos7编译安装nginx-下一朵云

给添加rc.local执行权限

chmod a+x /etc/rc.d/rc.local

到这里,nginx就安装完毕了,启动、停止、重启、开机自启动等操作也都完成了!

版权声明:本文为下一朵云发布文章,转载请附上原文出处链接和本声明。
本文链接:https://www.orcy.net.cn/1404.html

​ 广告:HCIE-Security认证课程辅导资料(付费)

暂无评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注