豫ICP备17040950号-2

linux下手动自定义安装nginx

文章目录
  1. 1. 适用版本
  2. 2. 必备前提
  3. 3. 下载软件
  4. 4. 安装软件
  5. 5. 验证测试
    1. 5.1. 是否成功
    2. 5.2. 错误信息
    3. 5.3. 原因分析
    4. 5.4. 解决方法
  6. 6. **启动nginx **
    1. 6.1. 手动启动
    2. 6.2. 自动启动

适用版本

linux版本:CentOS6 64位,CentOS7 64位,CentOS8 64位,其他linux也适用。

必备前提

在安装nginx前首先要确认系统中安装了gccpcre-develzlib-developenssl-devel

安装命令:

1
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

下载软件

nginx下载地址:https://nginx.org/download/

wget下载“nginx-1.9.9.tar.gz”或者其他更新版本,移动到/home/itbag/下。

安装软件

1
2
3
4
5
6
7
8
9
10
11
12
# 解压
tar -zxvf nginx-1.9.9.tar.gz

# 进入nginx目录
cd nginx-1.9.9

# 配置
./configure --prefix=/home/itbag/install/nginx

# 执行make、make install命令
make
make install

验证测试

是否成功

1
2
# cd到刚才配置的安装目录/usr/loca/nginx/
./sbin/nginx -t

错误信息

1
2
nginx: [alert] could not open error log file: open() "/usr/local/nginx/logs/error.log" failed (2: No such file or directory)
2016/09/13 19:08:56 [emerg] 6996#0: open() "/usr/local/nginx/logs/access.log" failed (2: No such file or directory)

原因分析

nginx/目录下没有logs文件夹

解决方法

1
2
mkdir logs
chmod 700 logs

正常情况的信息输出:

1
2
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 **

手动启动

1
2
cd /usr/local/nginx/sbin
./nginx //启动nginx

在浏览器中输入服务器的ip地址,如:192.168.1.12

很不幸,打不开链接。下面进行原因排查:

image.png
image.png

说明服务器的80端口是打不开的。

因为我使用的linux系统版本是CentOS7,所以可以在服务器中执行如下命令来验证》》

1
firewall-cmd --query-port=80/tcp

显然80端口没有开启。

下面我们开启80端口:

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

--permanent #永久生效,没有此参数重启后失效

刷新浏览器
image.png

自动启动

配置nginx开机自启动

1
vim /etc/rc.d/rc.local

image.png