centos7.6从零开始编译nginx+mysql+php(二) 安装Nginx

其实这篇文章编辑了两次,第一次是采用yum安装的,后面想了想还是得用编译安装,这样能帮自己熟悉Linux命令和未来自己搭建环境。

准备工作

首先我们新建一个software目录,存放软件的安装包

1
2
[root@localhost ~]# mkdir /software
[root@localhost ~]# cd /software/

然后下载 Nginx 安装包

移动线路推荐 http://mirrors.sohu.com/nginx/nginx-1.14.0.tar.gz
电信线路推荐 http://nginx.org/download/nginx-1.14.2.tar.gz

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 如果运行报错:-bash: wget: 未找到命令
# 那么说明你还没有安装wget命令,需要运行yum install wget先安装这个命令
[root@localhost software]# wget http://nginx.org/download/nginx-1.14.2.tar.gz

# 关闭系统防火墙 此文章是内网测试所用所以直接关闭防火墙也无所谓。
# 外网环境或者正式环境请根据对应的需求开放指定端口
[root@localhost software]# systemctl stop firewalld.service
[root@localhost software]# systemctl disable firewalld.service

# 关闭SElinux
[root@localhost software]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@localhost software]# setenforce 0

# 创建www账户 用来启动nginx
[root@localhost software]# useradd www -s /sbin/nologin
# 安装运行Nginx所依赖的包
[root@localhost software]# yum -y install pcre pcre-devel zlib zlib-devel gcc-c++ gcc openssl*

# 解压Nginx源码包
[root@localhost software]# tar zxvf nginx-1.14.0.tar.gz

# 进入解压后的目录,对Nginx进行配置
[root@localhost software]# cd nginx-1.14.0/
[root@localhost nginx-1.14.0]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre

# 编译和安装
[root@localhost nginx-1.14.0]# make && make install

# 启动Nginx
[root@localhost nginx-1.14.0]# /usr/local/nginx/sbin/nginx

浏览器访问测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 关闭Nginx进程
# 如果报错 -bash: killall: 未找到命令,请运行 yum install psmisc 安装kill、killall命令。
[root@localhost nginx-1.14.0]# killall nginx

# 把nginx命令作为软连接
[root@localhost nginx-1.14.0]# ln -s /usr/local/nginx/sbin/nginx /sbin/nginx

# 编写nginx启动脚本
[root@localhost nginx-1.14.0]# cat >> /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=//usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

# 修改完systemctl服务,需要重新加载下daemon
[root@localhost nginx-1.14.0]# systemctl daemon-reload

# 用systemctl启动Nginx服务,并查看状态
[root@localhost nginx-1.14.0]# systemctl start nginx
[root@localhost nginx-1.14.0]# systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since 日 2019-05-05 23:05:38 CST; 5s ago
Docs: http://nginx.org/en/docs/
Process: 10466 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 10464 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Main PID: 10469 (nginx)
CGroup: /system.slice/nginx.service
├─10469 nginx: master process /usr/sbin/nginx
└─10470 nginx: worker process

5月 05 23:05:38 centos7 systemd[1]: Starting nginx - high performance web .....
5月 05 23:05:38 centos7 nginx[10464]: nginx: the configuration file /usr/l...ok
5月 05 23:05:38 centos7 nginx[10464]: nginx: configuration file /usr/local...ul
5月 05 23:05:38 centos7 systemd[1]: Failed to read PID from file /usr/loca...nt
5月 05 23:05:38 centos7 systemd[1]: Started nginx - high performance web s...r.
Hint: Some lines were ellipsized, use -l to show in full.

# 设置nginx开机启动
[root@localhost nginx-1.14.0]# systemctl enable nginx

到此Nginx安装成功。

五一期间我大概安装了6-7次环境。nginx和php基本上都没什么问题。

但是mysql出现了一个问题我找了好几天都找不到,几度打算放弃去装一键包。后来经人无意之间的提醒找到了问题所在。

下一篇更新mysql。

参考文档

CentOS 7下编译安装Nginx+MySQL+PHP


评论区