如何快速搭建LNMP环境
1.先准备一台虚拟机,并配好yum源
我准备的是一台RedHat 7.4版本的虚拟机,使用的是redhat7的光盘镜像作为yum源
2.下载所需软件包
3.解压lnmp_soft.tar.gz
[root@localhost ~]# tar -xf lnmp_soft.tar.gz
4.进入lnmp_soft目录,执行install_lnmp.sh脚本
[root@localhost lnmp_soft]# ./install_lnmp.sh
5.程序主页面
6.安装nginx,mariadb,php等软件包
6.1 按 “1” 安装nginx
首先会安装依赖包,然后再源码安装nginx。
6.2 按 “2” 安装mariadb
6.3 按 “3” 安装php
7.启动服务
7.1 启动nginx服务
[root@localhost lnmp_soft]# ln -s /usr/local/nginx/sbin/nginx /sbin/ //做一个软连接
[root@localhost lnmp_soft]# nginx //启动服务
7.2 启动mariadb服务
[root@localhost lnmp_soft]# systemctl restart mariadb.service
7.3启动php-fpm服务
[root@localhost lnmp_soft]# systemctl restart php-fpm.service
8.修改nginx.conf主配置文件实现动静分离,修改完配置文件重启服务
8.1 [root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
修改65-71行实现动静分离,先把65-71行的注释打开,然后对比下面进行更改。
-
65 location ~ \.php$ {</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-number">66</span> root html;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-number">67</span> fastcgi_pass <span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">9000</span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-number">68</span> fastcgi_index index.php;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-number">69</span> # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
//把这一行注释
-
70 include fastcgi.conf;
//把后缀改为.conf
-
71 }
8.2 修改完nginx.conf,需要让nginx重新读取配置文件(重启服务)
[root@localhost html]# nginx -s stop
[root@localhost html]# nginx9.部署测试页面
[root@localhost html]# cp /root/lnmp_soft/php_scripts/mysql.php /usr/local/nginx/html/
[root@localhost html]# ls
50x.html index.html mysql.php
10.浏览器测试LNMP环境是否搭建成功
[root@localhost html]# firefox http://192.168.4.100/mysql.php
mysql.php脚本取出了mariadb中的mysql数据库user表中数据。证明LNMP环境搭建成功。