红联Linux门户
Linux帮助

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

发布时间:2011-01-16 12:19:10来源:红联作者:tuhaihe
[i=s] 本帖最后由 tuhaihe 于 2011-1-16 12:20 编辑 [/i]

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


安装MySQL服务器
1.打开终端
2.安装MySQL[code]sudo apt-get install mysql-server mysql-client[/code]安装、配置Nginx
1.打开终端
2.升级软件仓库列表[code]sudo apt-get update[/code]3.安装nginx[code]sudo apt-get install nginx[/code]4.Nginx默认配置文件在 etc/nginx/sites-available/default,该文件应该改成:[code]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;
}[/code]用Fastcgi安装和配置PHP5
1.安装spawn-fcgi,输入命令:[code]sudo apt-get install spawn-fcgi[/code]2.安装PHP[code]sudo apt-get install php5 php5-cli php5-common php5-suhosin php5-cgi php-pear php5-mysql[/code]3.现在写一个脚本,它使FastCGI PHP 进程出现在unix domain socket中。我们建立文件 /usr/sbin/fastcgi-php。[code]sudo touch /usr/sbin/fastcgi-php[/code]然后将下面内容写入文件:[code]#!/bin/sh
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -f /usr/bin/php5-cgi[/code]4.使它在启动时工作,需要创建一个init 脚本:[code]sudo touch /etc/init.d/init-fastcgi[/code]将以下内容写入文件:[code]#!/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[/code]赋予脚本有执行权限:[code]sudo chmod 755 /etc/init.d/init-fastcgi
#then run it
/etc/init.d/init-fastcgi start[/code]使它开机启动[code]sudo update-rc.d init-fastcgi defaults[/code]5.重启Nginx[code]sudo /etc/init.d/nginx restart[/code]6.使用phpinfo()进行测试。如果结果OK,则成功了,没有成功你继续调试吧。

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

来源『ivankristianto.com』,本文由Bentutu.com编辑,转载注明出处http://bentutu.com/?p=424
文章评论

共有 7 条评论

  1. william2011 于 2014-07-29 13:10:55发表:

    好贴

  2. lijiang 于 2011-06-15 19:03:32发表:

    ding

  3. nktxsj 于 2011-01-21 15:34:15发表:

    支持原创啊

  4. w417592279 于 2011-01-17 15:26:44发表:

    只能支持php吧 asp 或者其他的怎么办

  5. Growth兆 于 2011-01-17 09:58:29发表:

    感谢tuhaihe的技术应用贡献,本文已被论坛置顶的Linux应用贴索引贴收录

  6. tuhaihe 于 2011-01-16 21:58:46发表:

    好帖子,别沉了~~

  7. Around 于 2011-01-16 15:28:50发表:

    路过学习