Debian安装配置nginx,php,mysql,wordpress教程

这段时间开始折腾VPS(Virtual Private Server 虚拟专用服务器),以前博客一直是用虚拟主机,因为我的虚拟主机所在的服务器是电信的,而我的宽带是网通的,有时上传点东西速度实在是不敢恭维,而且国内提供虚拟主机服务的公司真的是抠门到极点了,ftp连mp3都无法上传(我估计是对mp3类型的文件做了上传限制),使用过程中还经常掉线,总之是很不稳定,访问速度也不怎么样.和国外的服务相比性价比真的不怎么样.这几天正在转出域名,整个过程让我对国内的IDC服务完全失望.

我现在用的VPS主机是基于OpenVZ 技术.现在VPS用的虚拟化技术主要有Xen和OpenVZ两种.Xen是半虚拟化,基本上接近硬件环境,自由度也非常大,可以实现自定义内核配置,基于这种技术的VPS基本不用担心超卖的问题,在主机上用free -m命令查到的内存显示多少就是多少;OpenVZ 是操作系统虚拟化,效率会高一些,但会有超卖的问题,这个时候只能碰运气了,只要不是碰上无良的商家.我现在用的是小众软件的HelloHost项目提供的服务,应该是我所知道现在国内最低价的VPS服务,运行速度也不错.

用SSH连接上线后VPS用起来和真是的服务器差不多,有root权限可以自定义安装软件.只是最初的配置会比较麻烦,基本上就是自己去部署一台服务器,也算是一种锻炼吧.我现在的硬件配置如下:
256M内存
OpenVZ VPS
无交互分区
硬盘空间 15G
月流量 150G

只要稍微做一点优化,扛住200~300人同时在线一点问题也没有(估计我的网站再过几年也达不到这个级别),即使建十个八个这样的个人主页也是绰绰有余.

上网查了一些资料,我选择用nginx + php-fpm + mysql部署我的web服务器.我并不喜欢网上的那些一键安装包,我有比较强的控制欲,就像以前有个女生所我占有欲很强一样.所以部署服务器我还是喜欢一步一步自己操作.

HelloHost的VPS可供选择的操作系统有CentOS、Debain、Ubuntu、Fedora、Gentoo.我的选择Debian,因为Debian很稳定,我也比较熟悉,我自己的笔记本上装的就是Debian,相对其他几个它非常的节约内存.

1.准备工作

首先先卸载VPS上已经安装的mysql和apache2等相关软件包,这样可以避免一些不必要的错误.

apt-get remove --purge  apache2* mysql*
apt-get update
apt-get upgrade

因为php5-fpm现在还没有进入官方源,所以安装php5-fpm需要添加来自dotdeb的第三方源,编辑/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

php5-fpm依赖的一些软件包在lenny里面还没有,所以要把sources.list里面的lenny都替换成stable,记住现在千万别用apt-get upgrade命令更新系统.

安装dotdeb源的Key文件

gpg --keyserver keys.gnupg.net --recv-key 89DF5277
gpg -a --export 89DF5277 |  apt-key add -
apt-get update

2.安装&&配置php5-fpm

对于php5首先要安装php5-cgi否则会自动安装上apache2的相关依赖,所以要分别执行以下命令来安装相关软件

apt-get install php5-cgi
apt-get install php5-cli php5-common php5-suhosin php5-fpm php5-mysql php5-gd
#以上这些都是wordpress需要的php模块,如果你的wordpress还需要其他的,可以自行安装

对于自定义配置,我不是很拿手,这里我采用的是 极致运维 博客的配置文件.

编辑 /etc/php5/fpm/php.ini ,用以下配置文件替换原来默认文件

