建站教程(二):Ubuntu上如何配置Nginx+MySQL+PHP7(LNMP)

在Linux上搭建个人网站,最常用的就是Nginx+MySQL+PHP环境,即LNMP。搭建LNMP的一键脚本很多,不过本文介绍如何不用脚本一步一步自己搭建LNMP环境,这样以后自己的网站遇到什么问题也会清楚如何修改。

 

安装Nginx

Ubutun(本教程是基于Ubuntu 16.04)安装nginx还是很简单的,就两句命令(全部root权限):

apt-get update
apt-get install nginx

安装好后,可以访问http://xx.xx.xx.xx或者是你的域名),如果显示下图所示结果,就说明成功了

 

安装MySQL

还是很简单,一行命令:

apt-get install mysql-server

输入完之后你会被要求输入root的密码,输完之后就安装成功了:

 

安装PHP

安装命令:

apt-get install php-fpm php-mysql

配置Nginx使用PHP

现在我们已经安装了所有需要的软件,目前要做的是修改Nginx的配置文件来使用PHP processor来处理动态内容。

修改Nginx的server block configuration:

vim /etc/nginx/sites-available/default

打开应该是这样的:

我们需要做如下修改:

  1. 添加index.php作为我们的起始页面;
  2. 修改server_name来指向我们的域名或者是公网IP;
  3. 忽略那些以#开头的行。(原文:For the actual PHP processing, we just need to uncomment a segment of the file that handles PHP requests by removing the pound symbols (#) from in front of each line. This will be the location ~\.php$ location block, the included fastcgi-php.conf snippet, and the socket associated with php-fpm
  4. 用同样的方法忽略.htaccess文件。(原文:We will also uncomment the location block dealing with .htaccess files using the same method. Nginx doesn’t process these files. If any of these files happen to find their way into the document root, they should not be served to visitors.

所以,修改完后我们的配置文件应该是这个样子的:

验证配置文件有没有错误:

nginx -t

如果提示OK则说明配置搞定:

重启Nginx:

/etc/init.d/nginx restart

测试PHP与Nginx有没有集成成功

添加一个info.php:(这里的 /var/www/html/ 对应配置文件中root的路径)

vim /var/www/html/info.php

内容为:

<?php 
phpinfo();

访问http://xx.xx.xx.xx/info.php(或者是你的域名),如下图所示则说明全部安装成功~

 

转载请注明:flyzy小站 » 在Ubuntu上配置Nginx+MySQL+PHP7

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值