一、下载nginx安装包
访问地址:nginx.org
根据自己的需求下载合适的安装包
wget https://nginx.org/download/nginx-1.17.7.tar.gz
二、安装gcc
gcc是用来编译下载下来的nginx源码
yum install gcc-c++
三、安装pcre和pcre-devel
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,pcre-devel 是使用 pcre 开发的一个二次开发库。
yum install -y pcre pcre-devel
四、安装zlib
zlib提供了很多压缩和解方式,nginx需要zlib对http进行gzip
yum install -y zlib zlib-devel
五、安装openssl
openssl是一个安全套接字层密码库,nginx要支持https,需要使用openssl
yum install -y openssl openssl-devel
六、解压你所下载的nginx安装包
tar -zxvf nginx-1.17.7.tar.gz
七、进入你所解压后的目录,ls就能找到
cd nginx-1.17.7/
八、三条命令完成安装与编译
1、构建
-
默认方式安装:
./configure
默认安装路径:/usr/local/nginx
-
指定安装路径安装:
./configure --prefix=/usr/local/nginx-summergaobooks
安装路径:/usr/local/nginx-summergaobooks
- 同时安装https模块
./configure --prefix=/usr/local/nginx-summergaobooks --with-http_stub_status_module --with-http_ssl_module
2、编译
make
3、安装
make install
九、进入默认安装目录启动nginx
默认安装路径:/usr/local/nginx
启动nginx:
cd /usr/local/nginx/sbin
./nginx
停止nginx
1. 快速关闭
./nginx -s stop
2. 优雅关闭
(不接受新的连接请求,等待旧的连接请求处理完毕再关闭):nginx -s quit 或者 kill -QUIT 主进程号
./nginx -s quit
注意:执行该命令的用户应该是启动nginx的用户
[root@summergao_centos sbin]# ps -ef |grep nginx
root 18002 1 0 21:57 ? 00:00:00 nginx: master process ./nginx
nobody 21413 18002 0 22:23 ? 00:00:00 nginx: worker process
root 22266 12532 0 22:28 pts/0 00:00:00 grep --color=auto nginx
kill -QUIT 18002
3. 重新加载配置文件
nginx -s reload 或者 kill -HUP 主进程号
./nginx -s reload
或者
ps -ef |grep nginx
[root@summergao_centos sbin]# ps -ef |grep nginx
root 18002 1 0 21:57 ? 00:00:00 nginx: master process ./nginx
nobody 21413 18002 0 22:23 ? 00:00:00 nginx: worker process
root 22266 12532 0 22:28 pts/0 00:00:00 grep --color=auto nginx
kill -HUP 18002
4. 查看nginx版本信息
./nginx -v
[root@summergao_centos sbin]# ./nginx -v
nginx version: nginx/1.17.7
5. 查看nginx版本信息,编译版本,和配置参数:nginx -V
./nginx -V
[root@summergao_centos sbin]# ./nginx -V
nginx version: nginx/1.17.7
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
configure arguments: --prefix=/usr/local/nginx-summergaobooks
十、配置开机启动
切换到/lib/systemd/system/目录,创建nginx.service文件vim nginx.service
# cd /lib/systemd/system/
# vim 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 enable nginx.service
启动相关指令
systemctl start nginx.service #启动nginx
systemctl stop nginx.service #结束nginx
systemctl restart nginx.service #重启nginx
问题解决
Nginx 403 forbidden错误的原因以及解决方法
问题原因
查看nginx的启动用户,发现是nginx,而为是用root启动的
ps aux|grep nginx
解决办法
将nginx.config的user改为和启动用户一致
vim etc/nginx.conf
#user nobody;
user root; # 就是这里
worker_processes auto;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
./nginx -s reload