手动搭建 LNMP环境
LNMP 环境是指在 Linux 系统下,由 Nginx + MySQL/MariaDB + PHP 组成的网站服务器架构。
一、安装Nginx
1.在/etc/yum.repsd.d/下创建nginx.repo文件。
vi /etc/yum.repos.d/nginx.repo
2.按“i”切换至编辑模式,输入以下内容。
[nginx]
name = nginx repo
baseurl = https://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck = 0
enabled = 1
3.按“Esc”,输入“:wq”,保存文件并返回。
4.安装nginx。
yum install -y nginx

5.编辑nginx.conf文件
vim /etc/nginx/nginx.conf
或
nano可能需要安装
nano /etc/nginx/nginx.conf
6.找到 server{...},并将 server 大括号中相应的配置信息替换为如下内容。用于取消对 IPv6 地址的监听,同时配置 Nginx,实现与 PHP 的联动。
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{...} 配置内容。如下图所示:

7.启动nginx
systemctl start nginx
8.设置nginx为开机自启动
systemctl enable nginx
9.在浏览器中访问centos服务器的地址,查看nginx服务是否正常运行

显示这个就说明nginx安装配置成功
二、安装数据库
1.查看系统中是否已安装MariaDB
rpm -qa | grep -i mariadb
显示如下内容,表示有存在MariaDB

为了避免安装版本不同造成冲突,需要将已安装的MariaDB移除
yum -y remove 包名

可以看到移除已安装的MariaDB
2.在/etc/yum.repos.d/下创建MariaDB.repo文件
vi /etc/yum.repos.d/MariaDB.repo
写入以下内容,添加MariaDB软件库
# MariaDB 10.4 CentOS repository list - created 2019-11-05 11:56 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.4/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
3.安装MariaDB
yum -y install MariaDB-client MariaDB-server
4.启动MariaDB服务
systemctl start mariadb
5.设置MariaDB为开机自启动
systemctl enable mariadb
6.验证MariaDB是否安装成功
mysql
显示如下结果表示成功安装

7.退出MariaDB
\q

三、安装配置PHP
1.更新yum中的PHP软件源
- 添加epel源
yum install \
https://repo.ius.io/ius-release-el7.rpm \
https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
- 添加Webtatic源
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

2.安装PHP
yum -y install php70w-devel php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-pdo.x86_64 php70w-mysqlnd php70w-fpm php70w-opcache php70w-pecl-redis php70w-pecl-mongodb
建议直接复制本行命令安装,以免出错
3.查看PHP版本
php -v

4.启动PHP-FPM服务
systemctl start php-fpm
5.设置PHP-FPM服务为开机自启动
systemctl enable php-fpm
验证环境配置
当完成环境配置后,可以验证LNMP环境是否搭建成功
1.创建测试文件
echo "<?php phpinfo(); ?>" >> /usr/share/nginx/html/index.php
2.重启Nginx服务
systemctl restart nginx
3.在浏览器输入centos服务器地址,查看环境是否配置成功
http://IP地址

如果出现上图,说明LNMP环境配置成功
网友评论