“这是我参与更文挑战的第18天,活动详情查看: 更文挑战”
本篇是Nginx基础应用实战第一篇,接下来几天将介绍关于Nginx方面的知识点。
版本说明
常用版本分为四大阵营
Nginx开源版
Nginx plus 商业版
openresty
Tengine
Nginx安装
下载
编译安装
./configure --prefix=/usr/local/nginx
make
make install
如果出现警告或报错
安装perl库
yum install -y pcre pcre-devel
./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre=<path> option.
安装zlib库
yum install -y zlib zlib-devel
./configure: error: the HTTP gzip module requires the zlib library. You can either disable the module by using --without-http_gzip_module option, or install the zlib library into the system, or build the zlib library statically from the source with nginx by using --with-zlib=<path> option.
多版本冲突
如果出现多版本错误,用提示信息进行解决!
启动Nginx
进入安装好的目录/usr/local/nginx/sbin
启动
./nginx
快速停止
./nginx -s stop
优雅关闭
在退出前完成已经接受的连接请求
./nginx -s quit
重新加载配置
./nginx -s reload
验证配置文件是否正确
./nginx -t
查看版本
./nginx -v
查看Nginx信息
会展示出已经安装的module
./nginx -V
关于防火墙
centos7和centos6 命令不太一样!
关闭防火墙
systemctl stop firewalld.service
禁止防火墙开机启动
systemctl disable firewalld.service
放行端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
重启防火墙
firewall-cmd --reload
安装成系统服务
创建服务脚本
注意脚本内容的路径地址!
vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
重新加载系统服务
systemctl daemon-reload
启动服务
systemctl start nginx.service
开机启动
systemctl enable nginx.service
总结
本篇介绍了Nginx的基本安装、及安装过程中依赖的类库、安装过程中出现的错误解决办法。其次介绍了下关于防火墙问题、Nginx服务安装成系统服务的命令。下一篇介绍Nginx基础应用实战第二篇。
欢迎大家关注公众号(MarkZoe)互相学习、互相交流。