Centos-7.2部署LNMP环境
更新时间:2024-12-17
本文介绍了如何使用centos 7.2系统搭建LNMP环境。centos7.2搭建LNMP具体步骤如下:
1. 配置防火墙
CentOS 7.0以上的系统默认使用的是firewall作为防火墙。
关闭firewall:
Plain Text
1systemctl stop firewalld.service #停止firewall
2systemctl disable firewalld.service #禁止firewall开机启动
2. 安装以及配置nginx
(1)可直接使用服务器内自带的yum源进行安装;
yum install -y nginx
(2)修改nginx默认配置文件;
Plain Text
1cd /etc/nginx/ #nginx的默认配置目录
2mv nginx.conf nginx.conf.swf #将通过yum安装自动生成的配置文件换名,不再使用。
3mv nginx.conf.default nginx.conf #将default文件修改为加载配置文件
(3)启动nginx,并访问。
Plain Text
1systemctl start nginx.service #启动nginx
2systemctl stop nginx.service #停止
3systemctl restart nginx.service #重启
4systemctl enable nginx.service
5此时使用公网IP访问,即可打开nginx的默认欢迎页面。
3. 安装php环境以及依赖扩展,并开启php
(1)安装环境;
Plain Text
1yum 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
1systemctl start php-fpm.service #启动php-fpm
2systemctl enable php-fpm.service #设置开机启动
(3)修改配置文件,使nginx兼容php;
vim /etc/nginx/nginx.conf
在location内添加上index.php,将php识别之前的#注释去掉,并且改一下fastcgi_param。
Plain Text
1location / {
2root html;
3index index.php index.html index.htm;
4}
5
6error_page 500 502 503 504 /50x.html;
7location = /50x.html {
8root html;
9}
10
11location ~ \.php$ {
12root html;
13fastcgi_pass 127.0.0.1:9000;
14fastcgi_index index.php;
15fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
16include fastcgi_params;
17}
(4)重启nginx和php-fpm;
Plain Text
1systemctl restart php-fpm.service
2systemctl restart nginx.service
(5)访问phpinfo验证;
进入nginx默认根目录 cd /usr/share/nginx/html/
vim index.php
Plain Text
1<?php
2echo phpinfo();
3?>
此时访问公网IP/index.php,可看到php的探针页面:
4. 安装mysql,并连接php进行验证
(1)centos7以上的系统,默认数据库为MariaDB,需要下载mysql源进行安装。
Plain Text
1wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
2rpm -ivh mysql-community-release-el7-5.noarch.rpm
3yum install -y mysql-community-server
成功安装之后重启mysql服务:
systemctl start mysqld
(2)安装后的mysql数据库是没有密码的,可进入数据库后授权,设置密码。
Plain Text
1mysql -u root
2mysql> use mysql;
3mysql> update user set password=PASSWORD("这里输入root用户密码") where user='root';
4mysql> flush privileges;
5mysql> exit
(3)编写php测试mysql是否可连接的代码,并访问。
vim /usr/share/nginx/html/test.php
Plain Text
1<?php
2$link=mysql_connect("localhost","root","刚才所设置的数据库密码");
3if(!$link) echo "FAILD!error";
4else echo "OK!You succeeded.";
5?>
保存后,此时访问IP/test.php
,可看到
至此,基于centos7.2系统所安装的LNMP环境已经全部搭建完毕。