一、简介
- 需求:本人最近做一些实验,需要在宿主机和虚拟机之间进行通信,因此选择nginx来做反向代理。
- 内容:本人尝试了在虚拟机中安装nginx、在虚拟机的docker容器中安装nginx、使用shell脚本自动化安装nginx。因此,将分为三篇文章记录和分享在安装nginx过程中的感想以及遇到的问题。
- 环境:虚拟机 + centos
二 、安装过程
安装过程主要分为三部分:
- 初始化安装环境
- 安装nginx
- 配置systemctl,使用systemctl启动、停止nginx
1、初始化安装环境
注意:为了避免不必要的麻烦安装前先关闭防火墙systemctl stop firewalld
yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
2、安装nginx
wget http://nginx.org/download/nginx-1.14.0.tar.gz
(下载)tar -zxvf nginx-1.14.0.tar.gz
(解压)- cd到文件路径(
cd nginx-1.14.0
)(切换路径) ./configure --prefix=/usr --sbin-path=/usr/local/sbin/nginx --conf-path=/usr/local/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/usr/local/run/nginx/nginx.pid --lock-path=/usr/local/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/tem/nginx/client --http-proxy-temp-path=/var/tem/nginx/proxy --http-fastcgi-temp-path=/var/tem/nginx/fcgi --with-http_stub_status_module
(配置nginx相关参数,以下是参数的具体意思)
nginx path prefix: "/usr"
nginx binary file: "/usr/local/sbin/nginx"
nginx modules path: "/usr/modules"
nginx configuration prefix: "/usr/local/nginx"
nginx configuration file: "/usr/local/nginx/nginx.conf"
nginx pid file: "/usr/local/run/nginx/nginx.pid"
nginx error log file: "/var/log/nginx/error.log"
nginx http access log file: "/var/log/nginx/access.log"
nginx http client request body temporary files: "/var/tem/nginx/client"
nginx http proxy temporary files: "/var/tem/nginx/proxy"
nginx http fastcgi temporary files: "/var/tem/nginx/fcgi"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
-
安装
make
make install
-
启动\关闭
- nginx -c + 配置文件路径(
nginx -c /usr/local/nginx/nginx.conf
) - 启动nginx
/usr/local/sbin/nginx -c /usr/local/nginx/nginx.conf
- 快速停止nginx
/usr/local/nginx/sbin/nginx -s stop
- 完整有序的停止nginx
/usr/local/nginx/sbin/nginx -s quit
- 注:stop和quit的区别在于
- quit是一个优雅的关闭方式,Nginx在退出前完成已经接受的连接请求
- Stop 是快速关闭,不管有没有正在处理的请求。
- 进程的方式
pkill -9 nginx
- ps -ef | grep nginx
- kill -QUIT 主进程号 :从容停止Nginx
- kill -TERM 主进程号 :快速停止Nginx
- pkill -9 nginx :强制停止Nginx
- 平滑重启nginx:
- kill -HUP 主进程号
- nginx -c + 配置文件路径(
3、配置systemctl,使用systemctl启动、停止nginx
- 将nginx服务添加到systemctl
-
修改nginx配置文件,开启pid(
vi /usr/local/nginx/nginx.conf
)- 在配置文件中加入
pid /usr/local/run/nginx/nginx.pid;
- 注意:该路径与步骤8中设置的配置文件路径要保持一致,如果不知道自己的配置文件路径可以通过命令
find / -name nginx.conf
查找
- 在配置文件中加入
-
关闭nginx服务
ps aux|grep nginx
kill -9 进程id
-
配置systemctl服务:
- systemd的service文件都在
/usr/lib/systemd/system/
路径下,需要在该路径下新建一个nginx.service文件 vi /usr/lib/systemd/system/nginx.service
,在其中添加以下内容:
[Unit] Description=nginx - high performance web server After=network.target remote-fs.target nss-lookup.target /usr/local/sbin/nginx -c /usr/local/nginx/nginx.conf [Service] Type=forking PIDFile=/usr/local/run/nginx/nginx.pid ExecStartPre=/usr/local/sbin/nginx -t -c /usr/local/nginx/nginx.conf ExecStart=/usr/local/sbin/nginx -c /usr/local/nginx/nginx.conf ExecReload=/usr/local/sbin/nginx -s reload ExecStop=/usr/local/sbin/nginx -s stop ExecQuit=/usr/local/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target
- systemd的service文件都在
-
systemctl daemon-reload
使nginx.service文件生效 -
systemctl start/stop/reload/quit nginx.service
启动、停止、重启、退出 -
systemctl enable nginx.service
开机自启动
注意:nginx.service文件中nginx的路径(/usr/local/sbin/nginx
)要与步骤8中配置的一致、配置文件路径(/usr/local/nginx/nginx.conf
)和配置文件中设置的pid路径
尽量与步骤8中配置的一致
原因:- 首先来看nginx的启动命令
/usr/local/sbin/nginx -c /usr/local/nginx/nginx.conf
,从中可以看出/usr/local/sbin/nginx
为nginx的可执行路径,-c /usr/local/nginx/nginx.conf
为启动的参数,也可以通过该参数来改变启动时使用的nginx配置文件。 - 通过systemctl来管理nginx进程时,需要读取nginx配置文件中的pid,通过pid管理nginx进程。
- 首先来看nginx的启动命令
-
三、 总结
在虚拟机+centos7环境下安装nginx的过程已经展示完毕,下一篇文章将介绍在docker中安装nginx。文章可能在编辑过程中,由于个人疏忽、不同版本markdown解析器不兼容等原因导致字符书写错误,导致安装失败。本文属于原创,若有引用请注明出处。若有疑问或错误,欢迎各位指出,可以评论或者跟本人联系。