在Ubuntu上安装配置Nginx, PHP, MySQL

Nginx是一简单却功能强大的网络服务器,以稳定著称。若配置得当,nginx将很少会使CPU负担达到峰值,有效降低内存消耗。Nginx以其稳定性、富特点设置、配置简单、低资源消耗得到好评。
在这篇文章里,我想和大家分享一下,用Nginx作为网络服务器、支持PHP5(使用fastcgi)和MySQL,如何来安装和配置Ubuntu服务器。


安装MySQL服务器
1.打开终端
2.安装MySQL
sudo apt-get install mysql-server mysql-client

安装、配置Nginx
1.打开终端
2.升级软件仓库列表
sudo apt-get update
3.安装nginx
sudo apt-get install nginx
4.Nginx默认配置文件在 etc/nginx/sites-available/default,该文件应该改成:

server {
listen 80;
server_name localhost;
access_log /var/log/nginx/localhost.access.log;

## Default location
location / {
root /var/www;
index index.php;
}

## Images and static content is treated different
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
access_log off;
expires 30d;
root /var/www;
}

## Parse all .php file in the /var/www directory
location ~ .php$ {
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass backend;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
include fastcgi_params;
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_intercept_errors on;
fastcgi_ignore_client_abort off;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}

 

## Disable viewing .htaccess & .htpassword
location ~ /\.ht {
deny all;
}
}
upstream backend {
server 127.0.0.1:9000;
}

用Fastcgi安装和配置PHP5
1.安装spawn-fcgi,输入命令:
sudo apt-get install spawn-fcgi
2.安装PHP
sudo apt-get install php5 php5-cli php5-common php5-suhosin php5-cgi php-pear php5-mysql
3.现在写一个脚本,它使FastCGI PHP 进程出现在unix domain socket中。我们建立文件 /usr/sbin/fastcgi-php。
sudo touch /usr/sbin/fastcgi-php
然后将下面内容写入文件:

#!/bin/sh
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -f /usr/bin/php5-cgi

4.使它在启动时工作,需要创建一个init 脚本:
sudo touch /etc/init.d/init-fastcgi
将以下内容写入文件:
#!/bin/bash
PHP_SCRIPT=/usr/bin/php-fastcgi
RETVAL=0
case "$1" in
start)
$PHP_SCRIPT
RETVAL=$?
;;
stop)
killall -9 php
RETVAL=$?
;;
restart)
killall -9 php
$PHP_SCRIPT
RETVAL=$?
;;
*)
echo "Usage: php-fastcgi {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL

 

赋予脚本有执行权限:

sudo chmod 755 /etc/init.d/init-fastcgi
#then run it
/etc/init.d/init-fastcgi start

使它开机启动
sudo update-rc.d init-fastcgi defaults
5.重启Nginx
sudo /etc/init.d/nginx restart
6.使用phpinfo()进行测试。如果结果OK,则成功了,没有成功你继续调试吧。

注意:该步骤经Ubuntu10.04服务器测试通过,可能对你的版本来说不太好使,但这是安装Nginx,PHP,MySQL的基本步骤。如果没有成功,看看哪里出了问题,继续调试。

来源『ivankristianto.com』,本文由Bentutu.com编辑,转载注明出处

 

Advertisements


发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s