[PHP]
engine = On
short_open_tag = On
asp_tags = Off
precision = 14
y2k_compliance = On
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = 100
allow_call_time_pass_reference = Off
safe_mode = Off
safe_mode_gid = Off
safe_mode_include_dir =
safe_mode_exec_dir =
safe_mode_allowed_env_vars = PHP_
safe_mode_protected_env_vars = LD_LIBRARY_PATH
disable_functions =
disable_classes =
expose_php = Off
max_execution_time = 60
exit_on_timeout = On
max_input_time = 60
memory_limit = 32M
error_reporting = E_ALL
display_errors = On
display_startup_errors = Off
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = Off
html_errors = Off
variables_order = "GPCS"
request_order = "GP"
register_globals = Off
register_long_arrays = Off
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 16M
magic_quotes_gpc = Off
magic_quotes_runtime = Off
magic_quotes_sybase = Off
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
doc_root =
user_dir =
enable_dl = Off
cgi.fix_pathinfo=1
file_uploads = On
upload_max_filesize = 16M
max_file_uploads = 25
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60
[Date]
date.timezone = "Asia/Shanghai"
date.default_latitude = 31.5167
date.default_longitude = 121.4500
[filter]
[iconv]
[intl]
[sqlite]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.cache_size = 2000
pdo_mysql.default_socket=
[Phar]
[Syslog]
define_syslog_variables  = Off
[mail function]
SMTP = localhost
smtp_port = 25
sendmail_path = "/usr/sbin/sendmail -t -i "
mail.add_x_header = Off
[SQL]
sql.safe_mode = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[Interbase]
ibase.allow_persistent = 1
ibase.max_persistent = -1
ibase.max_links = -1
ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
ibase.dateformat = "%Y-%m-%d"
ibase.timeformat = "%H:%M:%S"
[MySQL]
mysql.allow_local_infile = On
mysql.allow_persistent = On
mysql.cache_size = 2000
mysql.max_persistent = -1
mysql.max_links = -1
mysql.default_port =
mysql.default_socket =
mysql.default_host =
mysql.default_user =
mysql.default_password =
mysql.connect_timeout = 60
mysql.trace_mode = Off
[MySQLi]
mysqli.max_persistent = -1
mysqli.allow_persistent = On
mysqli.max_links = -1
mysqli.cache_size = 2000
mysqli.default_port = 3306
mysqli.default_socket =
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = Off
[OCI8]
[PostgresSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0
[Sybase-CT]
sybct.allow_persistent = On
sybct.max_persistent = -1
sybct.max_links = -1
sybct.min_server_severity = 10
sybct.min_client_severity = 10
[bcmath]
bcmath.scale = 0
[browscap]
[Session]
session.save_handler = files
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.bug_compat_42 = Off
session.bug_compat_warn = Off
session.referer_check =
session.entropy_length = 0
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.hash_function = 0
session.hash_bits_per_character = 5
url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=fakeentry"
[MSSQL]
mssql.allow_persistent = On
mssql.max_persistent = -1
mssql.max_links = -1
mssql.min_error_severity = 10
mssql.min_message_severity = 10
mssql.compatability_mode = Off
mssql.secure_connection = Off
[Assertion]
[COM]
[mbstring]
[gd]
[exif]
[Tidy]
tidy.clean_output = Off
[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit = 5
[sysvshm]
[ldap]
ldap.max_links = -1
[mcrypt]
[dba]

编辑 /etc/php5/fpm/php5-fpm.conf 用以下文本替换

[global]
pid = run/php5-fpm.pid
emergency_restart_threshold = 10
emergency_restart_interval = 1m
process_control_timeout = 60
[www]
listen = /dev/shm/php-cgi.sock
listen.allowed_clients = 127.0.0.1
user = www-data
group = www-data
pm = static
pm.max_children = 2
pm.max_requests = 360
request_terminate_timeout = 60
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

3.安装&&配置 nginx

apt-get install nginx
#关于软件,我觉得真的没有必要盲目的去追求最新版,源里面自带的都是非常稳定的,而且新版的一些特性我们更本就用不上

新建 /etc/nginx/common.conf 文件,输入以下内容

location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
    expires max;
    break;
}
 
location ~ .*\.php$
{
  fastcgi_pass_request_body off;
  client_body_temp_path /tmp/client_body_temp;
  client_body_in_file_only clean;
  fastcgi_param REQUEST_BODY_FILE $request_body_file;
  fastcgi_pass unix:/dev/shm/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;
    }
}

建立/tmp/client_body_temp 目录,用于存放上传的临时文件.

mkdir /tmp/client_body_temp
chown -R www-data:www-data /tmp/client_body_temp
chmod -R 666 /tmp/client_body_temp

编辑 /etc/nginx/nginx.conf 用以下内容替换

user www-data;
worker_processes  8;
worker_cpu_affinity 01 10 01 10 01 10 01 10;
 
