源文是从张宴的博客http://blog.s135.com/nginx_php_v6 来的,其中把程序版本,系统版本升级外,去掉已经过时的和新添加的,
本文系统centos6.3,最小安装,64位
#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-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel openldap-clients openldap-servers
#tar zxvf libiconv-1.14.tar.gz
#cd libiconv-1.14/
#./configure --prefix=/usr/local
#make
#make install
#cd ../
#tar zxvf libmcrypt-2.5.7.tar.gz
#cd libmcrypt-2.5.7/
#./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 ..
下面为mysql5.7版本的安装,一般新版本较旧版都会在功能和bug上面有所修改和升级,5.6以后就改用cmake进行编译,5.7的版对cmake版本要求较高,要2.8以上
# groupadd mysql
# useradd -g mysql mysql
# tar -zxvf cmake-2.8.12.2.tar.gz
# cd cmake-2.8.12.2
# sh bootstrap
# make
# make install
# cd ..
# cd mysql-5.7.4-m14
# mkdir -p /usr/local/mysql/data
# chmod -R 755 /usr/local/mysql/
# chown -R mysql:mysql /usr/local/mysql/
# tar -zxvf mysql-5.7.4-m14.tar.gz
#cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS:STRING=utf8 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL_TCP_PORT=3306 -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_EMBEDDED_SERVER=1 -DENABLED_LOCAL_INFILE=1 -DEXTRA_CHARSETS=complex
#make;make install
# cd /usr/local/mysql/
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
# chmod -R 755 /etc/rc.d/init.d/mysqld
# ./scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data/ --basedir=/usr/local/mysql/
# mv /etc/my.cnf /etc/my.cnf.bak
# cp support-files/my-default.cnf /etc/my.cnf
# mkdir -p /var/log/mysql/
# touch /var/log/mysql/binlog.index
# chmod -R 755 /var/log/mysql/
# chown -R mysql:mysql /var/log/mysql/
#vi /etc/my.cnf
添加下面内容,去掉原配置文件里的东西,注意系统版本区别
[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
tmpdir = /tmp
replicate-ignore-db = mysql
replicate-ignore-db = test
user = mysql
datadir = /usr/local/mysql/data
log-error = /var/log/mysql/mysql_error.log
skip-external-locking ##旧版本为skip-locking
back_log = 50
max_connections = 1000
max_connect_errors = 2000
local-infile = 0
wait_timeout= 2
table_open_cache = 1000
external-locking = 0
max_allowed_packet = 32M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 300
query_cache_size= 64M
query_cache_limit= 1M
query_cache_min_res_unit = 2k
innodb_thread_concurrency=4
default-time-zone = system
character-set-server = utf8
default-storage-engine = InnoDB
thread_stack = 192K
transaction_isolation = READ-COMMITTED
tmp_table_size = 128M
max_heap_table_size = 128M
long-query-time = 2
log-bin = /var/log/mysql/binlog
binlog_cache_size = 2M
binlog_format = ROW
max_binlog_cache_size = 8M
max_binlog_size = 1G
relay-log-index = /var/log/mysql/relaylog
relay-log-info-file =/var/log/mysql/relaylog
relay-log = /var/log/mysql/relaylog
expire_logs_days = 7
key_buffer_size = 256M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 32M
myisam_sort_buffer_size = 64M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam-recover-options #旧版本为myisam-recover
interactive_timeout = 120
wait_timeout = 120
skip-name-resolve
slave-skip-errors = 1032,1062,126,1114,1146,1048,1396
server-id = 1
####新版本已经去掉innodb_additional_mem_pool_size
innodb_buffer_pool_size = 256M
innodb_data_home_dir = /usr/local/mysql/data
innodb_data_file_path = ibdata01:1G: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 = /var/log/mysql/slow.log
long_query_time = 10
explicit_defaults_for_timestamp=true
socket = /tmp/mysql.sock
slow_query_log_file =/var/log/mysql/slow.log ##旧版本为log-slow-queries
[client]
port = 3306
socket = /tmp/mysql.sock
[mysql]
default-character-set=utf8
[mysqldump]
quick
max_allowed_packet = 32M
[mysql]
disable-auto-rehash
default-character-set = utf8
connect-timeout = 3
#######在启动过程中,如果出现the server quit without PID file(usr/local/mysql/data/localhost.pid),一般都是配置文件出错,请查看/var/log/mysql/mysql_error.log,找到标有[ERROR]的一行,一般就是出错的地方;再者每次调试重启的时候,最好rm -rf /usr/local/mysql/data/ib_*
#service mysqld start
#chkconfig mysqld on
下面为PHP编译
#tar -zxvf php-5.5.7.tar.gz
#cd php-5.5.7
#cp -frp /usr/lib64/libldap* /usr/lib/
#mdkir /var/log/php
#ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64
##./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-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --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
--enable-opcache (如果提示--enable-opcache=no,那么先设置--enable-opcache=no,后面自己安装)
#make ZEND_EXTRA_LIBS='-liconv';make install
#cp php.ini-development /usr/local/php/etc/php.ini
######安装memcache时要留意版本,如果php5.5,至少需要memcache2.2.6,如果在安装下面几个组件时,提示config.m4 not found之类的,都是版本太旧了
#tar zxvf memcache-2.2.7.tgz
#cd memcache-2.2.7/
#/usr/local/php/bin/phpize
#./configure --with-php-config=/usr/local/php/bin/php-config
#make;make install
#tar zxvf PDO_MYSQL-1.0.2.tgz
#cd PDO_MYSQL-1.0.2/
#/usr/local/php/bin/phpize
#ln -s /usr/local/mysql/include/* /usr/local/include/
#./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql
#make
#make install
#tar -zxvf ImageMagick-6.8.8-10.tar.gz
#cd ImageMagick-6.8.8-10/
#./configure
#make
#make install
#cd ../
#tar -zxvf imagick-3.1.1.tgz
#cd imagick-2.3.0/
#/usr/local/php/bin/phpize
#./configure --with-php-config=/usr/local/php/bin/php-config
#make
#make install
#cd ../
前面--enable-opcache=no现在需要自己安装
#wget wget http://pecl.php.net/get/zendopcache-7.0.2.tgz
#tar -zxvf zendopcache-7.0.2.tgz
#cd zendopcache-7.0.2
#/usr/local/php/bin/phpize
#./configure --with-php-config=/usr/local/php/bin/php-config
#make;make install
#cd /usr/local/php/etc
#vi php.ini
查找php.ini中的extension_dir = "./"
修改为extension_dir = /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212
并在此行后增加以下几行,然后保存:
extension = "memcache.so"
extension = "pdo_mysql.so"
extension = "imagick.so"
再查找; cgi.fix_pathinfo=0
修改为cgi.fix_pathinfo=0,防止Nginx文件类型错误解析漏洞。
在配置文件的[opcache]最后添加下面
zend_extension= /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/opcache.so
opcache.memory_consumption=256
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.force_restart_timeout=180
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1
###############vi fpm-php.conf #######注意php的注释号为;而不是#
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /var/log/php/php-fpm.log
log_level = notice
emergency_restart_threshold = 10
emergency_restart_interval = 60s
process_control_timeout = 5s
daemonize = yes
[www]
rlimit_files = 65535
rlimit_core = 0
user = www
group = www
listen.backlog = -1
listen.owner = www
listen.group = www
listen.mode = 0666
listen.allowed_clients = 127.0.0.1
listen = 127.0.0.1:9000
pm = static
pm.max_children = 128
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 1024
ping.response = pong
slowlog = /var/log/php/$pool.log.slow
request_slowlog_timeout = 0
request_terminate_timeout = 0
catch_workers_output = yes
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
下面安装nginx
pcre安装时,不要指定安装路径,否则容易出错
#tar -zxvf pcre-8.34.tar.gz
#cd pcre-8.34
#./configure ;make;make install;cd ..
#tar -zxvf nginx-1.4.6.tar.gz
#cd nginx-1.4.6
#groupadd www
#useradd -g www www -M
#./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
#make;make install
# mkdir -p /var/log/nginx
# chmod -R 755 /var/log/nginx/
# chown -R www:www /var/log/nginx/
#vi /etc/ld.so.conf
添加一句 /usr/local/lib
#ldconfig
#vi /usr/local/nginx/conf/nginx.conf
user www www;
worker_processes 4;
error_log /var/log/nginx/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 192.168.100.10;
index index.html index.htm index.php;
root /usr/local/nginx/html;
#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 /var/log/nginx/access.log access;
}
}
###################################################
#vi /usr/local/nginx/conf/fastcgi.conf
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;
##############################################################
以上为fastcgi.conf配置文件
#vi /etc/rc.d/init/nginx
##添加下面内容
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
killall -9 nginx
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
#chmod +x /etc/rc.d/init.d/nginx
#chkconfig nginx on
#service nginx start
#vi /etc/rc.d/init/php-fpm
##添加下面内容
#!/bin/sh
#
# php-fpm - this script starts and stops the php-fpm daemin
#
# chkconfig: - 85 15
# processname: php-fpm
# config: /usr/local/php/etc/php-fpm.conf
set -e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="php-fpm daemon"
NAME=php-fpm
DAEMON=/usr/local/php/sbin/$NAME
CONFIGFILE=/usr/local/php/etc/php-fpm.conf
PIDFILE=/usr/local/php/var/run/$NAME.pid #####要与你的php-fpm.conf对应,其他几个配置的路径都要根据实际来
SCRIPTNAME=/etc/init.d/$NAME
# If the daemon file is not found, terminate the script.
test -x $DAEMON || exit 0
d_start(){
$DAEMON -y $CONFIGFILE || echo -n " already running"
}
d_stop(){
kill -QUIT `cat $PIDFILE` || echo -n " no running"
}
d_reload(){
kill -HUP `cat $PIDFILE` || echo -n " could not reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
reload)
echo -n "Reloading $DESC configuration..."
d_reload
echo "Reloaded."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
# Sleep for two seconds before starting again, this should give the nginx daemon some time to perform a graceful stop
sleep 2
d_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload)" >&2
exit 3
;;
esac
exit 0
# mkdir -p /var/log/php
#chmod +x /etc/rc.d/init/php-fpm
#chkconfig php-fpm on
#service php-fpm start
如果有提示出错,根据提示进行排查,然后pkill php-fpm,再开启服务
最后,就是内核调优了
#vi /etc/sysctl.conf
###添加下面内容
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
##################################################
#ulimit -SHn 65535
这个是临时修改,重启后会消失,如果要永久修改,可以在/etc/profile最下面添加上上面这一句,然后就可以把服务都开起来。
想要测试php的处理能力,可以参考http://www.cppblog.com/qiujian5628/archive/2008/03/10/44060.html
浙公网安备 33010602011771号