[TOC]

这个文档是我们小组制定的论坛搭建生产机环境文档,服务过很大一部分中国的网站,长期积累下来安全可靠。

适用于Centos 5,Centos 6的32和64位版本

一、系统约定

软件源代码包存放位置 /usr/local/src

源码包编译安装位置(prefix) /usr/local/software_name

MySQL 数据库位置 /data/mysql(可按情况设置)

网站根目录 /data/wwwroot/html(可按情况设置)

日志根目录 /data/logs(可按情况设置)

运行账户 www:www

二、系统环境部署及调整

  1. 检查系统是否正常

more /var/log/messages

(检查有无系统级错误信息)

dmesg

(检查硬件设备是否有错误信息)

ifconfig

(检查网卡设置是否正确)

ping www.163.com

(检查网络是否正常)

cat /proc/cpuinfo

(检查CPU频率是否正常)

top

(按1检测CPU核数是否正常,内存大小是否正常)

  1. 关闭SElinux:

setenforce 0

修改/etc/selinux/config文件中的SELINUX= 为 disabled

  1. 服务器时间检查和设置

date

(检查时间是否正确,是否是中国时间CST)

cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

(如果时区不对,则执行,时间正常的跳过。提示是否覆盖的时候按y)

yum -y install ntp

(安装ntp对时工具)

ntpdate 210.72.145.44

(对时一次,纠正当前的时间)

service ntpd start

(启动对时服务)

chkconfig ntpd on

(让对时服务开机启动)

  1. 使用 yum 对系统进行更新并且安装必要软件包

yum update -y

yum install make openssl openssl-devel pcre pcre-devel libpng libpng-devel libjpeg-6b libjpeg-devel-6b freetype freetype-devel gd gd-devel zlib zlib-devel gcc gcc-c++ libXpm libXpm-devel ncurses ncurses-devel libxml2 libxml2-devel imake autoconf automake screen sysstat compat-libstdc++-33 curl curl-devel flex bison -y
  1. 重新启动系统

init 6

三、编译安装L.N.M.P环境

  1. 下载软件(截止到12年12月的最新版本)

cd /usr/local/src
wget http://download.comsenz.com/ComsenzEXP/file/filelist.txt
wget -i filelist.txt
  1. 安装MySQL 5.5.28

提示:如果机器上已有自带的MySQL版本,最好先卸载自带的MySQL,以防以后使用的时候混淆。 卸载命令:

yum remove mysql

如果不确定机器上是否自带有MySQL,也一样可以执行下卸载命令无妨。

/usr/sbin/groupadd mysql

(如果有提示已存在,并不是错误)

/usr/sbin/useradd -g mysql mysql

(如果有提示已存在,并不是错误)

mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql

cd /usr/local/src

tar zxvf cmake-2.8.9.tar.gz

cd cmake-2.8.9

./configure

gmake

gmake install

gmake clean

cd /usr/local/src

tar zxvf mysql-5.5.28.tar.gz

cd mysql-5.5.28

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc/
make
make install

make clean

cd /usr/local/mysql
cp ./support-files/my-huge.cnf /etc/my.cnf

编辑/etc/my.cnf 在 [mysqld] 段增加

datadir = /data/mysql
wait-timeout = 30
max_connections = 512
max_connect_errors = 10000000
default-storage-engine=MyISAM

在 [mysqld] 段修改

max_allowed_packet = 16M
./scripts/mysql_install_db --user=mysql
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod 755 /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 3 mysqld on

编辑 /etc/rc.d/init.d/mysqld 把basedir=编辑成basedir=/usr/local/mysql 把datadir=编辑成datadir=/data/mysql

service mysqld start
bin/mysqladmin -u root password 'password_for_root'

其中引号内的password_for_root是要设置的root密码

service mysqld restart
  1. 安装Nginx

cd /usr/local/src/

tar zxvf nginx-1.2.4.tar.gz

cd nginx-1.2.4

./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --with-http_realip_module --with-http_addition_module --with-http_gzip_static_module --with-http_random_index_module --with-http_stub_status_module --with-http_sub_module --with-http_dav_module

make

make install

make clean

cd /usr/local/src/

cp nginx /etc/init.d/nginx

chmod +x /etc/init.d/nginx

chkconfig --add nginx

mv -f nginx.conf /usr/local/nginx/conf/nginx.conf

groupadd www

useradd -g www www

mkdir -p /data/logs/nginx

mkdir -p /data/wwwroot/html

service nginx start

worker_processes 的数值一般设置为内核数。使用

cat /proc/cpuinfo |grep processor

查看核数

  1. 安装PHP 5.3.18

cd /usr/local/src

tar zxvf php-5.3.18.tar.gz

cd php-5.3.18

./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5/etc --enable-mbstring --enable-ftp --with-gd --with-jpeg-dir=/usr --with-png-dir=/usr --enable-magic-quotes --with-mysql=/usr/local/mysql --with-pear --enable-sockets --with-freetype-dir=/usr --enable-gd-native-ttf --with-zlib --enable-sysvsem --enable-sysvshm --with-libxml-dir=/usr --with-xmlrpc --enable-zip --enable-fpm --enable-bcmath --with-mysqli=/usr/local/mysql/bin/mysql_config --with-openssl --with-gettext --with-curl=/usr/local/lib

make

make install

make clean

cd /usr/local/src

cp php.ini /usr/local/php5/etc/php.ini

cp php-fpm /etc/init.d/

chmod +x /etc/init.d/php-fpm

chkconfig --add php-fpm

cp php-fpm.conf /usr/local/php5/etc/php-fpm.conf

mkdir -p /data/log/php-fpm/

