Centos-7.2部署LNMP环境
本文介绍了如何使用centos 7.2系统搭建LNMP环境。centos7.2搭建LNMP具体步骤如下:
1. 配置防火墙
CentOS 7.0 及以上系统默认使用 firewalld 作为防火墙。
关闭 firewall:
Plain Text 复制
11systemctl stop firewalld.service #停止firewall
22systemctl disable firewalld.service #禁止firewall开机启动
2. 安装以及配置 Nginx
(1)可直接使用服务器内自带的yum源进行安装;
yum install -y nginx
(2)修改nginx默认配置文件;
Plain Text 复制
11cd /etc/nginx/ #nginx的默认配置目录
22mv nginx.conf nginx.conf.swf #将通过yum安装自动生成的配置文件换名,不再使用。
33mv nginx.conf.default nginx.conf #将default文件修改为加载配置文件
(3)启动nginx,并访问。
Plain Text 复制
11systemctl start nginx.service #启动nginx
22systemctl stop nginx.service #停止
33systemctl restart nginx.service #重启
44systemctl enable nginx.service
55此时使用公网IP访问,即可打开nginx的默认欢迎页面。

3. 安装 PHP 环境以及依赖扩展,并开启 PHP
(1)安装环境;
Plain Text 复制
11yum install -y php-fpm php php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash
(2)启动php-fpm;
Plain Text 复制
11systemctl start php-fpm.service #启动php-fpm
22systemctl enable php-fpm.service #设置开机启动
(3)修改配置文件,使nginx兼容php;
vim /etc/nginx/nginx.conf
在location内添加上index.php,将php识别之前的#注释去掉,并且改一下fastcgi_param。
Plain Text 复制
11location / {
22root html;
33index index.php index.html index.htm;
44}
55
66error_page 500 502 503 504 /50x.html;
77location = /50x.html {
88root html;
99}
1010
1111location ~ \.php$ {
1212root html;
1313fastcgi_pass 127.0.0.1:9000;
1414fastcgi_index index.php;
1515fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
1616include fastcgi_params;
1717}

(4)重启nginx和php-fpm;
Plain Text 复制
11systemctl restart php-fpm.service
22systemctl restart nginx.service
(5)访问phpinfo验证;
进入nginx默认根目录 cd /usr/share/nginx/html/
vim index.php
Plain Text 复制
11<?php
22echo phpinfo();
33?>
此时访问公网IP/index.php,可看到php的探针页面:

4. 安装 MySQL,并连接 PHP 进行验证
(1)centos7以上的系统,默认数据库为MariaDB,需要下载mysql源进行安装。
Plain Text 复制
11wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
22rpm -ivh mysql-community-release-el7-5.noarch.rpm
33yum install -y mysql-community-server
成功安装之后重启mysql服务:
systemctl start mysqld
(2)安装后的mysql数据库是没有密码的,可进入数据库后授权,设置密码。
Plain Text 复制
11mysql -u root
22mysql> use mysql;
33mysql> update user set password=PASSWORD("这里输入root用户密码") where user='root';
44mysql> flush privileges;
55mysql> exit
(3)编写php测试mysql是否可连接的代码,并访问。
vim /usr/share/nginx/html/test.php
Plain Text 复制
11<?php
22$link=mysql_connect("localhost","root","刚才所设置的数据库密码");
33if(!$link) echo "FAILD!error";
44else echo "OK!You succeeded.";
55?>
保存后,此时访问 IP/test.php,可看到

至此,基于 CentOS 7.2 系统安装的 LNMP 环境已经全部搭建完毕。
评价此篇文章
