1、环境安装

在linux下安装nginx,首先需要安装 gcc-c++编译器。然后安装nginx依赖的pcre和zlib包。最后安装nginx即可

1.1、先安装gcc-c++编译器

yum install gcc-c++
yum install -y openssl openssl-devel

1.2、再安装pcre包

yum install -y pcre pcre-devel

1.3、再安装zlib包

yum install -y zlib zlib-devel

2、下面进行nginx的安装

2.1、准备nginx安装包

方式1:在网上下nginx包上传至Linux(https://nginx.org/download/,

wget https://nginx.org/download/nginx-1.99.9.tar.gz

方式2:也可以本地直接上传
例如我将物理机上的nginx安装包上传至服务器/root/wenjian的文件夹下

在这里插入图片描述

2.2、解压压缩包

tar -zxvf nginx-1.9.9.tar.gz

2.3、将解压后的压缩包放到/usr/local下

mv nginx-1.9.9 /usr/local/

2.4、切换到/usr/local文件夹下,修改nginx-1.9.9文件名

cd /usr/local
mv nginx-1.9.9 nginx

2.5、进入nginx文件夹

cd nginx

2.6、配置

./configure

2.7、编译安装

make
make install

2.8、查找安装路径

whereis nginx

2.9、进入sbin目录,可以看到有一个可执行文件nginx,直接./nginx执行就OK了

./nginx

2.10、查看是否启动成功

ps -ef | grep nginx

在这里插入图片描述
我的物理机访问:
在这里插入图片描述
虚拟机访问:
在这里插入图片描述

3、注意问题

3.1、防火墙

  • 查看防火墙是否开启
systemctl status firewalld
  • 启动防火墙后,默认没有开启任何端口,需要手动开启端口。nginx默认是80端口
手动开启端口命令
firewall-cmd --zone=public --add-port=80/tcp --permanent
命令含义: --zone #作用域 --add-port=80/tcp #添加端口,格式为:端口/通讯协议 --permanent #永久生效,没有此参数重启后失效
  • 开启后需要重启防火墙才生效
systemctl restart firewalld.service
  • 查看防火墙是否开启了80端口的访问
firewall-cmd --list-all

在这里插入图片描述

3.2、如果启动后出现了如下的问题就是80端口被占用

netstat -tunlp | grep 80

在这里插入图片描述这里因为我之前开启了的是被nginx.master或者nginx.woeker占用就不用管,如果不是这个的话那就把那个进程关闭掉

kill -9 进程号

关闭之后重启nginx再次访问!!

3.3、nginx报错

nginx报错:nginx: [alert] could not open error log file: open() “/usr/local/nginx/logs/error.log“ faile
在这里插入图片描述
1、分析原因

nginx/目录下没有logs文件夹

2、解决方案 :创建文件夹并授权

mkdir logs
chmod 700 logs

3、验证
输入以下命令

/usr/local/nginx/sbin/nginx -t

4、正常情况的信息输出:

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

4、重启nginx

nginx -t nginx -s reload
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