手动搭建LNMP

实用性-阐述规则

This article was last updated on <span id="expire-date"></span> days ago, the information described in the article may be outdated.

安装 Nginx

  1. 执行以下命令,在 /etc/yum.repos.d/ 下创建 nginx.repo 文件。
1
vi /etc/yum.repos.d/nginx.repo
  1. i 切换至编辑模式,写入以下内容。
1
2
3
4
5
[nginx] 
name = nginx repo
baseurl = https://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck = 0
enabled = 1
  1. Esc,输入 :wq,保存文件并返回。

  2. 执行以下命令,安装 nginx

1
yum install -y nginx
  1. 执行以下命令,打开 nginx.conf 文件。
1
vim /etc/nginx/nginx.conf
  1. i 切换至编辑模式,编辑 nginx.conf 文件。

  2. 找到 server{...},并将 server 大括号中相应的配置信息替换为如下内容。用于取消对 IPv6 地址的监听,同时配置 Nginx,实现与 PHP 的联动。

    说明:可使用 Ctrl+F 向下翻页、Ctrl+B向上翻页查看文件。

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
server {
listen 80;
root /usr/share/nginx/html;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
#
location / {
index index.php index.html index.htm;
}
#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$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

nginx.conf 文件中未找到 server{...} ,请在 include /etc/nginx/conf.d/*conf; 上方添加以上的 server{...} 配置内容。如下图所示:

img

  1. Esc,输入 :wq,保存文件并返回。

  2. 执行以下命令启动 Nginx。

1
systemctl start nginx
  1. 执行以下命令,设置 Nginx 为开机自启动。
1
systemctl enable nginx 
  1. 在本地浏览器中访问以下地址,查看 Nginx 服务是否正常运行。
1
http://云服务器实例的公网 IP

显示如下,则说明 Nginx 安装配置成功。
img

安装数据库

  1. 执行以下命令,查看系统中是否已安装 MariaDB。
1
rpm -qa | grep -i mariadb
  • 返回结果类似如下内容,则表示已存在 MariaDB。

    img

    为避免安装版本不同造成冲突,请执行以下命令移除已安装的 MariaDB。

1
yum -y remove 包名
  • 若返回结果为空,则说明未预先安装,则执行下一步。
  1. 执行以下命令,在 /etc/yum.repos.d/ 下创建 MariaDB.repo 文件。
1
vi /etc/yum.repos.d/MariaDB.repo
  1. i 切换至编辑模式,写入以下内容,添加 MariaDB 软件库。

    说明:不同操作系统的 MariaDB 软件库不同,可前往 MariaDB 官网 获取其他版本操作系统的 MariaDB 软件库安装信息。

1
2
3
4
5
6
7
8
[mariadb-main]
name = MariaDB Server
#baseurl = https://downloads.mariadb.com/MariaDB/mariadb-10.4/yum/rhel/$releasever/$basearch
baseurl = https://mirrors.ustc.edu.cn/mariadb/yum/10.4/centos7-amd64
#gpgkey = file:///etc/pki/rpm-gpg/MariaDB-Server-GPG-KEY
gpgkey = https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck = 1
enabled = 1

这里使用了国内的镜像,下载速度快得多

  1. Esc,输入 :wq,保存文件并返回。

  2. 执行以下命令,安装 MariaDB。

1
yum -y install MariaDB-client MariaDB-server
  1. 执行以下命令,启动 MariaDB 服务。
1
systemctl start mariadb
  1. 执行以下命令,设置 MariaDB 为开机自启动。
1
systemctl enable mariadb
  1. 执行以下命令,验证 MariaDB 是否安装成功。
1
mysql

显示结果如下,则成功安装。

image-20201129170024369

  1. 执行以下命令,退出 MariaDB。
1
\q

安装配置PHP

  1. 依次执行以下命令,更新 yum 中 PHP 的软件源。
1
rpm -Uvh https://mirrors.cloud.tencent.com/epel/epel-release-latest-7.noarch.rpm
1
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
  1. 执行以下命令,安装 PHP 7.2 所需要的包。
1
yum -y install mod_php72w.x86_64 php72w-cli.x86_64 php72w-common.x86_64 php72w-mysqlnd php72w-fpm.x86_64
  1. 执行以下命令,启动 PHP-FPM 服务。
1
systemctl start php-fpm
  1. 执行以下命令,设置 PHP-FPM 服务为开机自启动。
1
systemctl enable php-fpm

验证环境配置

当您完成环境配置后,可以通过以下验证 LNMP 环境是否搭建成功。

  1. 执行以下命令,创建测试文件。
1
echo "<?php phpinfo(); ?>" >> /usr/share/nginx/html/index.php
  1. 执行以下命令,重启 Nginx 服务。
1
systemctl restart nginx
  1. 在本地浏览器中访问如下地址,查看环境配置是否成功。
1
http://云服务器实例的公网 IP

参考资料: