ubuntu nginx + mysql + php5-fpm + wordpress详细配置

原文链接: http://www.netroby.com/article-1367.html

文章自己写的,只存在于自己的javaeye博客和自己的独立博客。欢迎转载,多谢注明出处。

ubuntu 8.04 LTS 64位系统nginx + mysql + php5-fpm + wordpress详细配置,最后更新于2010年04月26日。
网上找一圈,也没找到一个非常全的快速配置nginx,mysql,php-fpm的资料,大多文章都是转载别人,且不论对错,不求证,直接搬过来。
绕了许多弯路,终于顺利的把配置工作做到位了。现在把配置流程文档整理出来,跟大家分享,如果大家发现有不对的地方,请指证出来。我会实时加以完善和修 正,做出更好的更订和修改。

安装教程是用root权限执行的。
vps服务器的环境如下:
128M内存
XEN vps
交换分区是256M
硬盘空间是3G
操作系统我选用的是ubuntu 8.04 LTS 的64位版本

1. 安装PHP5-fpm
64 位ubuntu 安装 32位兼容包

apt-get install ia32-libs

ubuntu 需要安装依赖包

wget http://us.archive.ubuntu.com/ubuntu/pool/main/k/krb5/libkrb53_1.6.dfsg.4~beta1-5ubuntu2_i386.deb
wget http://us.archive.ubuntu.com/ubuntu/pool/main/i/icu/libicu38_3.8-6ubuntu0.2_i386.deb
dpkg -i *.deb

添加第三方源,来自于dotdeb组织的php5-fpm的deb包,编辑 /etc/apt/sources.list

deb http://packages.dotdeb.org stable all
deb-src http://packages.dotdeb.org stable all
deb http://php53.dotdeb.org stable all

更新源

apt-get update

安装php环境

apt-get install php5-cli php5-common php5-suhosin php5-fpm php5-cgi php5-mysql php5-gd

我的wordpress目前只用到这些php组件,所以我就只装这些
2. 安装nginx

echo "deb http://ppa.launchpad.net/jdub/devel/ubuntu hardy main" >> /etc/apt/sources.list
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E9EEF4A1
apt-get update
apt-get install nginx

更多关于nginx的安装配置请参考:

http://wiki.nginx.org/NginxInstall

http://articles.slicehost.com/nginx

3. 开始配置nginx

编 辑配置 vim /etc/nginx/wordpress.conf

输入以下内容

location / {
index index.html index.php;
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
 expires max;
 break;
}

location ~ .*\.php$ {
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;

}
location ~ .*\.(gif|jpg|png|swf|flv)$ {
valid_referers none blocked *.xgyl.net *.netroby.com *.google.com;
if ($invalid_referer) {
return 403;
}
}

编 辑配置 vim /etc/nginx/nginx.conf

用下面的内容替换原来的所有内容

user www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

worker_rlimit_nofile 1024;
events {
use epoll;

worker_connections  1024;
}

http {
include       /etc/nginx/mime.types;
default_type  application/octet-stream;

access_log    /var/log/nginx/access.log;
optimize_server_names off;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;

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 7;
gzip_types       text/plain application/x-javascript text/css application/xml;

include /etc/nginx/sites-enabled/*;

}

编 辑配置 vim /etc/nginx/sites-availale/default

输入以下内容,自行修改为自己的域名

server {
listen   80;
server_name www.netroby.com;
root /vhosts/www.netroby.com;
include /etc/nginx/wordpress.conf;
}
server
{
listen 80;
server_name www.xgyl.net;
root /vhosts/www.xgyl.net;
include /etc/nginx/wordpress.conf;
}

编 辑配置 vim /etc/nginx/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;

4.配置 php5-fpm

编 辑配置 vim /etc/php5/fpm/php5-fpm.conf

用下面的配置替换原来的内容

<?xml version="1.0" ?>
<configuration>

All relative paths in this config are relative to php's install prefix

<section name="global_options">

Pid file
<value name="pid_file">/var/run/php5-fpm.pid</value>

Error log file
<value name="error_log">/var/log/php5-fpm.log</value>

Log level
<value name="log_level">notice</value>

When this amount of php processes exited with SIGSEGV or SIGBUS ...
<value name="emergency_restart_threshold">10</value>

... in a less than this interval of time, a graceful restart will be initiated.
Useful to work around accidental curruptions in accelerator's shared memory.
<value name="emergency_restart_interval">1m</value>

Time limit on waiting child's reaction on signals from master
<value name="process_control_timeout">5s</value>

Set to 'no' to debug fpm
<value name="daemonize">yes</value>

</section>

<workers>

<section name="pool">

Name of pool. Used in logs and stats.
<value name="name">default</value>

Address to accept fastcgi requests on.
Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'
<value name="listen_address">127.0.0.1:9000</value>

<value name="listen_options">

Set listen(2) backlog
<value name="backlog">-1</value>

Set permissions for unix socket, if one used.
In Linux read/write permissions must be set in order to allow connections from web server.
Many BSD-derrived systems allow connections regardless of permissions.
<value name="owner">www-data</value>
<value name="group">www-data</value>
<value name="mode">0666</value>
</value>

Additional php.ini defines, specific to this pool of workers.
These settings overwrite the values previously defined in the php.ini.
<value name="php_defines">
<!-- <value name="sendmail_path">/usr/sbin/sendmail -t -i</value> -->
<!-- <value name="display_errors">0</value> -->
<!-- <value name="error_log">/var/log/php-error.log</value> -->
<!-- <value name="log_errors">true</value> -->
</value>

Unix user of processes
<value name="user">www-data</value>

Unix group of processes
<value name="group">www-data</value>

Process manager settings
<value name="pm">

Sets style of controling worker process count.
Valid values are 'static' and 'apache-like'
<value name="style">static</value>

Sets the limit on the number of simultaneous requests that will be served.
Equivalent to Apache MaxClients directive.
Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi
Used with any pm_style.
<value name="max_children">3</value>

Settings group for 'apache-like' pm style
<value name="apache_like">

Sets the number of server processes created on startup.
Used only when 'apache-like' pm_style is selected
<value name="StartServers">20</value>

Sets the desired minimum number of idle server processes.
Used only when 'apache-like' pm_style is selected
<value name="MinSpareServers">5</value>

Sets the desired maximum number of idle server processes.
Used only when 'apache-like' pm_style is selected
<value name="MaxSpareServers">35</value>

</value>

</value>

The timeout (in seconds) for serving a single request after which the worker process will be terminated
Should be used when 'max_execution_time' ini option does not stop script execution for some reason
'0s' means 'off'
<value name="request_terminate_timeout">30s</value>

The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file
'0s' means 'off'
<value name="request_slowlog_timeout">0s</value>

The log file for slow requests
<value name="slowlog">/var/log/php5-fpm.log.slow</value>

Set open file desc rlimit
<value name="rlimit_files">1024</value>

Set max core size rlimit
<value name="rlimit_core">0</value>

Chroot to this directory at the start, absolute path
<value name="chroot"></value>

Chdir to this directory at the start, absolute path
<value name="chdir"></value>

Redirect workers' stdout and stderr into main error log.
If not set, they will be redirected to /dev/null, according to FastCGI specs
<value name="catch_workers_output">yes</value>

How much requests each process should execute before respawn.
Useful to work around memory leaks in 3rd party libraries.
For endless request processing please specify 0
Equivalent to PHP_FCGI_MAX_REQUESTS
<value name="max_requests">500</value>

Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect.
Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+)
Makes sense only with AF_INET listening socket.
<value name="allowed_clients">127.0.0.1</value>

Pass environment variables like LD_LIBRARY_PATH
All $VARIABLEs are taken from current environment
<value name="environment">
<value name="HOSTNAME">$HOSTNAME</value>
<value name="PATH">/usr/local/bin:/usr/bin:/bin</value>
<value name="TMP">/tmp</value>
<value name="TMPDIR">/tmp</value>
<value name="TEMP">/tmp</value>
<value name="OSTYPE">$OSTYPE</value>
<value name="MACHTYPE">$MACHTYPE</value>
<value name="MALLOC_CHECK_">2</value>
</value>

</section>

</workers>

</configuration>


5. 保存配置,并运行nginx和php-fpm

/etc/init.d/nginx restart
/etc/init.d/php5-fpm restart

6. 安装mysql-server

apt-get install mysql-server libtcmalloc-minimal0

编辑mysql启动文件 vim /etc/init.d/mysql

在export Home下边加上下面一句,并保存

export LD_PRELOAD="/usr/lib/libtcmalloc_minimal.so.0"

编辑Mysql的配置文件 vim /etc/mysql/my.cnf

用下面的配置覆盖原来的配置

[client]
port            = 3306
socket          = /var/run/mysqld/mysqld.sock

[mysqld_safe]
socket          = /var/run/mysqld/mysqld.sock
nice            = 0

[mysqld]
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
language        = /usr/share/mysql/english
skip-external-locking
bind-address            = 127.0.0.1
key_buffer              = 16K
max_allowed_packet      = 1M
table_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K

thread_stack            = 64K
thread_cache_size       = 8
myisam-recover         = BACKUP
query_cache_limit       = 64K
query_cache_size        = 256K
expire_logs_days        = 10
max_binlog_size         = 10M
[mysqldump]
quick
quote-names
max_allowed_packet      = 16M

[mysql]

[isamchk]
key_buffer              = 16M

!includedir /etc/mysql/conf.d/

更多关于mysql的内容,请参考:

http://articles.slicehost.com/mysql

7. 测试

编 辑文件 vim /vhosts/www.netroby.com/index.php

<?php
phpinfo();

如果看到正确的php信息,则可以开始安装wordpress了。

如果你用的ubuntu的版本不是8.04,那么,就要修改/etc/nginx/nginx.conf中的 optimize_server_names为server_name_in_redirect

本配置,在多台vps下面均完美通过测试。

如果在配置过程中遇到什么问题,可以记录下来。然后去google上找一下问题答案,也可以给我email.

因为我己经测试过,所以可以保证提供的配置是完全正确的。出现问题,请检查是不是正确的输入了配置文字。

更新记录:

2010年04月26日

增加mysql的最新可用源,并优化和调整mysql的配置, 更新了nginx最新的源

2010年04月21日

增加了Expires缓存控制,把图片,css,js等不动的东西,最大可能缓存。

2010年04月18日

减少mysql查询缓存大小,减少mysql的内存占用

优化php-fpm的配置,提升性能,降低负载

2010年04月17日

增加mysql的配置

修改fcgi为tcp协议,据观察这种方式比较稳定。

设置fpm的最大可执行时间,减轻大量并发负载下面服务器的压力。

2010年04月16日

第一版完稿

你可能感兴趣的:(nginx,PHP,mysql,wordpress,ubuntu)