Ubuntu Server 9.04下安装nginx+PHP+MySQL
笔记而已,供以后查阅,大部分的内容参照http://blog.s135.com/nginx_php_v5/和http://rashost.com/blog,省去了第三方的一些编译,个人来说特别不喜欢编译(除非没有办法),自己编译不会带来性能多大的提高,维护起来还比较麻烦,尤其是卸载升级的时候。
Ubuntu Server 9.04的安装是最干净的那种,除了openssh-server,openssh-client外别的没有安装。
安装完系统后,如果选的是中文的话,最好重新设置一下语言:
vi /etc/default/locale
LANG="en_US.UTF-8"
LANGUAGE="en_US:en"这样不会有一些乱码啊,菱形字的出现。
设置apt升级源:
vi /etc/apt/sources.list全部干掉,测试了两个速度比较好的源:
deb http://mirror.optus.net/ubuntu/ jaunty main restricted universe multiverse
deb http://mirror.optus.net/ubuntu/ jaunty-security main restricted universe multiverse
deb http://mirror.optus.net/ubuntu/ jaunty-updates main restricted universe multiverse
deb http://mirror.optus.net/ubuntu/ jaunty-proposed main restricted universe multiverse
deb http://mirror.optus.net/ubuntu/ jaunty-backports main restricted universe multiverse
deb-src http://mirror.optus.net/ubuntu/ jaunty main restricted universe multiverse
deb-src http://mirror.optus.net/ubuntu/ jaunty-security main restricted universe multiverse
deb-src http://mirror.optus.net/ubuntu/ jaunty-updates main restricted universe multiverse
deb-src http://mirror.optus.net/ubuntu/ jaunty-proposed main restricted universe multiverse
deb-src http://mirror.optus.net/ubuntu/ jaunty-backports main restricted universe multiverse
deb http://ftp.jaist.ac.jp/pub/Linux/ubuntu/ jaunty main restricted universe multiverse
deb http://ftp.jaist.ac.jp/pub/Linux/ubuntu/ jaunty-security main restricted universe multiverse
deb http://ftp.jaist.ac.jp/pub/Linux/ubuntu/ jaunty-updates main restricted universe multiverse
deb http://ftp.jaist.ac.jp/pub/Linux/ubuntu/ jaunty-proposed main restricted universe multiverse
deb http://ftp.jaist.ac.jp/pub/Linux/ubuntu/ jaunty-backports main restricted universe multiverse
deb-src http://ftp.jaist.ac.jp/pub/Linux/ubuntu/ jaunty main restricted universe multiverse
deb-src http://ftp.jaist.ac.jp/pub/Linux/ubuntu/ jaunty-security main restricted universe multiverse
deb-src http://ftp.jaist.ac.jp/pub/Linux/ubuntu/ jaunty-updates main restricted universe multiverse
deb-src http://ftp.jaist.ac.jp/pub/Linux/ubuntu/ jaunty-proposed main restricted universe multiverse
deb-src http://ftp.jaist.ac.jp/pub/Linux/ubuntu/ jaunty-backports main restricted universe multiverse当然,你也可以试试163的源,不过我这边的情况是龟速:
deb http://mirrors.163.com/Ubuntu/ jaunty main restricted universe multiverse
deb http://mirrors.163.com/Ubuntu/ jaunty-security main restricted universe multiverse
deb http://mirrors.163.com/Ubuntu/ jaunty-updates main restricted universe multiverse
deb http://mirrors.163.com/Ubuntu/ jaunty-proposed main restricted universe multiverse
deb http://mirrors.163.com/Ubuntu/ jaunty-backports main restricted universe multiverse
deb-src http://mirrors.163.com/Ubuntu/ jaunty main restricted universe multiverse
deb-src http://mirrors.163.com/Ubuntu/ jaunty-security main restricted universe multiverse
deb-src http://mirrors.163.com/Ubuntu/ jaunty-updates main restricted universe multiverse
deb-src http://mirrors.163.com/Ubuntu/ jaunty-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/Ubuntu/ jaunty-backports main restricted universe multiverse设置完源之后,执行:
apt-get update获得软件包信息,如果需要升级系统,还需要:
apt-get upgrade这个貌似比yum要复杂些,怀念yum update & yum install yum-fastestmirror
安装MySQL
直接来apt-get安装吧:
apt-get install mysql-server-5.1 mysql-client-5.1 –no-install-recommends记得加上–no-install-recommends,要不然连postfix都给你装了。
安装nginx
添加www用户和www用户组
/usr/sbin/groupadd www
/usr/sbin/useradd -g www www安装第三方包:
apt-get install gcc
apt-get install libpcre3-dev libssl-dev下载源码编译:
tar xzf nginx-0.8.14.tar.gz -C /usr/local/src
cd /usr/local/src/nginx-0.8.14/
./configure –user=www –group=www –with-http_stub_status_module –with-http_ssl_module
make && make install缺什么就apt-get安装,编辑nginx配置文件:vi /usr/local/nginx/conf/nginx.conf
user www www
worker_processes 2
error_log /var/log/nginx/error.log crit
pid /usr/local/nginx/nginx.pid
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200
events
{
use epoll
worker_connections 51200
}
http
{
include mime.types
default_type application/octet-stream
#charset gb2312
server_names_hash_bucket_size 128
client_header_buffer_size 32k
large_client_header_buffers 4 32k
client_max_body_size 8m
sendfile on
tcp_nopush on
keepalive_timeout 60
tcp_nodelay on
fastcgi_connect_timeout 300
fastcgi_send_timeout 300
fastcgi_read_timeout 300
fastcgi_buffer_size 64k
fastcgi_buffers 4 64k
fastcgi_busy_buffers_size 128k
fastcgi_temp_file_write_size 128k
gzip on
gzip_min_length 1k
gzip_buffers 4 16k
gzip_http_version 1.0
gzip_comp_level 2
gzip_types text/plain application/x-javascript text/css application/xml
gzip_vary on
#limit_zone crawler $binary_remote_addr 10m
server
{
listen 80
server_name 127.0.0.1
index index.html index.htm index.php
root /data/www
#limit_conn crawler 20
location ~ .*\.php
{
include fcgi.conf
}
location ~ .*\.(gifjpgjpegpngbmpswf)$
{
expires 30d
}
location ~ .*\.(jscss)?$
{
expires 1h
}
log_format access '$remote_addr – $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for'
access_log /var/log/nginx/access.log access
}
}编辑fcgi.conf文件,让nginx支持PATH_INFO变量:
fastcgi_pass 127.0.0.1:9000
#fastcgi_index index.php
set $path_info ""
set $real_script_name $fastcgi_script_name
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
set $real_script_name $1
set $path_info $2
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name
fastcgi_param SCRIPT_NAME $real_script_name
fastcgi_param PATH_INFO $path_info
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
下载最新的PHP5.2.10、php-fpm:
wget http://cn.php.net/get/php-5.2.10.tar.gz/from/this/mirror
wget http://php-fpm.org/downloads/php-5.2.10-fpm-0.5.13.diff.gz安装一些要用到的第三包:
apt-get install libxml2-dev libmcrypt-dev libssl-dev libldap2-dev libmhash-dev libmysqlclient-dev libcurl4-openssl-dev libpng-dev libjpeg-dev libsasl2-dev libdb4.7-dev解压缩PHP源码,并打上php-fpm补丁:
tar xzf php-5.2.10.tar.gz -C /usr/local/src
gzip -cd php-5.2.10-fpm-0.5.11.diff.gz patch -d /usr/local/srcphp-5.2.10 -p1编译安装PHP&php-fpm:
./configure –with-iconv –with-zlib –enable-xml –disable-rpath –enable-discard-path –enable-bcmath –enable-shmop –enable-sysvsem –enable-inline-optimization –with-curl –with-curlwrappers –enable-mbregex –enable-fastcgi –enable-fpm –enable-force-cgi-redirect –enable-mbstring –with-mcrypt –with-gd –enable-gd-native-ttf –with-mhash –enable-pcntl –enable-sockets –with-xmlrpc –enable-zip –with-mysql –with-mysqli –enable-sqlite-utf8 –with-pdo-mysql –enable-ftp –with-jpeg-dir –with-png-dir –enable-dba –with-db4=/usr/lib/ –without-pear
make
make install可以看到我在扩展里面打开了dba扩展,并且指定了bdb库位置,如果你不需要PHP去访问bdb可以不用。安装的过程中提示缺什么包,直接apt-get install就可以了,也没有必要担心安装的不全,到时候缺什么,直接单个编译就可以了,比如想编译APC支持,到APC源码位置:
phpize
./configure
make
make install如果没有安装autoconf包,直接apt-get install autoconf就可以。把编译好的so文件拷贝到php扩展目录就可以了。
复制php.ini-recommended为php.ini到php ini配置目录,目录可以通过php -i|grep php.ini查看。找到php-fpm.con(命令:whereis php-fpm.conf),去掉display_errors参数的注释,修改参数值为1;去掉user,group参数的注释,设置user,group,一般设定为www就可以;修改max_children参数的值。
启动服务器
ulimit -SHn 51200
/usr/local/sbin/php-fpm start
/usr/local/nginx/sbin/nginx测试
最基础的测试,phpinfo.php:
测试了下ColaPHP在nginx+php+fastcgi下的表现,原生的PHP大概940RPS,ColaPHP大概是650RPS。