Nginx (读”engine x”) 是一款免费、开源的高性能 HTTP 服务。Nginx 稳定、丰富的功能集、配置简单、资源消耗低。本教程介绍了如何通过PHP7支持(通过PHP-FPM)和MySQL5.7支持(LEMP= LINUX + nginx(发音为“engine x”)+ MySQL+ PHP)在Ubuntu 16.04服务器上安装Nginx服务器。
1 初步说明
在本教程中,我使用的IP 地址192.168.1.100,主机名server1.example.com。这些设置可能与你的不同,所以你不得不在适当情况下更换他们。
我运行的所有步骤在本教程中使用root权限,所以一定要确保你以root身份登录:
2 安装 MySQL 5.7安装 MySQL 运行命令:
apt-get -y install mysql-server mysql-client
你会被要求提供MySQL的root用户密码 :
New password for the MySQL “root” user: <– yourrootsqlpassword
Repeat password for the MySQL “root” user: <– yourrootsqlpassword
为了确保数据库服务器,并删除匿名用户和测试数据库,运行mysql_secure_installation命令。
mysql_secure_installation
你会问这些问题:
root@server1:~# mysql_secure_installation
保护MySQL服务器部署。
Enter password for user root: <– Enter the MySQL root password
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: <– Press y if you want this function or press Enter otherwise.
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : <– Press enter
… skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : <– y
Success.
Normally, root should only be allowed to connect from
‘localhost’. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : <– y
Success.
By default, MySQL comes with a database named ‘test’ that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : <– y
– Dropping test database…
Success.
– Removing privileges on test database…
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : <– y
Success.
All done!
MySQL is secured now.
3 安装 Nginx在你已经安装了Apache2的话,那么使用这些命令先删除再安装nginx:
service apache2 stop
update-rc.d -f apache2 remove
apt-get remove apache2
Ubuntu16.04有Nginx安装包,我们可以安装。
apt-get -y install nginx
Start nginx afterwards:
service nginx start
输入您的Web服务器的IP地址或主机名到浏览器(例如),你应该看到如下页面:
在Ubuntu16.04的默认nginx的文档根目录为/var/www/html
4 安装 PHP 7我们可以通过使nginx的PHP工作PHP-FPM(PHP-FPM(FastCGI进程管理器)是为任何规模的网站,尤其是繁忙的网站有用的一些附加功能的替代PHP的FastCGI实现),我们安装如下:
apt-get -y install php7.0-fpm
5 配置 nginx打开配置文件 /etc/nginx/nginx.conf:
nano /etc/nginx/nginx.conf
首先(这是可选)调整keepalive_timeout到一个合理的值:
[...] keepalive_timeout 2; [...]虚拟主机服务器{}容器定义。默认的虚拟主机是在文件中定义的/etc/nginx/sites-available/default – 让我们来修改它,如下所示: