本篇中nginx、mysql、php都会被安装在/usr/local/目录下,它们的配置文件和日志等都在它们的安装目录下;mysql的数据库数据放在/data/mysql下,网站数据放在/data/www下。
系统准备和依赖程序库安装
[c-sharp] view plain copy print?
-
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers libXpm*
yum -y update
下载安装包
[c-sharp] view plain copy print?
-
cd /tmp/
-
wget http:
-
wget http:
-
wget http:
-
wget http:
-
wget http:
-
wget http:
-
wget http:
-
wget http:
-
wget ftp:
-
wget http:
-
wget http:
-
wget http:
安装libiconv、libmcrypt、libltdl、mhash、mcrypt
[c-sharp] view plain copy print?
-
cd /tmp/
-
tar zxvf libiconv-1.13.1.tar.gz
-
cd libiconv-1.13.1/
-
./configure --prefix=/usr/local
-
make
-
make install
-
cd /tmp/
-
tar zxvf libmcrypt-2.5.8.tar.gz
-
cd libmcrypt-2.5.8/
-
./configure
-
make
-
make install
-
/sbin/ldconfig
-
-
cd libltdl/
-
./configure --enable-ltdl-install
-
make
-
make install
-
-
cd /tmp/
-
tar zxvf mhash-0.9.9.9.tar.gz
-
cd mhash-0.9.9.9/
-
./configure
-
make
-
make install
-
ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
-
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
-
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
-
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
-
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
-
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
-
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
-
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
-
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
-
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
-
-
cd /tmp/
-
tar zxvf mcrypt-2.6.8.tar.gz
-
cd mcrypt-2.6.8/
-
/sbin/ldconfig
-
./configure
-
make
-
make install
安装cmake
[c-sharp] view plain copy print?
-
cd /tmp/
-
tar zxvf cmake-2.8.3.tar.gz
-
cd cmake-2.8.3/
-
./configure --prefix=/usr
-
gmake
-
gmake install
-
-
cd ../
-
tar zxvf bison-2.4.3.tar.gz
-
cd cd bison-2.4.3
-
./configure
-
make
-
make install
准备用户、目录
[c-sharp] view plain copy print?
-
groupadd mysql
-
useradd -g mysql mysql
-
groupadd www
-
useradd -g www www
-
mkdir -p /data/mysql
-
chown -R mysql:mysql /data/mysql
-
mkdir -p /data/www
-
chown -R www:www /data/www
安装mysql
编译
[c-sharp] view plain copy print?
-
cd /tmp/
-
tar zxvf mysql-5.5.9.tar.gz
-
cd mysql-5.5.9
-
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DSYSCONFDIR=/etc/ -DWITH_SSL=yes -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_READLINE=on
-
make
-
make install
-
ln -s /usr/local/mysql/bin/mysql /usr/bin
-
ln -s /usr/local/mysql/bin/mysqldump /usr/bin
-
ln -s /usr/local/mysql/bin/mysqladmin /usr/bin
-
ln -s /usr/local/mysql/lib/libmysqlclient.so /usr/lib
-
ln -s /usr/local/mysql/lib/libmysqlclient.so.16 /usr/lib
配置
[c-sharp] view plain copy print?
-
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/data/mysql --user=mysql
-
cd /usr/local/mysql/support-files
ls一下你会看到有下面的文件: my-huge.cnf 【对应1-2G内存】、my-large.cnf 【对应512M内存】
、my-medium.cnf 【对应128M内存】、my-small.cnf 【对应64M或更低】,你可以查看这些文件,参
照你的电脑配置,选择一个文件
cp my-xxx.cnf /etc/my.cnf
编辑文件/etc/my.conf
然后在[mysqld]配置选项下添加
character-set-server = utf8
初始化和启动
[c-sharp] view plain copy print?
-
cp /opt/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
-
chkconfig --add mysqld
-
chkconfig mysqld on
-
/etc/rc.d/init.d/mysqld start
-
cd /opt/mysql/
-
bin/mysql_secure_installation
按照提示设置root密码等
安装php
编译
[c-sharp] view plain copy print?
-
tar zxvf php-5.2.14.tar.gz
-
gzip -cd php-5.2.14-fpm-0.5.14.diff.gz | patch -d php-5.2.14 -p1
-
cd php-5.2.14/
-
./configure --prefix=/usr/local/webserver/php --with-config-file-path=/usr/local/webserver/php/etc --with-mysql=/usr/local/webserver/mysql --with-mysqli=/usr/local/webserver/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --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-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap
-
make ZEND_EXTRA_LIBS='-liconv'
-
make install
-
cp php.ini-dist /usr/local/webserver/php/etc/php.ini
-
cd ../
编译安装PHP5扩展模块
[c-sharp] view plain copy print?
-
tar zxvf memcache-2.2.5.tgz
-
cd memcache-2.2.5/
-
/usr/local/webserver/php/bin/phpize
-
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
-
make
-
make install
-
cd ../
-
-
tar jxvf eaccelerator-0.9.6.1.tar.bz2
-
cd eaccelerator-0.9.6.1/
-
/usr/local/webserver/php/bin/phpize
-
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/webserver/php/bin/php-config
-
make
-
make install
-
cd ../
-
-
tar zxvf PDO_MYSQL-1.0.2.tgz
-
cd PDO_MYSQL-1.0.2/
-
/usr/local/webserver/php/bin/phpize
-
./configure --with-php-config=/usr/local/webserver/php/bin/php-config --with-pdo-mysql=/usr/local/webserver/mysql
-
make
-
make install
-
cd ../
-
-
tar zxvf ImageMagick.tar.gz
-
cd ImageMagick-6.5.1-2/
-
./configure
-
make
-
make install
-
cd ../
-
-
tar zxvf imagick-2.3.0.tgz
-
cd imagick-2.3.0/
-
/usr/local/webserver/php/bin/phpize
-
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
-
make
-
make install
-
cd ../
修改php.ini文件
手工修改:查找/usr/local/webserver/php/etc/php.ini中的extension_dir = "./"
修改为extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"
并在此行后增加以下几行,然后保存:
extension = "memcache.so"
extension = "pdo_mysql.so"
extension = "imagick.so"
再查找output_buffering = Off
修改为output_buffering = On
再查找; cgi.fix_pathinfo=0
修改为cgi.fix_pathinfo=0,防止Nginx文件类型错误解析漏洞。
自动修改:若嫌手工修改麻烦,可执行以下shell命令,自动完成对php.ini文件的修改:
[c-sharp] view plain copy print?
-
sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"/nextension = "memcache.so"/nextension = "pdo_mysql.so"/nextension = "imagick.so"/n#' /usr/local/webserver/php/etc/php.ini
-
sed -i 's#output_buffering = Off#output_buffering = On#' /usr/local/webserver/php/etc/php.ini
-
sed -i "s#; always_populate_raw_post_data = On#always_populate_raw_post_data = On#g" /usr/local/webserver/php/etc/php.ini
-
sed -i "s#; cgi.fix_pathinfo=0#cgi.fix_pathinfo=0#g" /usr/local/webserver/php/etc/php.ini
配置eAccelerator加速PHP:
mkdir -p /usr/local/webserver/eaccelerator_cache
vi /usr/local/webserver/php/etc/php.ini
按shift+g键跳到配置文件的最末尾,加上以下配置信息:
[c-sharp] view plain copy print?
-
[eaccelerator]
-
zend_extension="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
-
eaccelerator.shm_size="64"
-
eaccelerator.cache_dir="/usr/local/webserver/eaccelerator_cache"
-
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"
创建www用户和组,以及供虚拟主机使用的目录:
[c-sharp] view plain copy print?
-
/usr/sbin/groupadd www
-
/usr/sbin/useradd -g www www
-
mkdir -p /home/www
-
chmod +w /home/www
-
chown -R www:www /home/www
创建php-fpm配置文件(php-fpm是为PHP打的一个FastCGI管理补丁,可以平滑变更php.ini配置而无需重启php-cgi):
在/usr/local/webserver/php/etc/目录中创建php-fpm.conf文件:
rm -f /usr/local/webserver/php/etc/php-fpm.conf
vi /usr/local/webserver/php/etc/php-fpm.conf
输入以下内容(如果您安装 Nginx + PHP 用于程序调试,请将以下的<value name="display_errors">0</value>改为<value name="display_errors">1</value>,以便显示PHP错误信息,否则,Nginx 会报状态为500的空白错误页):
启动php-cgi进程,监听127.0.0.1的9000端口,进程数为128(如果服务器内存小于3GB,可以只开启64个进程),用户为www:
ulimit -SHn 65535
/usr/local/webserver/php/sbin/php-fpm start
注:/usr/local/webserver/php/sbin/php-fpm还有其他参数,包括:start|stop|quit|restart|reload|logrotate,修改php.ini后不重启php-cgi,重新加载配置文件使用reload。
安装Nginx 0.9.5
1、安装Nginx所需的pcre库:
[c-sharp] view plain copy print?
-
tar zxvf pcre-8.10.tar.gz
-
cd pcre-8.10/
-
./configure
-
make && make install
-
cd ../
安装nginx
[c-sharp] view plain copy print?
-
tar zxvf nginx-0.9.5.tar.gz
-
cd nginx-0.9.5/
-
Nginx简单优化
-
-
减小 nginx 编译后的文件大小 (Reduce file size of nginx)
-
默认的 nginx 编译选项里居然是用 debug 模式(-g)的(debug 模式会插入很多跟踪和
-
ASSERT 之类) ,编译以后一个 nginx有好几兆。去掉 nginx的 debug 模式编译,编译以
-
后只有几百 K
-
在 auto/cc/gcc,最后几行有:
-
# debug
-
CFLAGS=”$CFLAGS -g”
-
注释掉或删掉这几行
-
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
-
make && make install
-
cd ../
nginx日志目录权限修改 chown -R www:www /usr/local/nginx/logs
创建Nginx配置文件
rm -f /usr/local/nginx/conf/nginx.conf
vi /usr/local/nginx/conf/nginx.conf
[c-sharp] view plain copy print?
-
user www www;
-
-
worker_processes 8;
-
-
error_log /usr/local/nginx/logs/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 65535;
-
-
events
-
{
-
use epoll;
-
worker_connections 65535;
-
}
-
-
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 www.ceshi.com;
-
index index.html index.htm index.php;
-
root /home/www;
-
-
#limit_conn crawler 20;
-
-
location ~ .*/.(php|php5)?$
-
{
-
#fastcgi_pass unix:/tmp/php-cgi.sock;
-
fastcgi_pass 127.0.0.1:9000;
-
fastcgi_index index.php;
-
include fastcgi.conf;
-
}
-
-
location ~ .*/.(gif|jpg|jpeg|png|bmp|swf)$
-
{
-
expires 30d;
-
}
-
-
location ~ .*/.(js|css)?$
-
{
-
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 /usr/local/nginx/logs/access.log access;
-
}
-
}
启动Nginx
ulimit -SHn 65535
/usr/local/webserver/nginx/sbin/nginx
配置开机自动启动Nginx + PHP
vi /etc/rc.local
在末尾增加以下内容:
引用
ulimit -SHn 65535
/usr/local/webserver/php/sbin/php-fpm start
/usr/local/webserver/nginx/sbin/nginx
五、优化Linux内核参数
vi /etc/sysctl.conf
在末尾增加以下内容:
[c-sharp] view plain copy print?
-
# Add
-
net.ipv4.tcp_max_syn_backlog = 65536
-
net.core.netdev_max_backlog = 32768
-
net.core.somaxconn = 32768
-
-
net.core.wmem_default = 8388608
-
net.core.rmem_default = 8388608
-
net.core.rmem_max = 16777216
-
net.core.wmem_max = 16777216
-
-
net.ipv4.tcp_timestamps = 0
-
net.ipv4.tcp_synack_retries = 2
-
net.ipv4.tcp_syn_retries = 2
-
-
net.ipv4.tcp_tw_recycle = 1
-
#net.ipv4.tcp_tw_len = 1
-
net.ipv4.tcp_tw_reuse = 1
-
-
net.ipv4.tcp_mem = 94500000 915000000 927000000
-
net.ipv4.tcp_max_orphans = 3276800
-
-
#net.ipv4.tcp_fin_timeout = 30
-
#net.ipv4.tcp_keepalive_time = 120
-
net.ipv4.ip_local_port_range = 1024 65535
使配置立即生效:
/sbin/sysctl -p
在不停止Nginx服务的情况下平滑变更Nginx配置
1、修改/usr/local/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:
/usr/local/webserver/nginx/sbin/nginx -t
如果屏幕显示以下两行信息,说明配置文件正确:
the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok
the configuration file /usr/local/webserver/nginx/conf/nginx.conf was tested successfully
2、平滑重启:
平滑重启Nginx配置非常简单,执行以下命令即可:
/usr/local/nginx/sbin/nginx -s reload
编写每天定时切割Nginx日志的脚本
1、创建脚本/usr/local/webserver/nginx/sbin/cut_nginx_log.sh
vi /usr/local/webserver/nginx/sbin/cut_nginx_log.sh
输入以下内容:
[c-sharp] view plain copy print?
-
#!/bin/bash
-
# This script run at 00:00
-
-
# The Nginx logs path
-
logs_path="/usr/local/nginx/logs/"
-
-
mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
-
mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
-
kill -USR1 `cat /usr/local/nginx/nginx.pid`
2、设置crontab,每天凌晨00:00切割nginx访问日志
crontab -e
输入以下内容:
引用
00 00 * * * /bin/bash /usr/local/webserver/nginx/sbin/cut_nginx_log.sh
修改文件描述符的方法ulimit -SHn 65535只能对当前用户有效 可以修改/etc/security/limits.conf中的 * soft nofile 65535
* hard nofile 65535
参考文章http://blog.s135.com/nginx_php_v6/
http://woyoo.org/index.php/linux/centos5-5-nginx0-8-54-mysql5-5-8-php5-3-5.html
http://blog.mcncc.com/9028.html
(责任编辑:IT) |