在centos7上安装nginx

我这里安装的是nginx-1.18.0,在安装nginx前首先要确认系统中安装了gcc、pcre-devel、zlib-devel、openssl-devel,如果没有安装的话也没关系,后面会有一些报错的解决方案,等报错了再安装也行。

首先下载nginx-1.18.0,并上传到/usr/local下。

#解压
r -zxvf nginx-1.18.0.tar.gz
## 进入nginx目录下
cd nginx-1.18.0
# 配置
/configure --prefix=/usr/local/nginx

可能遇到的坑:

1.显示错误:[./configure: error: C compiler cc is not found],

是因为没有执行下列的命令:
yum -y install gcc gcc-c++ autoconf automake make  

2.显示错误: ./configure: error: the HTTP rewrite module requires the PCRE library.安装pcre-devel解决问题:

yum -y install pcre-devel

3.显示错误:error: the HTTP gzip module requires the zlib library.安装“zlib-devel”即可

yum install -y zlib-devel

如果出现一下界面,则nginx安装成功:

测试是否安装成功:切换到/usr/local/nginx/sbin目录下,执行:

./nginx -t

出现下面的提示是表示安装成功。

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

到这一步说明nginx已经安装成功,执行 ./nginx 就可以启动了;

接下来在浏览器上输入linux的ip地址,查看能否访问nginx。如果访问不了,说明需要开放端口。

yum install firewalld firewall-config

firewall-cmd --add-port=80/tcp --permanent
#重启防火墙
systemctl restart firewalld

# 如果出现以下异常
Failed to start firewalld.service: Unit is masked

# 执行命令,即可实现取消firewall的锁定
#解锁 
systemctl unmask firewalld 
#启动 
systemctl start firewalld

再次在浏览器上访问,出现一下界面,则说明已经成功了。

原文地址:https://www.cnblogs.com/pluto-charon/p/13512816.html