1.Nginx简介
Nginx (发音为[engine x])专为性能优化而开发,其最知名的优点是它的稳定性和低系统资源消耗,以及对并发连接的高处理能力(单台物理服务器可支持30000~50000个并发连接), 是一个高性能的 HTTP 和反向代理服务器,也是一个IMAP/POP3/SMTP 代理服。
2.安装准备
gcc安装
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:
yum install gcc-c++
pcre安装
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。
yum install pcre
yum install pcre pcre-devel
zlib安装
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
yum install zlib
yum install zlib zlib-devel
OpenSSL安装
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
yum install openssl
yum install openssl openssl-devel
3.Nginx安装
Nginx版本
官网下载地址:http://nginx.org/en/download.html
版本说明:
Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以说是开发版
Stable version:最新稳定版,生产环境上建议使用的版本
Legacy versions:遗留的老版本的稳定版
Nginx下载
切换到/soft目录,下载软件包
cd /soft/
wget http://nginx.org/download/nginx-1.20.2.tar.gz
如没有wget命令则安装:
yum -y install wget
解压
tar -zxvf nginx-1.20.2.tar.gz
安装配置
-
新建nginx用户和组
groupadd nginx
useradd -g nginx -d /home/nginx nginx
passwd aholic123..
-
自定义配置
./configure \
--user=nginx --group=nginx \ #安装的用户组
--prefix=/soft/nginx \ #指定安装路径
--pid-path=/soft/nginx/prod/nginx.pid \ #进程文件安装目录路径
--lock-path=/soft/nginx/prod/lock/nginx.lock \ #设置NGINX锁文件安装目录路径
--error-log-path=/soft/nginx/prod/log/error.log \ #设置错误日志文件安装目录路径
--http-log-path=/soft/nginx/prod/log/access.log \ #设置访问日志文件存放目录路径。
--http-client-body-temp-path=/soft/nginx/prod/temp/client \ #设置用于存储客户端请求主体的临时文件存放目录路径
--http-proxy-temp-path=/soft/nginx/prod/temp/proxy \ #设置用于存储从代理服务器接受的数据临时文件存放目录路径
--http-fastcgi-temp-path=/soft/nginx/prod/temp/fastcgi \ #设置用于存储从FastCGI服务器接受的数据临时文件存放目录路径
--http-uwsgi-temp-path=/soft/nginx/prod/temp/uwsgi \ #设置用于存储从UwSGI服务器接受的数据临时文件存放目录路径
--http-scgi-temp-path=/soft/nginx/prod/temp/scgi \ #设置用于存储从SCGI服务器接受的数据临时文件存放目录路径
--with-http_stub_status_module \ #监控nginx状态,需在nginx.conf配置
--with-http_ssl_module \ #支持HTTPS
--with-http_sub_module \ #支持URL重定向
--with-http_gzip_static_module #静态压缩
--add-module=/root/nginx-sticky-1.2.5 #安装sticky模块
-
编译
make && make install
-
nginx命令全局执行设置
cd /soft/nginx/sbin/
ln -s /soft/nginx/sbin/nginx /usr/local/bin/nginx
或者修改/etc/profile文件
export NGINX_HOME=/usr/local/nginx
export PATH=${NGINX_HOME}/sbin:${PATH}
生效
source /etc/profile
4.Nginx相关命令
-
启动
nginx
-
停止
nginx -s stop
nginx -s quit
nginx -s quit:此方式停止步骤是待nginx进程处理任务完毕进行停止。
nginx -s stop:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
-
动态加载
ngins -s reload
nginx -s reload:动态加载,当配置文件nginx.conf有变化时执行该命令动态加载。
-
测试配置文件nginx.conf正确性
nginx -t
-
版本查看
nginx -v
-
查看加载的模块
nginx -V
5.开机自启动
-
第一种
编辑/etc/rc.d/rc.local文件,新增一行/usr/local/nginx/sbin/nginx
cd /etc/rc.d
sed -i '13a /soft/nginx/sbin/nginx' /etc/rc.d/rc.local
chmod u+x rc.local
-
第二种
切换到/lib/systemd/system/目录,创建nginx.service文件vim nginx.service
cd /lib/systemd/system/
vim nginx.service
文件内容如下:
#服务描述性的配置
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target
#服务关键配置
[Service]
Type=forking
#pid文件位置
#要与nginx配置文件中的pid配置路径一致,这个很重要,否则会服务启动失败
PIDFile=/soft/nginx/prod/nginx.pid
#启动前检测 nginx配置文件 是否正确
ExecStartPre=/soft/nginx/sbin/nginx -t -c /soft/nginx/conf/nginx.conf
#启动
ExecStart=/soft/nginx/sbin/nginx -c /soft/nginx/conf/nginx.conf
#重启
ExecReload=/bin/kill -s HUP $MAINPID
#关闭
ExecStop=/bin/kill -s QUIT $MAINPID
KillMode=process
KillSignal=SIGQUIT
TimeoutStopSec=5
PrivateTmp= true
[Install]
WantedBy=multi-user.target
启动命令
systemctl daemon-reload 重新加载 systemd
systemctl enable nginx.service 开机启动
systemctl start nginx.service 启动nginx
systemctl stop nginx.service 结束nginx
systemctl restart nginx.service 重启nginx
systemctl disable nginx.service 停止开机自启动
systemctl status nginx.service 查看服务当前状态
systemctl list-units --type=service 查看所有已启动的服务
-
第三种
(1)添加文件:
vi /etc/init.d/nginx
将下面代码复制加入即可:
# chkconfig: - 85 15
# description: nginx is a World Wide Web server. It is used to serve
nginx=”/usr/local/nginx/sbin/nginx” //修改成nginx执行程序的路径。
NGINX_CONF_FILE=”/usr/local/nginx/conf/nginx.conf” //修改成nginx.conf文件的路径。
保存后设置文件的执行权限
chmod a+x /etc/init.d/nginx
至此就可以通过下面指令控制启动停止
/etc/init.d/nginx start
/etc/init.d/nginx stop
上面的方法完成了用脚本管理nginx服务的功能,但是还是不太方便。
先将nginx服务加入chkconfig管理列表:
chkconfig --add /etc/init.d/nginx
加完这个之后,就可以使用service对nginx进行启动,重启等操作了。
service nginx start
service nginx stop
service nginx restart
最后设置开机自动启动
chkconfig nginx on