您的位置:首页 > 运维架构 > Nginx

在 Ubuntu 16.04上安装 Nginx, MySQL, PHP (LEMP)

2016-07-06 09:03 1271 查看


原文链接https://www.atlantic.net/community/howto/install-lemp-stack-on-ubuntu-16-04/


完全卸载apache2

sudo apt update

sudo apt upgrade

sudo apt remove apache2*

sudo apt autoremove


安装Nginx

sudo apt install nginx

sudo service nginx start

测试Nginx,首先使用如下命令获取本机IP地址

ifconfig



使用浏览器访问本地IP。
http://10.102.7.193
返回如下页面,则安装Nginx成功。





安装MySQL

sudo apt install mysql-server

安装过程中需要输入两次密码。









安装MySQL安全组件

sudo mysql_secure_installation

安装过程中首先需要输入MYSQL的root密码.

当询问“Change the root password?”时,输入“N”

剩下的问题可以敲击回车键,选择默认选项。


安装PHP

sudo apt install php php-fpm php7.0-mysql

为使Nginx支持PHP,需要修改Nginx的配置文件,首先备份原始配置文件。

sudo mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.old

编辑/etc/nginx/sites-available文件,作为Nginx的配置文件。

sudo vim /etc/nginx/sites-available/default

向/etc/nginx/sites-available输入以下内容

server {
listen       80;
server_name  your_site_name.com;
root /usr/share/nginx/html;
index index.php index.html;

location / {
try_files $uri $uri/ =404;
}

error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;

location = /50x.html {
root /var/www/html;
}

location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}


修改完Nginx配置,需要重启Nginx

sudo service nginx restart

在/usr/share/nginx/html/目录创建info.php文件,测试PHP

sudo nano /usr/share/nginx/html/info.php

输入以下内容

<?php

phpinfo();

?>

在浏览器打开http://本地IP/info.php

例如http://10.102.7.193/info.php.

返回如下内容,则Nginx能够支持PHP





最后,可以选择删除创建的info.php文件,避免被攻击者利用。

sudo rm /usr/share/nginx/html/info.php
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: