在 Ubuntu 16.04 LTS 上安装带有 PHP 7 和 MySQL 5.7 (LEMP) 的 Nginx在 Ubuntu 16.04 LTS 上安装带有 PHP 7 和 MySQL 5.7 (LEMP) 的 Nginx在 Ubuntu 16.04 LTS 上安装带有 PHP 7 和 MySQL 5.7 (LEMP) 的 Nginx在 Ubuntu 16.04 LTS 上安装带有 PHP 7 和 MySQL 5.7 (LEMP) 的 Nginx
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2025年2月28日
类别
  • 未分类
标签

在 Ubuntu 16.04 LTS 上安装带有 PHP 7 和 MySQL 5.7 (LEMP) 的 Nginx

本教程适用于这些操作系统版本

  • Ubuntu 14.04 LTS (Trusty Tahr)

在此页

  1. 1 条初步说明
  2. 2 安装 MySQL 5.7
  3. 3 安装 Nginx
  4. 4 安装 PHP 7
  5. 5 配置 nginx
  6. 6 在 PHP 7 中获得 MySQL 支持
  7. 7 让 PHP-FPM 使用 TCP 连接
  8. 8 个链接

PHP-FPM) 和 MySQL 5.7 支持 (LEMP=Linux + nginx (发音为 \engine x\) + MySQL + PHP)。

1 初步说明

在本教程中,我使用 IP 地址为 192.168.1.100 的主机名 server1.example.com。这些设置可能因您而异,因此您必须在适当的地方替换它们。

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

sudo -s

2 安装MySQL 5.7

为了安装 MySQL,我们运行:

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

系统将要求您提供 MySQL root 用户的密码 - 此密码对用户 [email 有效,因此我们以后不必手动指定 MySQL root 密码:

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

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

mysql_secure_installation

你会被问到这些问题:

:~# 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 16.04 上默认的 nginx 文档根目录是 /var/www/html。

4 安装 PHP 7

我们可以通过 PHP-FPM 使 PHP 在 nginx 中工作(PHP-FPM(FastCGI 进程管理器)是一种替代的 PHP FastCGI 实现,具有一些对任何规模的站点都有用的附加功能,尤其是繁忙的站点)我们安装如下:

apt-get -y install php7.0-fpm

PHP-FPM 是一个守护进程(带有初始化脚本 php7.0-fpm),它在套接字 /run/php/php7.0-fpm.sock 上运行 FastCGI 服务器。

5 配置nginx

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

nano /etc/nginx/nginx.conf

配置简单易懂(你可以在这里了解更多信息:http://wiki.nginx.org/NginxFullExample2)

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

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

虚拟主机在服务器 {} 容器中定义。默认虚拟主机在文件 /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;
}
} [...]

服务器名称 _;使它成为默认的通用虚拟主机(当然,您也可以在此处指定一个主机名,例如 www.example.com)。

根/var/www/html;表示文档根目录是目录/var/www/html。

PHP 的重要部分是 location ~ \.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):

如您所见,PHP 7 正在运行,并且它通过 FPM/FastCGI 运行,如服务器 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 模块:

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 行如下所示:

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

这将使 PHP-FPM 侦听 IP 127.0.0.1(本地主机)上的端口 9000。确保您使用的端口未在您的系统上使用。

然后重新加载 PHP-FPM:

php7.0-fpm reload

接下来检查您的 nginx 配置和所有虚拟主机并更改行 fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;到 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 服务器已安装。

8个链接

  • nginx:http://nginx.net/
  • nginx 维基:http://wiki.codemongers.com/Main
  • PHP:http://www.php.net/
  • PHP-FPM:http://php-fpm.org/
  • MySQL:http://www.mysql.com/
  • Ubuntu:http://www.ubuntu.com/

©2015-2025 艾丽卡 support@alaica.com