一般php-fpm进程占用20~30m左右的内存就按30m算。如果单独跑php-fpm,动态方式起始值可设置物理内存Mem/30M,由于大家一般Nginx、MySQL都在一台机器上,于是预留一半给它们,即php-fpm进程数为$Mem/2/30

动态方式时,987M内存:

pm = dynamic
pm.max_children =24 #静态下子进程数量
pm.start_servers =16 #动态起始数量
pm.min_spare_servers =12 #动态最少数量
pm.max_spare_servers =24 #动态最大数量

PHP-FPM 是阻塞的单线程模型,pm.max_children 指定的是最大的进程数量,pm.max_requests 指定的是每个进程处理多少个请求后重启(因为 PHP 偶尔会有内存泄漏,所以需要重启). PHP-FPM 的每个进程也只有一个线程,但是一个进程同时只能服务一个客户端。

能并发执行的FPM个数不会超过CPU个数,但是如果max_children个数设置过少,nginx请求到php的时候所有work都在运行,则会直接502,所以要稍多一点work,把请求hang住。

  1. 安装APC缓存

cd /usr/local/src

tar zxvf APC-3.1.9.tgz

cd APC-3.1.9

/usr/local/php5/bin/phpize

./configure --enable-apc --enable-mmap --enable-apc-spinlocks --disable-apc-pthreadmutex --with-php-config=/usr/local/php5/bin/php-config

make

make install

make clean

service php-fpm start
  1. 测试访问

在/data/wwwroot/html放入一个index.php,内容为

<?php phpinfo(); ?>

保存后打开浏览器访问,即可看到phpinfo页面。表示安装成功。

  1. 防火墙设置

编辑/usr/local/sbin/fw.sh

复制以下内容进去

#!/bin/bash

# Stop iptables service first

service iptables stop

# Load FTP Kernel modules

/sbin/modprobe ip_conntrack_ftp

/sbin/modprobe ip_nat_ftp

# Inital chains default policy

/sbin/iptables -F -t filter

/sbin/iptables -P INPUT DROP

/sbin/iptables -P OUTPUT ACCEPT

# Enable Native Network Transfer

/sbin/iptables -A INPUT -i lo -j ACCEPT

# Accept Established Connections

/sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# ICMP Control

/sbin/iptables -A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT

# WWW Service

/sbin/iptables -A INPUT -p tcp --dport 80 -j ACCEPT

# FTP Service

/sbin/iptables -A INPUT -p tcp --dport 21 -j ACCEPT

# SSH Service

/sbin/iptables -A INPUT -p tcp --dport 22 -j ACCEPT

退出编辑,执行以下命令

chmod 755 /usr/local/sbin/fw.sh

echo '/usr/local/sbin/fw.sh' >> /etc/rc.local

/usr/local/sbin/fw.sh

资料&问题

  1. ARM下mysql安装失败

make[2]: *** [libmysql/CMakeFiles/clientlib.dir/__/sql-common/client_plugin.c.o] Error 1

在mysql-5.5.8/sql-common/client_plugin.c中修改,+代表新增,-代表删除

Now, gcc will emit a warning about non initialised stuff on arm but I found no way to
create properly an empty va_list.

Signed-off-by: Arnaud Patard <arnaud.patard@rtp-net.org>

# Index: mysql-5.5.8/sql-common/client_plugin.c

--- mysql-5.5.8.orig/sql-common/client_plugin.c
+++ mysql-5.5.8/sql-common/client_plugin.c
@@ -233,6 +233,7 @@ int mysql_client_plugin_init()
 {
   MYSQL mysql;
   struct st_mysql_client_plugin **builtin;

- va_list dummy;

  if (initialized)
    return 0;
  @@ -249,7 +250,7 @@ int mysql_client_plugin_init()
  pthread_mutex_lock(&LOCK_load_client_plugin);

  for (builtin= mysql_client_builtins; *builtin; builtin++)

- add_plugin(&mysql, *builtin, 0, 0, 0);

- add_plugin(&mysql, *builtin, 0, 0, dummy);

   pthread_mutex_unlock(&LOCK_load_client_plugin);

@@ -293,6 +294,7 @@ struct st_mysql_client_plugin *
 mysql_client_register_plugin(MYSQL *mysql,
                              struct st_mysql_client_plugin *plugin)
 {

- va_list dummy;
  if (is_not_initialized(mysql, plugin->name))
    return NULL;

@@ -307,7 +309,7 @@ mysql_client_register_plugin(MYSQL *mysq
     plugin= NULL;
   }
   else

- plugin= add_plugin(mysql, plugin, 0, 0, 0);

- plugin= add_plugin(mysql, plugin, 0, 0, dummy);

   pthread_mutex_unlock(&LOCK_load_client_plugin);
   return plugin;

http://sophie.zarb.org/distrib/Mageia/1/i586/media/core-updates-src/by-pkgid/3d9c17c564d30ead0a0dab9855d22f9d/files/27

  1. ubutun debian 下安装apt-get update

apt-get install nginx
apt-get install php5-fpm
apt-get install php5-cli
apt-get install php5-curl
apt-get install php5-gd
apt-get install php5-mcrypt
apt-get install php5-mysql
apt-get install php5-cgi
apt-get install mysql-server

http://cb.e-fly.org:81/archives/cb-rpi-server-lnmp.html

  1. 卸载

apt-get --purge remove mysql-client mysql-server mysql-common
apt-get autoremove
  • –purge : Remove given packages and config files.
  • remove : Uninstall packages.
  • autoremove : Force to remove packages that were automatically installed to satisfy dependencies for other packages and are now no longer needed.