联系QQ 284710375
首页 > 技术分享 > LNMP|WAMP
收藏

CentOS编译安装Nginx,并设置为自启系统服务2022-07-20 10:05:32

大潇博客 原创文章,转载请标明出处

Nginx有几个阵营,比如:Nginx plus 商业版、openresty、Tengine等,这里选择Nginx从官网下载的开源版,文章末尾有附件


编译安装

首先检查“/usr/local”目录下是含有nginx文件夹,若有此文件夹,且包含文件,请合理移除,以免引起错误


将nginx的压缩包解压后,进入文件夹,输入以下命令编译安装

./configure --prefix=/usr/local/nginx

CentOs默认安装位置即为“/usr/local”,这个文件夹好比windows中c盘下的“Program Files”,大多软件默认都安装在这里,上面使用“--prefix=/usr/local/nginx”是为了强调,可以使用此命令自定义安装位置


上文的编译安装,可能会报下面几种常见的警告或报错

提示

checking for OS

+ Linux 3.10.0-693.el7.x86_64 x86_64

checking for C compiler ... not found


./configure: error: C compiler cc is not found

安装gcc

yum install -y gcc


提示

./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=option.

安装perl库

yum install -y pcre pcre-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=option.

安装zlib库

yum install -y zlib zlib-devel


若还有其它提示,请百度查询

编译完成后,接下来执行

make

make install


全部执行完毕,安装成功


启动Ngixn

进入安装好的目录 /usr/local/nginx/sbin

./nginx 启动

./nginx -s stop 快速停止

./nginx -s quit 优雅关闭,在退出前完成已经接受的连接请求

./nginx -s reload 重新加载配置


设置防火墙

防火墙在多数生产环境中的作用其实也没多大,可以选择永久关闭,若不放心可选择单独开启端口


关闭防火墙

systemctl stop firewalld.service


禁止防火墙开机启动

systemctl disable firewalld.service


放行端口

firewall-cmd --zone=public --add-port=80/tcp --permanent


重启防火墙

firewall-cmd --reload


把Nginx安装成系统服务

创建服务脚本

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在运行,先关掉再启动服务,以免引起错误,使用

ps -ef | grep nginx

返回如下结果,表示有nginx在运行

nginx.png


使用下面命令查看nginx状态

systemctl status nginx.service

返回active(running)表示nginx在运行,也可看到上次启动时间等信息

nginx_status.png


对于nginx的目录结构,大多都理解,这里多说几句闲话,也比较简单

在/usr/local/nginx/logs 目录下有三个文件

access.log

error.log

nginx.pid

nginx_log.png

access.log 对应访问日志,每一次访问都会被记录

error.log 记录错误信息

nginx.pid 在nginx启动后,才会有这个文件,文件里只有一个进程识别号,这个进程号和上面“ps -ef | grep nginx”命令看到的nginx进程号一致

所以可以使用“kill 进程号”,杀死nginx进程


下载地址:nginx-1.21.6

下载地址:Nginx安装文档

打赏

阅读排行

大家都在搜

博客维护不易,感谢你的肯定
扫码打赏,建议金额1-10元
  • 15601023311