error_log  /var/log/nginx/error_log crit;
pid        /var/run/nginx.pid;
worker_rlimit_nofile 10240;
events
{
  use epoll;
  worker_connections  10240;
}
http
{
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;
  server_name_in_redirect off;
  server_names_hash_bucket_size 128;
  server_tokens off;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
  sendfile        on;
  tcp_nopush     on;
  keepalive_timeout 65;
  tcp_nodelay        off;
  client_body_timeout 10;
  client_header_timeout 10;
  send_timeout 60;
  output_buffers 1 32k;
  postpone_output 1460;
  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_buffers     4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types       text/plain application/x-javascript text/css application/xml;
  include /etc/nginx/sites-enabled/*;
}

编辑 /etc/nginx/sites-availale/default 用以下文本覆盖(域名和路径部分要注意修改成自己的)

server {
    listen   80;
    server_name whchen.net;
    location / {
        index index.html index.htm index.php;
        root /var/www/cwh_blog;
        if (!-e $request_filename){
            rewrite ^(.*) /blog/index.php;
#我的wordpress安装在更目录的blog文件夹下,
#所以rewrite的路径是/blog/index.php,这个在固定链接设置里会有用,
#以及找不到文件时可以避免403错误
        }
    }
    include /etc/nginx/common.conf;
}
#如果有多个域名使用这个VPS复制以上文本,再修改域名相关的几行就可以了

编辑 /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.安装&&配置mysql-server

安装mysql-server

apt-get install mysql-server

编辑 /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            = 0.0.0.0
key_buffer              = 32K
max_allowed_packet      = 1M
table_cache = 4
sort_buffer_size = 128K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
thread_stack            = 64K
thread_cache_size       = 8
myisam-recover         = BACKUP
query_cache_limit       = 128K
query_cache_size        = 256K
expire_logs_days        = 5
max_binlog_size         = 8M
skip-innodb
[mysqldump]
quick
quote-names
max_allowed_packet      = 16M
[mysql]
[isamchk]
key_buffer              = 16M
!includedir /etc/mysql/conf.d/

5.建立网站目录&&测试

建立 /var/www/cwh_blog 目录(这个和default文件里的设置有关,只要两个能对应起来,这个目录在那里都可以),比高修改权限.

mkdir -p /var/www/cwh_blog
chown -R www-data:www-data /var/www/cwh_blog
chmod -R 777 /var/www/cwh_blog

在/var/www/cwh_blog目录下建立index.php文件,里面只需要一行文本

重新启动程序

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

在浏览器中打开你的VPS,如果能显示php相关信息,那就大功告成了.

6.安装PHP加速器eAccelerator

eAccelerator是一个自由开放源码php加速器,优化和动态内容缓存,提高了性能php脚本的缓存性能,使得PHP脚本在编译的状态下,对服务器的开销几乎完全消除. 它还有对脚本起优化作用,以加快其执行效率.使您的PHP程序代码执效率能提高1-10倍.

因为eAccelerator在官方源里还没有,所以Debian暂时只能编译安装

#安装编译所需软件包,其他所需的他会自动安装上的
apt-get install php5-dev
从官方网站下载源代码
wget http://bart.eaccelerator.net/source/0.9.6.1/eaccelerator-0.9.6.1.tar.bz2
tar jxf eaccelerator-0.9.6.1.tar.bz2
cd eaccelerator-0.9.6.1
phpize
./configure
make
make install

编辑/etc/php5/fpm/conf.d/eaccelerator.ini 覆盖以下文本

zend_extension="/usr/lib/php5/20090626+lfs/eaccelerator.so"
eaccelerator.shm_size="24"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"

配置参数的详细信息,可以搜索《eAccelerator 配置参数详解》了解到.这里基本按默认设置.

建立临时目录,并重启

mkdir /tmp/eaccelerator
chown -R www-data:www-data /tmp/eaccelerator
chmod -R 777 /tmp/eaccelerator
/etc/init.d/php5-fpm restart

到这里就真的大功告成了,如果对性能要求不是很高的话,第六步是可以不用做的.本文的配置文件大多来自 极致运维 博客,并做了少量的修改.

如果你也在考虑迁移到VPS,而现在还没有找到合适的VPS提供商,我建议你使用Hello Host的VPS,价钱对于个人站长还是比较公道的,服务页不错,如果你要注册的话可以通过这个链接http://hhmembers.net/aff.php?aff=424,当然我也能为此得到一点点的报酬!

如果文章有什么错误,欢迎指正,互相交流大家才能共同进步.

» 本博客采用署名 2.5 中国大陆许可协议进行许可,本文版权归作者所有,欢迎转载,但必须在明显位置给出原文连接。
anyShare分享到:
发表评论?

0 条评论。

发表评论

注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>