平台环境
centos 6.2 64位 nginx-1.2.1 php-5.3.14 mysql-5.5.25
一、安装依赖库和开发环境
安装gcc的
错误 提示:kernel-headers >= 2.2.1
解决办法是,打开 /etc/yum.conf 然后找到这行:
exclude=kernel*
修改为:
exclude=
就可以安装 gcc 了。
安装升级所需程序库
- sudo -s
- LANG=C
- 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
64位编译mysql5.5.25a可能会出现各种各样的问题,下面的库也一起安装吧,有部分重复的自己去掉
- yum -y install bison gcc gcc-c++ autoconf automake zlib* libxml* ncurses-devel libtool-ltdl-devel* mysql-devel
三、下载所需程序
- mkdir -p /data0/soft
- cd /data0/soft
- wget http://cn2.php.net/get/php-5.3.14.tar.gz/from/this/mirror
- wget http://www.nginx.org/download/nginx-1.2.1.tar.gz
- wget http://mysql.ntu.edu.tw/Downloads/MySQL-5.5/mysql-5.5.25a.tar.gz
- wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
- wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/libmcrypt-2.5.8.tar.gz
- wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/mcrypt-2.6.8.tar.gz
- wget http://blog.s135.com/soft/linux/nginx_php/mhash/mhash-0.9.9.9.tar.gz
- wget http://pecl.php.net/get/memcache-3.0.6.tgz
- wget http://blog.s135.com/soft/linux/nginx_php/eaccelerator/eaccelerator-0.9.6.1.tar.bz2
- wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz
- wget http://blog.s135.com/soft/linux/nginx_php/imagick/ImageMagick.tar.gz
- wget http://pecl.php.net/get/imagick-2.3.0.tgz
- wget http://www.cmake.org/files/v2.8/cmake-2.8.8.tar.gz
- wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.30.tar.gz
四、安装PHP 5.3.14
- tar zxvf libiconv-1.14.tar.gz
- cd libiconv-1.14/
- ./configure --prefix=/usr/local
- make
- make install
- cd ../
- 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 ../../
- tar zxvf mhash-0.9.9.9.tar.gz
- cd mhash-0.9.9.9/
- ./configure
- make
- make install
- cd ../
- 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
- tar zxvf mcrypt-2.6.8.tar.gz
- cd mcrypt-2.6.8/
- /sbin/ldconfig
- ./configure
- make
- make install
- cd ../
五、编译安装MySQL 5.5.25a
- tar zxvf cmake-2.8.8.tar.gz
- cd cmake-2.8.8
- ./configure
- gmake
- gmake install
- cd ../
64位系统安装cmake后执行cmake命令,如不能找到命令,是环境变量的问题,可到cmake目录再执行cmake命令
或添加环境变量,如能直接执行,此步可省略,设置环境变量# export PATH=/usr/local/bin:$PATH
- /usr/sbin/groupadd mysql
- /usr/sbin/useradd -g mysql mysql
- tar zxvf mysql-5.5.25a.tar.gz
- cd mysql-5.5.25a
- cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/webserver/mysql \
- -DEXTRA_CHARSETS=all \
- -DWITH_READLINE=1 \
- -DWITH_SSL=system \
- -DWITH_EMBEDDED_SERVER=1 \
- -DENABLED_LOCAL_INFILE=1 \
- -DWITH_INNOBASE_STORAGE_ENGINE=1 \
- -DWITHOUT_PARTITION_STORAGE_ENGINE=1
- make
- make intall
- chmod +w /usr/local/webserver/mysql
- chown -R mysql:mysql /usr/local/webserver/mysql
- cd ../
附:以下为附加步骤,如果你想在这台服务器上运行MySQL数据库,则执行以下几步。如果你只是希望让PHP支持MySQL扩展库,能够连接其他服务器上的MySQL数据库,那么,以下两步无需执行。
①、创建MySQL数据库存放目录
- mkdir -p /data0/mysql/3306/data/
- mkdir -p /data0/mysql/3306/binlog/
- mkdir -p /data0/mysql/3306/relaylog/
- chown -R mysql:mysql /data0/mysql/
#设置mysql我这里分两种方案,可以任选其一。
方案一:
#在support-files目录中有五个配置信息文件:这里我们选择内存1-2G的
#my-small.cnf (内存<=64M)
#my-medium.cnf (内存 128M)
#my-large.cnf (内存 512M)
#my-huge.cnf (内存 1G-2G)
#my-innodb-heavy-4G.cnf (内存 4GB)
- cd /usr/local/webserver/mysql
- cp ./support-files/my-huge.cnf /etc/my.cnf
- vi /etc/my.cnf
编辑配置文件,在
- datadir = /data0/mysql/3306/data #添加MySQL数据库路径
- wait-timeout = 30
- max_connections = 512
- default-storage-engine = MyISAM
- max_allowed_packet = 16M
方案二:
关于my.cnf配置文件,我这里贴一个我配置的,仅作参考,可以忽略。如选择此方案,在/etc/my.cnf 里面输入下面的内容
[client]
default-character-set = utf8
port = 3306
socket = /tmp/mysql.sock
[mysqld]
character-set-server = utf8
replicate-ignore-db = mysql
replicate-ignore-db = test
replicate-ignore-db = information_schema
user = mysql
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/webserver/mysql
datadir = /data0/mysql/3306/data
log-error = /data0/mysql/3306/mysql_error.log
pid-file = /data0/mysql/3306/mysql.pid
#默认10240
open_files_limit = 9600
#默认600
back_log = 300
#默认5000
max_connections = 2000
#默认6000
max_connect_errors = 3000
table_cache = 614
table_open_cache = 614
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 1M
join_buffer_size = 1M
#默认300
thread_cache_size = 150
#双核的话设4,4核设8,此处禁止了
#thread_concurrency = 8
#默认512
query_cache_size = 256M
query_cache_limit = 2M
query_cache_min_res_unit = 2k
default-storage-engine = MyISAM
thread_stack = 192K
transaction_isolation = READ-COMMITTED
#默认246
tmp_table_size = 128M
max_heap_table_size = 246M
long_query_time = 3
log-slave-updates
log-bin = /data0/mysql/3306/binlog/binlog
binlog_cache_size = 4M
binlog_format = MIXED
max_binlog_cache_size = 8M
max_binlog_size = 1G
relay-log-index = /data0/mysql/3306/relaylog/relaylog
relay-log-info-file = /data0/mysql/3306/relaylog/relaylog
relay-log = /data0/mysql/3306/relaylog/relaylog
expire_logs_days = 30
#默认256
key_buffer_size = 64M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
#默认64
bulk_insert_buffer_size = 32M
#默认128
myisam_sort_buffer_size = 64M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover
interactive_timeout = 120
wait_timeout = 120
skip-name-resolve
#master-connect-retry = 10
slave-skip-errors = 1032,1062,126,1114,1146,1048,1396
#master-host = 192.168.1.2
#master-user = username
#master-password = password
#master-port = 3306
server-id = 1
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 512M
innodb_data_file_path = ibdata1:256M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 16M
innodb_log_file_size = 128M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0
#log-slow-queries = /data0/mysql/3306/slow.log
#long_query_time = 10
[mysqldump]
quick
max_allowed_packet = 32M
[myisamchk]
#默认256
key_buffer_size = 128M
#默认256
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
以上配置文件仅作1.5G内存主机参考
#生成授权表
- ./scripts/mysql_install_db --basedir=/usr/local/webserver/mysql --datadir=/data0/mysql/3306/data --user=mysql #生成mysql系统数据库
#更改密码
- /usr/local/webserver/mysql/bin/mysqladmin -u root password '12345678'
#开启mysql
- /usr/local/webserver/mysql/bin/mysqld_safe &
#通过命令行登录管理MySQL服务器(提示输入密码时直接回车,上处修改密码了的话这里要输入的):
- /usr/local/webserver/mysql/bin/mysql -u root -p -S /tmp/mysql.sock
- show databases;
- exit;
#设置开机启动
- vi /etc/rc.d/rc.local
#加入
- /usr/local/webserver/mysql/bin/mysqld_safe &
可以暂时停止mysql
/usr/local/webserver/mysql/bin/mysqladmin -u root -p shutdown
//mysql安装到此截止了,php请看下一章
附:常用mysql命令:
(1)查看用户信息
select user,host,password from mysql.user;
(2)输入以下SQL语句,创建一个具有root权限的用户(admin)和密码(12345678):
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' IDENTIFIED BY '12345678';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'127.0.0.1' IDENTIFIED BY '12345678';
(3)创建数据库
/usr/local/webserver/mysql/bin/mysql -u admin -p -S /tmp/mysql.sock
(注意密码是 12345678)
MySQL>create database blog;
MySQL>flush privileges;
查询用户下的数据库
SHOW DATABASES;
查询数据库编码:SHOW VARIABLES LIKE 'character%';
3、编译安装PHP(FastCGI模式)如果是64位操作系统,需要先进行下步工作
如果是64位操作系统,需要先进行下步工作
解决没有安装 patch 工具问题,
解决imagemagick问题
解决 configure: error: libjpeg.(a|so) not found 错误
解决configure: error: libpng.(a|so) not found
解决 configure: error: Cannot find ldap libraries in /usr/lib 错误,上面的错误也可用这种方法解决
- yum install patch
- yum -y install perl-CPAN
- ln -s /usr/lib64/libjpeg.so /usr/lib/libjpeg.so
- ln -s /usr/lib64/libpng.so /usr/lib/libpng.so
- ln -s /usr/lib64/libldap.so /usr/lib/libldap.so
- tar zxvf php-5.3.14.tar.gz
- cd php-5.3.14/
- ln -s /usr/local/webserver/mysql/lib/libmysqlclient.so /usr/lib64/
- ln -s /usr/local/webserver/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18
32位系统应该是下面的,为免出错,可以全做
- ln -s /usr/local/webserver/mysql/lib/libmysqlclient.so /usr/lib/
- ln -s /usr/local/webserver/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18
- ./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-production /usr/local/webserver/php/etc/php.ini
- cd ../
4、编译安装PHP5扩展模块
- tar zxvf memcache-3.0.6.tgz
- cd memcache-3.0.6/
- /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 ../
5、修改php.ini
手工修改:查找/usr/local/webserver/php/etc/php.ini中的找到
找到:;date.timezone =
修改为:date.timezone = PRC #设置时区
找到:expose_php = On
修改为:expose_php = OFF #禁止显示php版本的信息
找到:display_errors = On
修改为:display_errors = OFF #关闭错误提示
查找extension_dir = "./"
修改为extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/"
并在此行后增加以下几行,然后保存:
extension = "memcache.so"
extension = "pdo_mysql.so"
extension = "imagick.so"
查找
short_open_tag = Off
修改
short_open_tag = On
6、配置eAccelerator加速PHP:
- mkdir -p /usr/local/webserver/eaccelerator_cache
- chmod 777 /usr/local/webserver/eaccelerator_cache
- vi /usr/local/webserver/php/etc/php.ini
按shift+g键跳到配置文件的最末尾,加上以下配置信息:
7、创建www用户和组,以及供两个虚拟主机使用的目录:
- /usr/sbin/groupadd www
- /usr/sbin/useradd -g www www
- mkdir -p /data0/htdocs/blog
- chmod +w /data0/htdocs/blog
- chown -R www:www /data0/htdocs/blog
- mkdir -p /data0/htdocs/www
- chmod +w /data0/htdocs/www
- chown -R www:www /data0/htdocs/www
创建php-fpm配置文件(php-fpm是为PHP打的一个FastCGI管理补丁,可以平滑变更php.ini配置而无需重启php-cgi)
- cp /usr/local/webserver/php/etc/php-fpm.conf.default /usr/local/webserver/php/etc/php-fpm.conf
修改
- vi /usr/local/webserver/php/etc/php-fpm.conf
#检查语法是否正确
- /usr/local/webserver/php/sbin/php-fpm -t
#出现NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful 测试成功
启动fpm
- /usr/local/webserver/php/sbin/php-fpm &
相关命令
INT, TERM 立刻终止
QUIT 平滑终止
USR1 重新打开日志文件
USR2 平滑重载所有worker进程并重新载入配置和二进制模块
示例:
php-fpm 关闭:
kill -INT `cat /usr/local/php/var/run/php-fpm.pid`
php-fpm 重启:
kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`
查看php-fpm进程数:
ps aux | grep -c php-fpm
三、安装Nginx 1.2.1
1、安装Nginx所需的pcre库:
- tar zxvf pcre-8.30.tar.gz
- cd pcre-8.30/
- ./configure
- make && make install
- cd ../
2、安装Nginx
- tar zxvf nginx-1.2.1.tar.gz
- cd nginx-1.2.1/
- ./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/data0/soft/pcre-8.30
- make && make install
- cd ../
3、创建Nginx日志目录
- mkdir -p /data0/logs
- chmod +w /data0/logs
- chown -R www:www /data0/logs
4、创建Nginx配置文件
①、在/usr/local/webserver/nginx/conf/目录中创建nginx.conf文件:
- rm -f /usr/local/webserver/nginx/conf/nginx.conf
- vi /usr/local/webserver/nginx/conf/nginx.conf
输入以下内容:
user www www;
worker_processes 8;
error_log /data0/logs/nginx_error.log crit;
pid /usr/local/webserver/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;
server_tokens off;
#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 42.121.83.101;
index index.html index.htm index.php;
root /data0/htdocs/blog;
#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 fcgi.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 /data0/logs/access.log access;
server
{
listen 80;
server_name www.s135.com;
index index.html index.htm index.php;
root /data0/htdocs/www;
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
}
log_format wwwlogs '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /data0/logs/wwwlogs.log wwwlogs;
server
{
listen 80;
server_name status.blog.s135.com;
location / {
stub_status on;
access_log off;
}
}
}
②、在/usr/local/webserver/nginx/conf/目录中创建fcgi.conf文件:
- vi /usr/local/webserver/nginx/conf/fcgi.conf
输入以下内容:
5、启动Nginx
- /usr/local/webserver/nginx/sbin/nginx &
#检测配置文件
- /usr/local/webserver/nginx/sbin/nginx -t
#提示表示成功
#nginx: the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok
#nginx: configuration file /usr/local/webserver/nginx/conf/nginx.conf test is successful
相关命令
#开启Nginx
/usr/local/webserver/nginx/sbin/nginx &
#平滑重启Nginx
/usr/local/webserver/nginx/sbin/nginx -s reload
四、配置开机自动启动Nginx + PHP
- vi /etc/rc.d/rc.local
在末尾增加以下内容:
- /usr/local/webserver/php/sbin/php-fpm &
- /usr/local/webserver/nginx/sbin/nginx &
五、优化Linux内核参数
- vi /etc/sysctl.conf
在末尾增加以下内容:
使配置立即生效:
- /sbin/sysctl -p
六、在不停止Nginx服务的情况下平滑变更Nginx配置
1、修改/usr/local/webserver/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 0.8.x版本,现在平滑重启Nginx配置非常简单,执行以下命令即可:
/usr/local/webserver/nginx/sbin/nginx -s reload
②、对于Nginx 0.8.x之前的版本,平滑重启稍微麻烦一些,按照以下步骤进行即可。输入以下命令
查看Nginx主进程号:
ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'
屏幕显示的即为Nginx主进程号,例如:
6302
这时,执行以下命令即可使修改过的Nginx配置文件生效:
kill -HUP 6302
或者无需这么麻烦,找到Nginx的Pid文件:
kill -HUP `cat /usr/local/webserver/nginx/nginx.pid`
七、编写每天定时切割Nginx日志的脚本
1、创建脚本
- vi /usr/local/webserver/nginx/sbin/cut_nginx_log.sh
输入以下内容:
#!/bin/bash
# This script run at 00:00
# The Nginx logs path
logs_path="/usr/local/webserver/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/webserver/nginx/nginx.pid`
2、设置crontab,每天凌晨00:00切割nginx访问日志
- crontab -e
输入以下内容:
00 00 * * * /bin/bash /usr/local/webserver/nginx/sbin/cut_nginx_log.sh
查看你的定时任务是否加进去了。
- crontab -l