如何在centos7中安装 Nginx 的安装

本文详细描述了如何在Linux系统中安装Nginx,包括解压、配置编译、缺失库的安装(如gcc、pcre、zlib)、系统服务设置、防火墙配置及开机启动的过程。
摘要由CSDN通过智能技术生成

Nginx 的安装

解压 nginx

tar zxvf nginx-1.24.0.tar.gz 

进入 nginx目录

cd nginx-1.24.0

查看 nginx-1.24.0 目录下有什么

ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src

尝试安装 :

./configure 
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
//解决方式
//安装 gcc

yum install -y gcc

//下载下载完毕后 正常安装

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

接着又出现了以下问题

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

//问题原因
没有pcre 这个库
解决办法
安装perl 库

yum install -y pcre pcre-devel

接着

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

然后发现又有报错信息

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

报错原因
缺少zlib 库

解决方式
安装zlib库

yum install -y zlib zlib-devel

接着

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

如果没有问题了 我们就 make
输入

make

然后就

make install

启动nginx
首先进入安装好的目录/usr/local/nginx/sbin

cd /usr/local/nginx/sbin/

然后看看 sbin目录下有什么
有一个nginx 可执行文件
我么只要输入./nginx就能将这个服务启动起来了

启动

./nginx

#快速停止

./nginx -s stop

#优雅关闭,在退出前完成已经接受连接请求

./nginx -s quit

重新加载配置

./nginx -s reload

//启动完成后我们看看是不是真正请起来了
打开浏览器
输入你的ip地址
假设我的ip地址为
192.168.66.136
发现启动访问不了

//出现这个的原因是
由于防火墙的干扰 导致我们访问不到
解决办法
关闭防火墙

systemctl stop firewalld.service


禁止防火墙开机启动

systemctl disable firewalld.service

我们重新访问一下
打开浏览器
输入
192.168.66.136

出现以下内容表示成功

Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

关于防火墙的操作

关闭防火墙

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

再看看

ps -ef | grep nginx

root 79990 1 0 20:10 ? 00:00:00 nginx: master process ./nginx
nobody 79991 79990 0 20:10 ? 00:00:00 nginx: worker process
root 114217 44026 0 20:48 pts/0 00:00:00 grep --color=auto nginx

我们将nginx 关了

./nginx -s stop

root 115229 44026 0 20:49 pts/0 00:00:00 grep --color=auto nginx

#启动
systemctl start nginx
# 查看状态
systemctl status nginx

启动服务

systemctl start nginx.service

开机启动

systemctl enable nginx.service
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值