Ubuntu下安装和配置 Nginx + PHP + Mysql + FastCGI

安装nginx

	sudo apt-get install nginx

ubuntu安装之后的文件结构大致为:

  • /usr/local/nginx/conf 配置目录
  • /usr/local/nginx/html 默认的网站根目录
  • /usr/local/nginx/logs 日志和pid文件目录
  • /usr/local/nginx/sbin 执行文件目录

所有的配置文件都在/etc/nginx下,自己可以在/etc/nginx/sites-enabled下的default配置一个虚拟主机。

启动nginx

	sudo /etc/init.d/nginx start 启动
	sudo /etc/init.d/nginx restart 重启
	sudo /etc/init.d/nginx stop 停止
	sudo /etc/init.d/nginx reload 重新加载配置文件

要配置nginx的自动运行,可以将/usr/local/nginx/sbin/nginx添加到/etc/rc.local中,Ubuntu可以执行

	update-rc.d nginx defaults

安装MySQL

	sudo apt-get install mysql5.6

自己也可以到http://dev.mysql.com/downloads/ 下载需要的mysql版本

配置php和mysql

	sudo apt-get install php5-cli php5-cgi php5-mysql

单独运行

	php-cgi: ubuntu$ sudo php-cgi -b 127.0.0.1:9000

安装FastCgi

/usr/bin/spawn-fcgi这个文件来管理 FastCgi,它原属于lighttpd这个包里面,但 9.10 后,spawn-fcgi 被分离出来单独成包:

	sudo apt-get install spawn-fcgi

配置 nginx

修nginx的配置文件:sudo gedit /etc/nginx/nginx.conf 在http{}中加入以下内容

	fastcgi_connect_timeout 300;
	fastcgi_send_timeout 300;
	fastcgi_read_timeout 300;
	fastcgi_buffers 8 16k;
	fastcgi_buffer_size 32k;

构建自己的一个虚拟主机,修改nginx的配置文件:sudo gedit /etc/nginx/sites-enable/default

	server {
	listen   80; ## listen for ipv4; this line is default and implied
	#listen   [::]:80 default ipv6only=on; ## listen for ipv6
	 
	root /usr/share/nginx/www; //注意这里要写相应的文件夹路径
	index index.html index.htm index.php;
	 
	# Make site accessible from http://localhost/
	server_name localhost;
	 
	location / {
	# First attempt to serve request as file, then
	# as directory, then fall back to index.html
	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;
	deny all;
	}
	//新添加进去的内容
	location ~ \.php$ {
	        fastcgi_pass 127.0.0.1:9000;
	      fastcgi_index index.php;
	      fastcgi_param SCRIPT_FILENAME /usr/share/nginx/www(这是自己的文件夹位置)$fastcgi_script_name;
	      include /etc/nginx/fastcgi_params;
	    }
	}

配置Nginx的PHP FastCGI

请将以下内容保存为fastcgi_params文件,bbuntu下的位置为/etc/nginx下,他为我们的FastCGI模块设置了基本的环境变量:

	#fastcgi_params
	fastcgi_param GATEWAY_INTERFACE CGI/1.1;
	fastcgi_param SERVER_SOFTWARE nginx;
	fastcgi_param QUERY_STRING $query_string;
	fastcgi_param REQUEST_METHOD $request_method;
	fastcgi_param CONTENT_TYPE $content_type;
	fastcgi_param CONTENT_LENGTH $content_length;
	fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
	fastcgi_param SCRIPT_NAME $fastcgi_script_name;
	fastcgi_param REQUEST_URI $request_uri;
	fastcgi_param DOCUMENT_URI $document_uri;
	fastcgi_param DOCUMENT_ROOT $document_root;
	fastcgi_param SERVER_PROTOCOL $server_protocol;
	fastcgi_param REMOTE_ADDR $remote_addr;
	fastcgi_param REMOTE_PORT $remote_port;
	fastcgi_param SERVER_ADDR $server_addr;
	fastcgi_param SERVER_PORT $server_port;
	fastcgi_param SERVER_NAME $server_name;
	# PHP only, required if PHP was built with –enable-force-cgi-redirect
	fastcgi_param REDIRECT_STATUS 200;

