Contents
  1. 1. 开发环境
  2. 2. Nginx
    1. 2.1. 安装&启动
    2. 2.2. 配置
  3. 3. PHP7
    1. 3.1. 安装
    2. 3.2. 配置
    3. 3.3. FastCGI
  4. 4. MySQL8
    1. 4.1. 安装
    2. 4.2. 修改 root密码
    3. 4.3. 配置
  5. 5. 参考&致谢

在此前的文章中,详细记录了在 RHEL6/CentOS6上搭建 LAMP 的过程,随着时间的流逝,操作系统已经到了7.x版本,PHP 版本已经发展到了7.x, MySQL 版本也进化到了8.x,所以我们今天就来记录一下,在Linux7版本上搭建 LNMP环境 的全过程。

开发环境

按照惯例,先列一下我们最终的操作系统和各软件版本

  • RHEL/CentOS 7
  • Nginx 1.12
  • PHP 7.2
  • MySQL 8.0

注:
基本所有的安装都是通过 yum 来完成,所以需要确保你的服务器能访问到公网。
另外确保你的 yum 安装了 epel 源。考虑到国内 GFW 的威力,也可以安装其他靠谱的源。

1
$ yum install epel-release

Nginx

安装&启动

nginx 的安装非常简单

1
$ yum -y install nginx

安装成功后,可以通过下面的命令启动、查看及重启 nginx

1
2
3
4
$ systemctl start nginx         # 启动
$ systemctl status nginx # 查看状态
$ systemctl enable nginx # 激活开机自启动
$ systemctl restart nginx # 重启

配置

默认的 nginx 会在/etc/nginx/default.conf中设置默认的配置信息,不过考虑到之后可以搭建多套 web 服务,我们就把配置文件统一整理一下。
/etc/nginx/conf.d/目录下其实就是放置我们自定义的 web 服务配置文件的,每次 nginx 启动后,会加载该目录下的配置。我们可以在/etc/nginx/default.conf 文件中找到下面的代码:

1
2
3
4
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

我们新建一个默认的default.conf文件放置到/etc/nginx/conf.d/目录中,同时把原本位于/etc/nginx/default.conf中的 server部分信息剪切过来(如果不是剪切,你的自定义的配置会被默认的配置所覆盖)。
文件中写入下面的内容:

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
server {
listen 80; # 监听端口
server_name localhost;
root /usr/share/nginx/html; # web 服务根目录

#access_log /var/log/nginx/host.access.log main;

location / {
index index.php index.html index.htm; # 记得写入 index.php
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ { # 同样为了 PHP 而配置
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}
}

因为有些内容是为了 PHP能正确解析而加入的,关于 PHP 的内容,我们在后面章节会详细讨论。
如果我们需要在其他端口开启新的 web 服务,记得在/etc/nginx/conf.d目录下新建 conf 文件并仿照默认的配置文件设置好端口和根目录就好了。
配置完成后记得重启 nginx 服务。
更多关于 nginx 负载均衡、反向代理等内容就不在这里做过多的介绍,回头有需要可以单独开文章做进一步详细介绍。
关于开放相关端口,在另一篇文章【LINUX】Linux7中的防火墙 - Firewall中做了更详细的介绍,我们可以通过 firewall 打开默认的80端口。

PHP7

安装

PHP7 的安装同样需要两个额外的源,我们通过下面的命令安装:

1
2
$ yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
$ yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

接下来,我们需要安装yum-util工具来更好地管理我们的 yum 源以及安装包

1
$ yum install yum-utils

安装好了yum-utils后,我们可以配置想要安装的 PHP 版本,选择你想要安装的 PHP 版本

1
2
3
$ yum-config-manager --enable remi-php70   [Install PHP 7.0]
$ yum-config-manager --enable remi-php71 [Install PHP 7.1]
$ yum-config-manager --enable remi-php72 [Install PHP 7.2]

通过下面的命令安装 PHP 相关的组件

1
$ yum install -y php php-mcrypt php-cli php-gd php-curl php-mysql php-ldap php-zip php-fileinfo php-fpm

配置

同 nginx 的配置类似,PHP 也有自己默认的配置文件: /etc/php.ini,不过这里我们可以不用修改,如下的修改只是我针对项目做出的个性化的设置。

我们分别修改一下几个地方:

1
2
3
4
5
6
session.cookie_httponly = 1                     # 让我们 PHP 的 cookie 变成 http-only 的,更加安全

session.gc_probability = 1
session.gc_divisor = 100 # 这两个 gc 参数一并使用,表示每次访问有1/100的概率清除过期的 session

session.gc_maxlifetime = 1440 # 默认的 session 过期时间(24)分钟,但是session 是否被清除,看上面的概率

关于这几项配置,我们可以从它们各自的注释中得到更全面的解释。
配置修改之后,需要重启 nginx 服务使配置得以生效。

FastCGI

还记得我们在之前配置 Nginx 时提前写入的关于 PHP 脚本的 FastCGI 部分么,我们在这里详细介绍一下:
Fastcgi是一种进程管理器,管理cgi进程。市面上有多种实现了Fastcgi功能的进程管理器,php-fpm就是其中的一种。
再提一点,php-fpm作为一种Fast-cgi进程管理服务,会监听端口,一般默认监听9000端口,并且是监听本机,也就是只接收来自本机的端口请求,所以我们在 Nginx 的配置文件中能看到诸如下面的配置部分:

1
2
3
4
5
6
7
8
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

MySQL8

安装

同样先安装特定的 yum 源

1
$ yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm

特别提醒: 还是因为 GFW 的原因,下载安装过程可能耗时会很久,如果网速一般,可以用 nohup 方式运行在后台。

1
$ yum install mysql-community-server

安装完成后,开启 mysql 并设置开机自动启动

1
2
$ systemctl start mysqld
$ systemctl enable mysqld

修改 root密码

我们可以从 mysql 的安装 log 中拿到 root 用户的初始密码:

1
$ grep 'A temporary password is generated for root@localhost' /var/log/mysqld.log |tail -1

我们会得到如下的输出:

1
2015-11-20T21:11:44.229891Z 1 [Note] A temporary password is generated for root@localhost: -et)QoL4MLid

也就是我们的 root 初始默认密码是-et)QoL4MLid
当我们用 root 用户首次登录时,系统会提示我们修改默认密码。

配置

配置文件在/etc/my.cnf中,可以按照个人需求,分别修改不同的配置项:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[mysqld]                                    # mysqld 部分
bind-address=127.0.0.1 # 只能从本机访问 mysql

character_set_server=utf8mb4 # 修改默认字符集
init_connect='SET NAMES utf8mb4'

event_scheduler=on # 默认开启 event

innodb_buffer_pool_size = 128M # 修改 innodb 的 buffer 大小

default-authentication-plugin=mysql_native_password # 提高老版本的 mysql 连接稳定性

max_connections=1000 # 最大并发连接数

local-infile=1 # 允许导入 local file 中的数据

[client]
default_character_set=utf8mb4 # 默认字符集
local-infile=1 # 允许导入 local file 中的数据

配置文件修改后,记得systemctl restart mysqld来重启 mysql 服务。

到这里,我们整的 LNMP 环境就搭建完成了。

参考&致谢

Contents
  1. 1. 开发环境
  2. 2. Nginx
    1. 2.1. 安装&启动
    2. 2.2. 配置
  3. 3. PHP7
    1. 3.1. 安装
    2. 3.2. 配置
    3. 3.3. FastCGI
  4. 4. MySQL8
    1. 4.1. 安装
    2. 4.2. 修改 root密码
    3. 4.3. 配置
  5. 5. 参考&致谢