1搭建nginx需要的安装包 -----------------------------------------------------------------------2
2前期准备--------------------------------------------------------------------------------------------2
2.1 检测 -----------------------------------------------------------------------------------------2
2.2安装libiconv-1.13--------------------------------------------------------------------------2
2.3安装libmcrypt-2.5.8.----------------------------------------------------------------------2
2.4安装mhash-0.9.9.9-------------------------------------------------------------------------2
2.5安装mcrypt-2.6.8--------------------------------------------------------------------------3
3安装mysq------------------------------------------------------------------------------------------3
3.1创建用户和组--------------------------------------------------------------------------------3
3.2安装编译--------------------------------------------------------------------------------------3
3.3创建MySQL数据库存放目录------------------------------------------------------------3
3.4创建my.cnf配置文件-----------------------------------------------------------------------3
3.5创建管理MySQL数据库的shell脚本---------------------------------------------------5
3.6创建账户,赋予关闭的权限----------------------------------------------------------------6
4安装编译php----------------------------------------------------------------------------------------6
4.1 编译php---------------------------------------------------------------------------------------6
4.2编译安装PHP扩展模块--------------------------------------------------------------------7
4.2.1编译memcache--------------------------------------------------------------------------7
4.2.2编译eaccelerator-0.9.5.3--------------------------------------------------------------7
4.2.4编译ImageMagick----------------------------------------------------------------------7
4.2.5编译imagick-2.2.2-----------------------------------------------------------------------7
4.3 修改php配置文件------------------------------------------------------------------------7
4.4配置eAccelerator加速PHP--------------------------------------------------------------8
4.5创建www用户和组------------------------------------------------------------------------8
4.6创建虚拟机目录-----------------------------------------------------------------------------8
4.7创建php-fpm配置文件--------------------------------------------------------------------8
4.8启动php-cgi进程---------------------------------------------------------------------------12
5安装编译nginx-----------------------------------------------------------------------------------12
5.1安装Nginx所需的pcre库---------------------------------------------------------------12
5.2安装Nginx----------------------------------------------------------------------------------12
5.3建Nginx配置文件-------------------------------------------------------------------------12
5.4启动Nginx-----------------------------------------------------------------------------------12
5.5配置开机自动启动Nginx + PHP--------------------------------------------------------13
5.6优化Linux内核参数-----------------------------------------------------------------------13
5.7在不停止Nginx服务的情况下平滑变更Nginx配置--------------------------------13
5.7.1检查配置文件---------------------------------------------------------------------------13
5.7.2 nginx进程--------------------------------------------------------------------------------14
5.8编写每天定时切割Nginx日志的脚本---------------------------------------------------14
5.8.1创建脚本---------------------------------------------------------------------------------14
5.8.2添加自动任务----------------------------------------------------------------------------14
6. 让php支持mssql通常需要使用组件freeTDS
1、搭建nginx需要的安装包
放在219.234.83.29服务器上
cd /home/chenjl/software/nginx0.8_basepacage
2、前期准备
2.1需要安装一些常用的包,在安装服务器时选择安装开发工具,会安装一些常用的包,如果安装了yum,可以用检测一下
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
2.2安装libiconv-1.13.tar.gz
tar zxvf libiconv-1.13.tar.gz
cd libiconv-1.13/
./configure --prefix=/usr/local
make
make install
2.3安装libmcrypt-2.5.8.tar.gz
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
2.4安装mhash-0.9.9.9.tar.gz
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
2.5安装mcrypt-2.6.8.tar.gz
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure
make
make install
3、安装mysql
3.1 创建用户和组
/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql
3.2 安装编译
tar zxvf mysql-5.1.38.tar.gz
cd mysql-5.1.38/
./configure --prefix=/usr/local/mysql/ --enable-assembler --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile --with-plugins=innobase
make && make install
chmod +w /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
mkdir -p /usr/local/mysqldata/
3.3创建MySQL数据库存放目录
chown -R mysql:mysql /usr/local/mysql/data/
/usr/local/mysql/bin/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql /data --user=mysql
3.4创建my.cnf配置文件
输入以下内容:
###############################
[client]
default-character-set = utf8
port = 3306
socket = /tmp/mysql.sock
[mysql]
prompt="(\g.s135.com:)[\\d]> "
no-auto-rehash
[mysqld]
#default-character-set = utf8
user = mysql
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
open_files_limit = 10240
back_log = 600
max_connections = 3000
max_connect_errors = 6000
table_cache = 614
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 300
thread_concurrency = 8
query_cache_size = 32M
query_cache_limit = 2M
query_cache_min_res_unit = 2k
default-storage-engine = MyISAM
default_table_type = MyISAM
thread_stack = 192K
transaction_isolation = READ-COMMITTED
tmp_table_size = 246M
max_heap_table_size = 246M
long_query_time = 1
log_long_format
log-bin = /data0/mysql/3306/binlog
binlog_cache_size = 4M
binlog_format = MIXED
max_binlog_cache_size = 8M
max_binlog_size = 512M
expire_logs_days = 7
key_buffer_size = 256M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_max_extra_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover
skip-name-resolve
master-connect-retry = 10
slave-skip-errors = 1032,1062,126,1114,1146,1048,1396
server-id = 1
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 2048M
innodb_data_file_path = ibdata1:1024M: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
[mysqldump]
quick
max_allowed_packet = 32M
以上数据,根据需要做修改;
3.5创建管理MySQL数据库的shell脚本
#!/bin/sh
mysql_port=3306
mysql_username="chenjl"
mysql_password="cjl1981"
function_start_mysql()
{
printf "Starting MySQL...\\n"
/bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/my.cnf 2>&1 > /dev/null &
}
function_stop_mysql()
{
printf "Stoping MySQL...\\n"
/usr/local/mysql/bin/mysqladmin -u ${mysql_username} -p${mysql_password} -S /tmp/mysql.sock shutdown
}
function_restart_mysql()
{
printf "Restarting MySQL...\\n"
function_stop_mysql
sleep 5
function_start_mysql
}
function_kill_mysql()
{
kill -9 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port} | awk '{printf $2}')
kill -9 $(ps -ef | grep 'libexec/mysqld' | grep ${mysql_port} | awk '{printf $2}')
}
if [ "$1" = "start" ]; then
function_start_mysql
elif [ "$1" = "stop" ]; then
function_stop_mysql
elif [ "$1" = "restart" ]; then
function_restart_mysql
elif [ "$1" = "kill" ]; then
function_kill_mysql
else
printf "Usage: /data0/mysql/${mysql_port}/mysql {start|stop|restart|kill}\\n"
fi
chmod 755 /usr/local/mysql/bin/mysql.sh
3.6 创建账户,赋予关闭的权限
/usr/local/mysql/bin/mysql -u root -p -S /tmp/mysql.sock
GRANT ALL PRIVILEGES ON *.* TO 'chenjl'@'localhost' IDENTIFIED BY 'cjl1981';
GRANT ALL PRIVILEGES ON *.* TO 'chenjl'@'127.0.0.1' IDENTIFIED BY 'cjl1981;
4、安装编译php
4.1 编译php
tar zxvf php-5.2.10.tar.gz
gzip -cd php-5.2.10-fpm-0.5.11.diff.gz | patch -d php-5.2.10 -p1
cd php-5.2.10/
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/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 --without-pear –with-pdo-mysql=/usr/local/mysql
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-dist /usr/local/php/etc/php.ini
curl http://pear.php.net/go-pear | /usr/local/php/bin/php
4.2编译安装PHP扩展模块
4.2.1 编译memcache
tar zxvf memcache-2.2.5.tgz
cd memcache-2.2.5/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
4.2.2 编译eaccelerator-0.9.5.3
tar jxvf eaccelerator-0.9.5.3.tar.bz2
cd eaccelerator-0.9.5.3/
/usr/local/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
make
make install
4.2.4编译ImageMagick
tar zxvf ImageMagick.tar.gz
cd ImageMagick-6.5.1-2/
./configure tar zxvf imagick-2.2.2.tgz
make
make instal
4.2.5 编译imagick-2.2.2
tar zxvf imagick-2.2.2.tgz
cd imagick-2.2.2/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
4.3 修改php配置文件
手工修改:查找/usr/local/php/etc/php.ini中的extension_dir = "./"
修改为extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
并在此行后增加以下几行,然后保存:
extension = "memcache.so"
extension = "imagick.so"
再查找output_buffering = Off
修改为output_buffering = On
4.4配置eAccelerator加速PHP
mkdir -p /usr/local/eaccelerator_cache
vi /usr/local/php/etc/php.ini
按shift+g键跳到配置文件的最末尾,加上以下配置信息:
[eaccelerator]
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size=""
eaccelerator.cache_dir="/usr/local/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"
4.5创建www用户和组
usr/sbin/groupadd www
/usr/sbin/useradd -g www www
4.6创建虚拟机目录
Mkdir /usr/local/www
Chown –R www:www /usr/local/www
4.7创建php-fpm配置文件
All relative paths in this config are relative to php's install prefix Pid file Error log file Log level When this amount of php processes exited with SIGSEGV or SIGBUS ... ... 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. Time limit on waiting child's reaction on signals from master Set to 'no' to debug fpm Name of pool. Used in logs and stats. Address to accept fastcgi requests on. Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket' Set listen(2) backlog 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. Additional php.ini defines, specific to this pool of workers. Unix user of processes Unix group of processes Process manager settings Sets style of controling worker process count. Valid values are 'static' and 'apache-like' 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. Settings group for 'apache-like' pm style Sets the number of server processes created on startup. Used only when 'apache-like' pm_style is selected Sets the desired minimum number of idle server processes. Used only when 'apache-like' pm_style is selected Sets the desired maximum number of idle server processes. Used only when 'apache-like' pm_style is selected 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' The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file '0s' means 'off' The log file for slow requests Set open file desc rlimit Set max core size rlimit Chroot to this directory at the start, absolute path Chdir to this directory at the start, absolute path Redirect workers' stdout and stderr into main error log. If not set, they will be redirected to /dev/null, according to FastCGI specs 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 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. Pass environment variables like LD_LIBRARY_PATH All $VARIABLEs are taken from current environment
注:请将以下的
4.8启动php-cgi进程,监听127.0.0.1的9000端口,进程数为200(如果服务器内存小于3GB,可以只开启个进程),用户为www:
ulimit -SHn 65535
/usr/local/php/sbin/php-fpm start
注:/usr/local/php/sbin/php-fpm还有其他参数,包括:start|stop|quit|restart|reload|logrotate,修改php.ini后不重启php-cgi,重新加载配置文件使用reload。
5、安装编译nginx
5.1安装Nginx所需的pcre库:
tar zxvf pcre-7.9.tar.gz
cd pcre-7.9/
./configure
make && make install
5.2安装Nginx
tar zxvf nginx-0.8.15.tar.gz
cd nginx-0.8.15/
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
5.3建Nginx配置文件
在/usr/local/nginx/conf/目录中创建nginx.conf文件:
在/usr/local/nginx/conf/目录中创建fcgi.conf文件:
5.4启动Nginx
ulimit -SHn 65535
/usr/local/nginx/sbin/nginx
5.5配置开机自动启动Nginx + PHP
vi /etc/rc.local
ulimit -SHn 65535
/usr/local/php/sbin/php-fpm start
/usr/local/nginx/sbin/nginx
5.6优化Linux内核参数
vi /etc/sysctl.conf
# 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
5.7在不停止Nginx服务的情况下平滑变更Nginx配置
5.7.1修改/usr/local/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:
/usr/local/nginx/sbin/nginx -t
如果屏幕显示以下两行信息,说明配置文件正确:
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully
5.7.2这时,输入以下命令查看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/nginx/nginx.pid`
5.8编写每天定时切割Nginx日志的脚本
5.8.1创建脚本/usr/local/nginx/sbin/cut_nginx_log.sh
输入以下内容:
############################################################
#!/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`
############################################################
5.8.2设置crontab,每天凌晨00:00切割nginx访问日志
crontab -e
输入以下内容:
00 00 * * * /bin/bash /usr/local/nginx/sbin/cut_nginx_log.sh
6. 让php支持mssql通常需要使用组件freeTDS
下载地址为:http://www.ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-0.82.tar.gz
freeTDS的安装之前需要先安装 libiconv,安装好libiconv(安装在这里不做具体说明)以后,执行 ln -s /usr/local/lib/libiconv.so* /usr/lib
1、解压 freeTDS
2、编译 ./configure --prefix=/usr/local/freetds --with-tdsver=8.0 --enable-msdblib --enable-dbmfix
3、make && make install
如果没有出错,即安装成功,如果出错,再具体排查吧。
安装好以后,读取中文内容可能会乱码,为了解决乱码问题,我们要稍稍修改一下配置文件
vi /usr/local/freetds/etc/freetds.conf
增加一行:
#解决中文乱码问题
client charset=utf8
保存退出即可。
4.freeTDS安装测试成功以后,其实php的配置已经变得非常简单了,只需要重新编译PHP增加参数:
--with-mssql=/usr/local/freetds
重新编译后,即可使用函数 mssql_connect 进行 mssql的测试。