在Ubuntu 16.04 LTS上使用PHP 7和MySQL 5.7(LEMP)安装Nginx

在Ubuntu 16.04 LTS上使用PHP 7和MySQL 5.7(LEMP)安装Nginx

Nginx (发音为“引擎x”)是一个免费的,开放源码的高性能HTTP服务器。 Nginx以其稳定性,丰富的功能集,简单的配置和低资源消耗而闻名。 本教程将展示如何在支持PHP 7(通过PHP-FPM )和MySQL 5.7支持(LEMP = L inux + nginx(发音为“ e ngine x”)+ M ySQL + P HP的Ubuntu 16.04服务器上安装Nginx。

1初步说明

在本教程中,我使用IP地址为192.168.1.100的hostname server1.example.com 。 这些设置可能会有所不同,因此您必须在适当的情况下更换它们。

我使用root权限运行本教程中的所有步骤,因此请确保以root用户身份登录:

sudo -s

2安装MySQL 5.7

为了安装MySQL,我们运行:

apt-get -y install mysql-server mysql-client

您将被要求为MySQL root用户提供密码 - 此密码对用户root @ localhost以及root@server1.example.com有效 ,因此我们不必在以后手动指定MySQL根密码:

MySQL“root”用户的新密码: < - yourrootsqlpassword
重复MySQL“root”用户的密码: < - yourrootsqlpassword

要保护数据库服务器并删除匿名用户和测试数据库,请运行mysql_secure_installation命令。

mysql_secure_installation

你会被问到这些问题:

root@server1:~# mysql_secure_installation

Securing the MySQL server deployment.

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现在已经安全了

3安装Nginx

如果您已经安装了Apache2,请先使用这些命令删除它,然后再安装nginx:

service apache2 stop
update-rc.d -f apache2 remove
apt-get remove apache2

Nginx可用作Ubuntu 16.04的一个包,我们可以安装它。

apt-get -y install nginx

之后启动nginx:

service nginx start

在浏览器中输入您的Web服务器的IP地址或主机名(例如http://192.168.1.100 ),您将看到以下页面:

Ubuntu Nginx默认页面。

Ubuntu 16.04上的默认nginx文档根目录是/ var / www / html

4安装PHP 7

我们可以通过PHP-FPM使PHP工作在nginx(PHP-FPM(FastCGI Process Manager)是一种替代的PHP FastCGI实现,其中一些额外的功能对任何大小的网站尤其是繁忙的站点有用),我们安装如下:

apt-get -y install php7.0-fpm

PHP-FPM是在套接字/run/php/php7.0-fpm.sock上运行FastCGI服务器的守护进程(具有init脚本php7.0-fpm )。

5配置nginx

nginx配置在我们现在打开的/etc/nginx/nginx.conf中:

nano /etc/nginx/nginx.conf

配置很容易理解(您可以在这里了解更多信息: http : //wiki.nginx.org/NginxFullExample ,这里: http : //wiki.nginx.org/NginxFullExample2

首先(这是可选的)将keepalive_timeout调整到一个合理的值:

[...]
    keepalive_timeout   2;
[...]

虚拟主机在server {}容器中定义。 默认的vhost在文件/ etc / nginx / sites-available / default中定义 - 让我们修改如下:

nano /etc/nginx/sites-available/default

[...]
server {
listen 80 default_server;
listen [::]:80 default_server;

# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;

root /var/www/html;

# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

server_name _;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;

# With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
} [...]

服务器名称 _; 使它成为默认的catchall vhost(当然,您也可以像www.example.com一样指定一个主机名)。

root / var / www / html; 意味着文档根目录是/ var / www / html

PHP的重要部分是位置〜\ .php $ {}节。 取消注释以启用它。

现在保存文件并重新加载nginx:

service nginx reload

接下来打开/etc/php/7.0/fpm/php.ini ...

nano /etc/php/7.0/fpm/php.ini

...并设置cgi.fix_pathinfo = 0

[...]
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0
[...]

重新加载PHP-FPM:

service php7.0-fpm reload

现在在文件根/ var / www / html中创建以下PHP文件:

nano /var/www/html/info.php

<?php
phpinfo();
?>

现在我们在浏览器中调用该文件(例如http://192.168.1.100/info.php ):

Ubuntu的PHP信息与Nginx。

如您所见,PHP 7正在工作,它正在通过FPM / FastCGI工作,如Server API行所示。 如果您进一步向下滚动,您将看到在PHP中已启用的所有模块。 MySQL没有列出,这意味着我们还没有在PHP中支持MySQL。

6在PHP 7中获取MySQL支持

要在PHP中获得MySQL支持,我们可以安装php7.0-mysql软件包。 安装一些其他PHP模块是一个好主意,也可能需要它们用于应用程序。 您可以搜索可用的PHP模块,如下所示:

apt-cache search php7.0

选择您需要的并安装它们:

apt-get -y install php7.0-mysql php7.0-curl php7.0-gd php7.0-intl php-pear php-imagick php7.0-imap php7.0-mcrypt php-memcache  php7.0-pspell php7.0-recode php7.0-sqlite3 php7.0-tidy php7.0-xmlrpc php7.0-xsl php7.0-mbstring php-gettext

APCu是PHP 7附带的PHP Opcache模块的扩展,它为支持APC缓存的软件(例如Wordpress缓存插件)添加了一些兼容性功能。

APCu可以安装如下:

apt-get -y install php-apcu

现在重新加载PHP-FPM:

service php7.0-fpm reload

现在在您的浏览器中重新加载http://192.168.1.100/info.php并再次向下滚动到模块部分。 您现在应该会在那里找到很多新的模块,包括MySQL模块:

PHP模块已安装。

7使PHP-FPM使用TCP连接

默认情况下,PHP-FPM正在监听套接字/var/run/php/php7.0-fpm.sock 。 也可以使PHP-FPM使用TCP连接。 为此,请打开/etc/php/7.0/fpm/pool.d/www.conf ...

nano /etc/php/7.0/fpm/pool.d/www.conf

...并让听力线看起来如下:

[...]
;listen = /var/run/php5-fpm.sock
listen = 127.0.0.1:9000
[...]

这将使PHP-FPM在IP 127.0.0.1localhost )上的端口9000监听 。 确保使用系统上未使用的端口。

然后重新加载PHP-FPM:

php7.0-fpm reload

接下来,通过您的nginx配置和所有的vhosts,并更改行fastcgi_pass unix:/ var / run / php /php7.0-fpm.soc k;fastcgi_pass 127.0.0.1:9000; ,例如:

nano /etc/nginx/sites-available/default

[...]
        location ~ \.php$ {
include snippets/fastcgi-php.conf;

# With php7.0-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
} [...]

最后,重新加载nginx:

service nginx reload

而已。 安装了Nginx LEMP服务器。