请特别注意加粗的一行,PHP-CGI特别需要此行信息来确定PHP文件的位置。
另外需要在PHP-CGI的配置文件(Ubuntu 上此配置文件位于/etc/php5/cgi/php.ini)中,打开cgi.fix_pathinfo选项:

	cgi.fix_pathinfo=1;

这样php-cgi方能正常使用SCRIPT_FILENAME这个变量。
接下来在nginx的配置中针对php文件配置其利用FastCGI进程来执行:

	server {
	index index.php;
	root /usr/local/nginx/html;
	
	location ~ .*.php$ {
	include /usr/local/nginx/conf/fastcgi_params; #请根据自己保存的路径进行设置
	fastcgi_index index.php;
	fastcgi_pass 127.0.0.1:9000; #请根据自己的FastCGI绑定的地址和端口进行配置
	fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
	}
	}

通知Nginx重新载入配置:

	sudo /etc/init.d/nginx reload

然后启动

	sudo php-cgi -b 127.0.0.1:9000

假设你在文档根目录放了index.php,并包含“”的内容,现在再看http://localhost/index.php便应该能看到php的调试信息了。
配置php进程

直接使用php-cgi的FastCGI运行方式有两个问题(貌似应该有解决方案,如果知道的话可以教教我):

  1. 如果进程崩溃,难以配置重新启动
  2. 单进程的效率低

所以我们可以通过安装spawn-fcgi来代为管理,步骤六就是使用spawn-fcgi。

启动fastcgi php:

	/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php-cgi

参数含义如下

  • -f指定调用FastCGI的进程的执行程序位置,根据系统上所装的PHP的情况具体设置
  • -a绑定到地址addr
  • -p绑定到端口port
  • -s绑定到unix socket的路径path
  • -C指定产生的FastCGI的进程数,默认为5(仅用于PHP)
  • -P指定产生的进程的PID文件路径
  • -u和-g FastCGI使用什么身份(-u 用户 -g 用户组)运行,Ubuntu下可以使用www-data,其他的根据情况配置,如nobody、apache等

php-cgi

	sudo killall -HUP php-cgi

重启php-cgi

	spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u www-data -f /usr/bin/php-cgi

为了让php-cgi开机自启动:

	cd /etc/init.d
	cp nginx php-cgi
	sudo gedit php-cgi

替换nginx为php-cgi并修改相应部分为:

	DAEMON=/usr/bin/spawn-fcgi
	DAEMON_OPTS="-a 127.0.0.1 -p 9000 -C 10 -u www-data -f /usr/bin/php-cgi"
	...
	stop)
	echo -n "Stopping $DESC: "
	pkill -9 php-cgi
	echo "$NAME."

启动php-cgi

	/usr/bin/spawn-fcgi  -a 127.0.0.1 -p 9000 -C 128 -u www-data -f /usr/bin/php-cgi

建议把上面一行写入/etc/rc.local,开机将自动启动php-cgi

然后运行rcconf设置php-cgi为开机自启动
先安装rcconf工具:

	sudo apt-get install rcconf

安装mysql管理工具phpmyadmin:sudo apt-get install phpmyadmin。

注:phpmyadmin设置:

在安装过程中会要求选择Web server:apache2或lighttpd,选择apache2,按tab键然后确定。然后会要求输入设置的mysql数据库密码连接密码 Password of the database’s administrative user。

然后将phpmyadmin与apache2建立连接,以我的为例:www目录在/var/www,phpmyadmin在/usr/share/phpmyadmin目录,所以就用命令:sudo ln -s /usr/share/phpmyadmin /usr/share/nginx/www 建立连接。

创建、测试phpinfo:

	sudo vim /usr/share/nginx/www/index.php

然后在浏览器地址栏输入 http://localhost/index.php 会显php的信息

文章目录
  1. 1. 安装nginx
  2. 2. 启动nginx
  3. 3. 安装MySQL
  4. 4. 配置php和mysql
  5. 5. 安装FastCgi
  6. 6. 配置 nginx
  7. 7. 配置Nginx的PHP FastCGI
  8. 8. 启动fastcgi php:
  9. 9. php-cgi
  10. 10. 重启php-cgi
  11. 11. 为了让php-cgi开机自启动:
  12. 12. 启动php-cgi
  13. 13. 安装mysql管理工具phpmyadmin:sudo apt-get install phpmyadmin。
  14. 14. 创建、测试phpinfo:
|