搜索
首页数据库mysql教程安装带有 PHP5(和 PHP-FPM)和 MySQL 支持的 Nginx(_MySQL

Nginx

在 Ubuntu 14.04 LTS 上安装带有 PHP5(和 PHP-FPM)和 MySQL 支持(LEMP)的 Nginx

版本 1.0
作者:Falko Timme,由 Srijan Kishore 更新
最后编辑 07/ 2014 年 5 月

Nginx(发音为“engine x”)是一个免费、开源、高性能的 HTTP 服务器。 Nginx 以其稳定性、丰富的功能集、简单的配置和低资源消耗而闻名。本教程展示了如何在支持 PHP5(通过 PHP-FPM)和 MySQL 支持的 Ubuntu 14.04 服务器上安装 Nginx(LEMP =Linux nginx(发音为“engine x”) MySQL PHP) .

我不保证这对您有用!

1 初步说明

在本教程中,我使用主机名server1.example.com,IP 地址为192.168.0.100。这些设置可能与您不同,因此您必须在适当的情况下替换它们。

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

sudo su

2 安装 MySQL 5

为了安装 MySQL,我们运行

apt-get install mysql-server mysql-client

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

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

3 安装 Nginx

Nginx 作为 Ubuntu 14.04 的软件包提供,我们可以安装它,由于 Apache2 是默认安装的,所以我们首先将其删除,然后安装 nginx:

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

apt-get install nginx

之后启动 nginx:

service nginx start

类型在浏览器中输入您的网络服务器的 IP 地址或主机名(例如 http://192.168.0.100),您应该看到以下页面:

安装带有 PHP5(和 PHP-FPM)和 MySQL 支持的 Nginx(_MySQLClick to enlarge

默认Ubuntu 14.04 上的 nginx 文档根目录是/usr/share/nginx/html。

4 安装 PHP5

我们可以通过 PHP-FPM(PHP-FPM (FastCGI Process Manager))让 PHP5 在 nginx 中工作是另一种 PHP FastCGI 实现,具有一些对任何规模的网站(尤其是繁忙的网站)有用的附加功能,我们安装如下:

apt-get install php5-fpm

PHP-FPM 是一个守护进程(带有初始化脚本php5-fpm),在socket/var/run/php5-fpm.sock上运行FastCGI服务器。

5 配置nginx

nginx 配置位于我们现在打开/etc/nginx/nginx.conf:

vi /etc/nginx/nginx.conf

配置很容易理解(你可以在这里了解更多:http: //wiki.nginx.org/NginxFullExample 和这里​​:http://wiki.nginx.org/NginxFullExample2)

首先(这是可选的)调整工作进程的数量并将keepalive_timeout 设置为合理的值:

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

虚拟主机在服务器容器中定义。默认虚拟主机在文件 /etc/nginx/sites-available/default 中定义 - 让我们修改它如下:

vi /etc/nginx/sites-available/default

[...]server {	listen 80;	listen [::]:80 default_server ipv6only=on;	root /usr/share/nginx/html;	index index.php index.html index.htm;	# Make site accessible from http://localhost/	server_name _;	location / {		# First attempt to serve request as file, then		# as directory, then fall back to displaying a 404.		try_files $uri $uri/ /index.html;		# Uncomment to enable naxsi on this location		# include /etc/nginx/naxsi.rules	}	location /doc/ {		alias /usr/share/doc/;		autoindex on;		allow 127.0.0.1;		allow ::1;		deny all;	}	# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests	#location /RequestDenied {	# proxy_pass http://127.0.0.1:8080;	#}	#error_page 404 /404.html;	# redirect server error pages to the static page /50x.html	#	error_page 500 502 503 504 /50x.html;	location = /50x.html {		root /usr/share/nginx/html;	}	# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000	#	location ~ /.php$ {		try_files $uri =404;		fastcgi_split_path_info ^(.+/.php)(/.+)$;		# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini		# With php5-cgi alone:		#fastcgi_pass 127.0.0.1:9000;		# With php5-fpm:		fastcgi_pass unix:/var/run/php5-fpm.sock;		fastcgi_index index.php;		include fastcgi_params;	}	# deny access to .htaccess files, if Apache's document root	# concurs with nginx's one	#	location ~ //.ht {		deny all;	}}[...]

取消注释这两个listen,使nginx监听端口80 IPv4IPv6。

server_name _;使其成为默认的包罗万象的虚拟主机(当然,您也可以在这里指定一个主机名,例如www.example .com)。

我在indexline.root /usr/share/nginx/html中添加了index.php;表示文档根目录是/usr/share/nginx/html目录。

PHP 的重要部分是位置 ~ /.php$ {} 节。取消注释以启用它。请注意,我添加了 linetry_files $uri =404; 以防止零日漏洞(请参阅http://wiki.nginx.org/Pitfalls#Passing_Uncontrol_Requests_to_PHP 和 http://forum.nginx.org/read.php?2,88845 ,page=3).

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

服务 nginx 重新加载

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

vi /etc/php5/fpm/php.ini

... 和 setcgi.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-pathinfocgi.fix_pathinfo=0[...]

Reload PHP-FPM:

service php5-fpm reload

Now create the following PHP file in the document root/usr/share/nginx/html:

vi /usr/share/nginx/html/info.php

<?phpphpinfo ();?>

Now we call that file in a browser (e.g.http://192.168.0.100/info.php):

安装带有 PHP5(和 PHP-FPM)和 MySQL 支持的 Nginx(_MySQLClick to enlarge

As you see, PHP5 is working, and it's working through FPM/FastCGI, as shown in theServer APIline. If you scroll further down, you will see all modules that are already enabled in PHP5. MySQL is not listed there which means we don't have MySQL support in PHP5 yet.

6 Getting MySQL Support In PHP5

To get MySQL support in PHP, we can install thephp5-mysqlpackage. It's a good idea to install some other PHP5 modules as well as you might need them for your applications. You can search for available PHP5 modules like this:

apt-cache search php5

Pick the ones you need and install them like this:

apt-get install php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl

APC is a free and open PHP opcode cacher for caching and optimizing PHP intermediate code. It's similar to other PHP opcode cachers, such as eAccelerator and Xcache. It is strongly recommended to have one of these installed to speed up your PHP page.

APC can be installed as follows:

apt-get install php-apc

Now reload PHP-FPM:

service php5-fpm reload

Now reloadhttp://192.168.0.100/info.phpin your browser and scroll down to the modules section again. You should now find lots of new modules there, including the MySQL module:

安装带有 PHP5(和 PHP-FPM)和 MySQL 支持的 Nginx(_MySQLClick to enlarge

7 Making PHP-FPM Use A TCP Connection

By default PHP-FPM is listening on the socket/var/run/php5-fpm.sock. It is also possible to make PHP-FPM use a TCP connection. To do this, open/etc/php5/fpm/pool.d/www.conf...

vi /etc/php5/fpm/pool.d/www.conf

... and make thelistenline look as follows:

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

This will make PHP-FPM listen on port9000on the IP127.0.0.1(localhost). Make sure you use a port that is not in use on your system.

Then reload PHP-FPM:

php5-fpm reload

Next go through your nginx configuration and all your vhosts and change the linefastcgi_pass unix:/var/run/php5-fpm.sock;tofastcgi_pass 127.0.0.1:9000;, e.g. like this:

vi /etc/nginx/sites-available/default

[...]location ~ /.php$ {	try_files $uri =404;	fastcgi_split_path_info ^(.+/.php)(/.+)$;	# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini	# With php5-cgi alone:	fastcgi_pass 127.0.0.1:9000;	# With php5-fpm:	#fastcgi_pass unix:/var/run/php5-fpm.sock;	fastcgi_index index.php;	include fastcgi_params;}[...]

Finally reload nginx:

service nginx reload

8 CGI/Perl Scripts

If you want to serve CGI/Perl scripts with nginx, please read this tutorial:Serving CGI Scripts With Nginx On Debian Squeeze/Ubuntu 11.04

The recommended way is to usefcgiwrap(chapter 4).

  • nginx:http://nginx.net/
  • nginx Wiki: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/
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
如何使用Alter Table语句在MySQL中更改表?如何使用Alter Table语句在MySQL中更改表?Mar 19, 2025 pm 03:51 PM

本文讨论了使用MySQL的Alter Table语句修改表,包括添加/删除列,重命名表/列以及更改列数据类型。

如何为MySQL连接配置SSL/TLS加密?如何为MySQL连接配置SSL/TLS加密?Mar 18, 2025 pm 12:01 PM

文章讨论了为MySQL配置SSL/TLS加密,包括证书生成和验证。主要问题是使用自签名证书的安全含义。[角色计数:159]

您如何处理MySQL中的大型数据集?您如何处理MySQL中的大型数据集?Mar 21, 2025 pm 12:15 PM

文章讨论了处理MySQL中大型数据集的策略,包括分区,碎片,索引和查询优化。

哪些流行的MySQL GUI工具(例如MySQL Workbench,PhpMyAdmin)是什么?哪些流行的MySQL GUI工具(例如MySQL Workbench,PhpMyAdmin)是什么?Mar 21, 2025 pm 06:28 PM

文章讨论了流行的MySQL GUI工具,例如MySQL Workbench和PhpMyAdmin,比较了它们对初学者和高级用户的功能和适合性。[159个字符]

如何使用Drop Table语句将表放入MySQL中?如何使用Drop Table语句将表放入MySQL中?Mar 19, 2025 pm 03:52 PM

本文讨论了使用Drop Table语句在MySQL中放下表,并强调了预防措施和风险。它强调,没有备份,该动作是不可逆转的,详细介绍了恢复方法和潜在的生产环境危害。

如何在JSON列上创建索引?如何在JSON列上创建索引?Mar 21, 2025 pm 12:13 PM

本文讨论了在PostgreSQL,MySQL和MongoDB等各个数据库中的JSON列上创建索引,以增强查询性能。它解释了索引特定的JSON路径的语法和好处,并列出了支持的数据库系统。

您如何用外国钥匙代表关系?您如何用外国钥匙代表关系?Mar 19, 2025 pm 03:48 PM

文章讨论了使用外国密钥来代表数据库中的关系,重点是最佳实践,数据完整性和避免的常见陷阱。

如何保护MySQL免受常见漏洞(SQL注入,蛮力攻击)?如何保护MySQL免受常见漏洞(SQL注入,蛮力攻击)?Mar 18, 2025 pm 12:00 PM

文章讨论了使用准备好的语句,输入验证和强密码策略确保针对SQL注入和蛮力攻击的MySQL。(159个字符)

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具