搭建lnmp环境是每一位PHP开发者必会的技能,本文将带领大家一起完整的搭建一次lnmp环境。
1、我们买了云服务器之后可是安装公共镜像,一般常用的是centos,我们选择版本较为新的centos7.2作为我们的操作系统。
2、安装完系统后我们开始安装nginx,先下载对应的nginx包,建立yum库,安装nginx
(1)wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm (一般自带wget命令,如果没用 yum install wget安装即可)。
(2)建立nginx的yum库
rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
(3)安装nginx yum install nginx (安装完成后,可是使用whereis nginx查看安装位置)
(4) 启动nginx systemctl start nginx.service
3、MYSQL安装,先下载mysql的repo源包,建立mysql yum 库,安装mysql ,修改mysql密码
(1)wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
(2)rpm -ivh mysql-community-release-el7-5.noarch.rpm
(3)sudo yum install mysql-server
(4)sudo chown -R root:root /var/lib/mysql
(5)systemctl restart mysql.service
(6)mysql -u root
mysql > use mysql;
mysql > update user set password=password('123456') where user='root';
mysql > exit;
4、安装PHP,由于系统自带PHP ,首先删除旧版本的PHP。建立yum库,安装PHP以及主要的库还有PHP-fpm
(1)查看当前安装的php版本( yum list installed | grep php),如果存在php安装包先删除之前版本 用yum remove 移除 php相关的包
(2) rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
(3)yum install php70w php70w-mysql.x8664 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 yum install php70w-fpm
5、配置nginx、php-fpm
(1)、nginx配置文件/etc/nginx/conf.d/default.conf
location / {
root /home/wwwroot;
index index.php index.html index.htm;
}
location ~ \.php|\.html$ {
root /home/wwwroot/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
(2) 、配置php-fpm配置文件 /etc/php-fpm.d/www.conf
将
user =nginx
group=nginx
(3)、重启nginx,启动php-fpm
systemctl restart nginx.service (设置开启启动 sudo systemctl enable nginx.service)
systemctl start php-fpm.service
6、在/home/wwwroot/目录下新建index.html index.php两个文件 分别写入PHP语言
<?php phpinfo();?>
在浏览器上输入访问地址 111.111.111.111/index.php 跟 111.111.111.111/index.html(假设是111.111.111.111)