15分钟

任务 2 搭建 LNMP 环境

任务目的

搭建 LNMP 环境。本课程要搭建 WordPress 个人博客,需要先在腾讯云服务器上搭建 LNMP 环境。

任务步骤

1.在 Linux 上安装 Nginx

执行以下命令在/etc/yum.repos.d目录下创建存放 Nginx 镜像源的nginx.repo文件。

vi /etc/yum.repos.d/nginx.repo

i切换至编辑模式,在文件中写入以下内容。

[nginx]
name = nginx repo
baseurl = https://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck = 0
enabled = 1

按下Esc键,输入“:wq”保存对文件的更改并退出。

执行以下命令,安装 Nginx。安装成功结果如下图。

yum install -y nginx
安装Nginx成功

执行以下命令,打开 Nginx 的配置文件。

vim /etc/nginx/nginx.conf

i切换至编辑模式,在文件中的include /etc/nginx/conf.d/*conf;上方写入以下内容。

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;
 }
}

按下Esc键,输入“:wq”保存对文件的更改并退出。

执行以下命令,启动 Nginx。

systemctl start nginx

执行以下命令设置 Nginx 开机自启。

systemctl enable nginx

在本地浏览器中访问 Linux 服务器公网 IP,查看 Nginx 服务是否正常运行。出现如下图页面说明 Nginx 运行正常。

访问Nginx

2.安装 MariaDB

执行以下命令,查看系统中是否已安装 MariaDB。

rpm -qa | grep -i mariadb

如果命令无返回结果,说明未预先安装,则执行下一步。若是已安装则执行以下命令移除已安装的旧版本 MariaDB。

yum -y remove 包名

执行以下命令,在/etc/yum.repos.d/下创建MariaDB.repo文件。

vi /etc/yum.repos.d/MariaDB.repo

i切换至编辑模式,在文件中写入以下内容。

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.4/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

按下Esc键,输入“:wq”保存对文件的更改并退出。

执行以下命令安装 MariaDB。

yum -y install MariaDB-client MariaDB-server
安装MariaDB成功

执行以下命令启动 MariaDB 服务。

systemctl start mariadb

执行以下命令设置 MariaDB 开机自启。

systemctl enable mariadb

3.安装配置 PHP

执行以下命令,更新 yum 中 PHP 的软件源。

rpm -Uvh https://mirrors.cloud.tencent.com/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

执行以下命令安装 PHP。

yum -y install mod_php72w.x86_64 php72w-cli.x86_64 php72w-common.x86_64 php72w-mysqlnd php72w-fpm.x86_64

执行以下命令,启动 PHP-FPM 服务。

systemctl start php-fpm

执行以下命令,设置 PHP-FPM 开机自启。

systemctl enable php-fpm

执行以下命令测试安装是否成功。

echo "<?php phpinfo(); ?>" >> /usr/share/nginx/html/index.php

访问“CVM 公网 IP”+“/index.php”。出现如下图所示页面说明安装 LNMP 环境成功。

测试LNMP环境