LNMP简介
LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。
作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率。
目前大部分的互联网公司因为历史原因,使用的架构大多是比较旧的版本,但这不影响公司业务的开展。技术是一种工具,不管这种工具的新旧损耗,只要能够使大局稳定运行,即是值得学习的!
本文采用Nginx 1.12、MySQL 5.5、PHP 5.3进行搭建。
预装环境
$ yum -y install gcc gcc-c++
$ yum -y install 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
mysql5.5
安装mysql5.5
$ groupadd mysql
$ useradd -g mysql mysql
$ cd /usr/local/software
$ tar -zxvf mysql-5.5.3-m3.tar.gz
$ cd mysql-5.5.3-m3/
$ ./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=partition,innobase,myisammrg
$ make && make install
配置mysql5.5
$ chmod +w /usr/local/mysql
$ chown -R mysql:mysql /usr/local/mysql
$ mkdir -p /db/mysql/data/
$ mkdir -p /db/mysql/data/binlog/
$ mkdir -p /db/mysql/data/relaylog/
$ chown -R mysql:mysql /db/mysql/
创建my.cnf配置文件:
vim /etc/my.cnf
################ start ################
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /db/mysql/data
skip-innodb
skip-name-resolve
character-set-server = utf8
collation-server = utf8_general_ci
init-connect='SET NAMES utf8'
low-priority-updates
back_log = 1500
wait_timeout = 10
interactive_timeout = 20
key_buffer_size = 2000M
log_slow_queries=/db/mysql/data/log_slow_queries.log
long_query_time = 4
log-queries-not-using-indexes = 0
log= /db/mysql/data/mysql.log
max_allowed_packet = 500M
table_cache = 5000
sort_buffer_size = 64M
net_buffer_length = 2M
myisam_sort_buffer_size = 256M
thread_cache_size = 64
read_buffer_size = 64M
read_rnd_buffer_size = 100M
query_cache_size = 512M
query_cache_type = 1
log-bin = /db/mysql/data/binlog/mysql-bin
expire_logs_days = 3
binlog_cache_size = 4M
binlog_format = mixed
max_binlog_cache_size = 8M
max_binlog_size = 512M
tmp_table_size = 700M
max_connections = 32000
max_heap_table_size = 800M
join_buffer_size = 64M
open_files_limit = 4096
max_connect_errors = 30000
query_cache_limit = 3145728
[mysqldump]
quick
max_allowed_packet = 500M
[mysql]
no-auto-rehash
[isamchk]
key_buffer = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
[myisamchk]
key_buffer = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
################ stop ################
初始化数据表
$ /usr/local/mysql/bin/mysql_install_db --basedir=/usr/local/mysql --datadir=/db/mysql/data --user=mysql
创建mysql启动脚本
$ vim /etc/init.d/mysqld
################ start ################
basedir=
datadir=
service_startup_timeout=900
pid_file=
server_pid_file=
use_mysqld_safe=1
user=mysql
if test -z "$basedir"
then
basedir=/usr/local/mysql
bindir=/usr/local/mysql/bin
if test -z "$datadir"
then
datadir=/db/mysql/data
fi
sbindir=/usr/local/mysql/sbin
libexecdir=/usr/local/mysql/libexec
else
bindir="$basedir/bin"
if test -z "$datadir"
then
datadir="$basedir/data"
fi
sbindir="$basedir/sbin"
libexecdir="$basedir/libexec"
fi
datadir_set=
lsb_functions="/lib/lsb/init-functions"
if test -f $lsb_functions ; then
. $lsb_functions
else
log_success_msg()
{
echo " SUCCESS! $@"
}
log_failure_msg()
{
echo " ERROR! $@"
}
fi
PATH=/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin
export PATH
mode=$1 # start or stop
shift
other_args="$*" # uncommon, but needed when called from an RPM upgrade action
# Expected: "--skip-networking --skip-grant-tables"
# They are not checked here, intentionally, as it is the resposibility
# of the "spec" file author to give correct arguments only.
case `echo "testing\c"`,`echo -n testing` in
*c*,-n*) echo_n= echo_c= ;;
*c*,*) echo_n=-n echo_c= ;;
*) echo_n= echo_c='\c' ;;
esac
parse_server_arguments() {
for arg do
case "$arg" in
--basedir=*) basedir=`echo "$arg" | sed -e 's/^[^=]*=//'`
bindir="$basedir/bin"
if test -z "$datadir_set"; then
datadir="$basedir/data"
fi
sbindir="$basedir/sbin"
libexecdir="$basedir/libexec"
;;
--datadir=*) datadir=`echo "$arg" | sed -e 's/^[^=]*=//'`
datadir_set=1
;;
--user=*) user=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
--pid-file=*) server_pid_file=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
--service-startup-timeout=*) service_startup_timeout=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
--use-mysqld_safe) use_mysqld_safe=1;;
--use-manager) use_mysqld_safe=0;;
esac
done
}
parse_manager_arguments() {
for arg do
case "$arg" in
--pid-file=*) pid_file=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
--user=*) user=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
esac
done
}
wait_for_pid () {
verb="$1"
manager_pid="$2" # process ID of the program operating on the pid-file
i=0
avoid_race_condition="by checking again"
while test $i -ne $service_startup_timeout ; do
case "$verb" in
'created')
# wait for a PID-file to pop into existence.
test -s $pid_file && i='' && break
;;
'removed')
# wait for this PID-file to disappear
test ! -s $pid_file && i='' && break
;;
*)
echo "wait_for_pid () usage: wait_for_pid created|removed manager_pid"
exit 1
;;
esac
# if manager isn't running, then pid-file will never be updated
if test -n "$manager_pid"; then
if kill -0 "$manager_pid" 2>/dev/null; then
: # the manager still runs
else
# The manager may have exited between the last pid-file check and now.
if test -n "$avoid_race_condition"; then
avoid_race_condition=""
continue # Check again.
fi
# there's nothing that will affect the file.
log_failure_msg "Manager of pid-file quit without updating file."
return 1 # not waiting any more.
fi
fi
echo $echo_n ".$echo_c"
i=`expr $i + 1`
sleep 1
done
if test -z "$i" ; then
log_success_msg
return 0
else
log_failure_msg
return 1
fi
}
if test -x ./bin/my_print_defaults
then
print_defaults="./bin/my_print_defaults"
elif test -x $bindir/my_print_defaults
then
print_defaults="$bindir/my_print_defaults"
elif test -x $bindir/mysql_print_defaults
then
print_defaults="$bindir/mysql_print_defaults"
else
# Try to find basedir in /etc/my.cnf
conf=/etc/my.cnf
print_defaults=
if test -r $conf
then
subpat='^[^=]*basedir[^=]*=\(.*\)$'
dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf`
for d in $dirs
do
d=`echo $d | sed -e 's/[ ]//g'`
if test -x "$d/bin/my_print_defaults"
then
print_defaults="$d/bin/my_print_defaults"
break
fi
if test -x "$d/bin/mysql_print_defaults"
then
print_defaults="$d/bin/mysql_print_defaults"
break
fi
done
fi
# Hope it's in the PATH ... but I doubt it
test -z "$print_defaults" && print_defaults="my_print_defaults"
fi
extra_args=""
if test -r "$basedir/my.cnf"
then
extra_args="-e $basedir/my.cnf"
else
if test -r "$datadir/my.cnf"
then
extra_args="-e $datadir/my.cnf"
fi
fi
parse_server_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server`
parse_manager_arguments `$print_defaults $extra_args manager`
if test -z "$pid_file"
then
pid_file=$datadir/mysqlmanager-`/bin/hostname`.pid
else
case "$pid_file" in
/* ) ;;
* ) pid_file="$datadir/$pid_file" ;;
esac
fi
if test -z "$server_pid_file"
then
server_pid_file=$datadir/`/bin/hostname`.pid
else
case "$server_pid_file" in
/* ) ;;
* ) server_pid_file="$datadir/$server_pid_file" ;;
esac
fi
case "$mode" in
'start')
# Start daemon
# Safeguard (relative paths, core dumps..)
cd $basedir
manager=$bindir/mysqlmanager
if test -x $libexecdir/mysqlmanager
then
manager=$libexecdir/mysqlmanager
elif test -x $sbindir/mysqlmanager
then
manager=$sbindir/mysqlmanager
fi
echo $echo_n "Starting MySQL"
if test -x $manager -a "$use_mysqld_safe" = "0"
then
if test -n "$other_args"
then
log_failure_msg "MySQL manager does not support options '$other_args'"
exit 1
fi
# Give extra arguments to mysqld with the my.cnf file. This script may
# be overwritten at next upgrade.
"$manager" \
--mysqld-safe-compatible \
--user="$user" \
--pid-file="$pid_file" >/dev/null 2>&1 &
wait_for_pid created $!; return_value=$?
# Make lock for RedHat / SuSE
if test -w /var/lock/subsys
then
touch /var/lock/subsys/mysqlmanager
fi
exit $return_value
elif test -x $bindir/mysqld_safe
then
# Give extra arguments to mysqld with the my.cnf file. This script
# may be overwritten at next upgrade.
pid_file=$server_pid_file
$bindir/mysqld_safe --datadir=$datadir --pid-file=$server_pid_file $other_args >/dev/null 2>&1 &
wait_for_pid created $!; return_value=$?
# Make lock for RedHat / SuSE
if test -w /var/lock/subsys
then
touch /var/lock/subsys/mysql
fi
exit $return_value
else
log_failure_msg "Couldn't find MySQL manager ($manager) or server ($bindir/mysqld_safe)"
fi
;;
'stop')
# Stop daemon. We use a signal here to avoid having to know the
# root password.
# The RedHat / SuSE lock directory to remove
lock_dir=/var/lock/subsys/mysqlmanager
# If the manager pid_file doesn't exist, try the server's
if test ! -s "$pid_file"
then
pid_file=$server_pid_file
lock_dir=/var/lock/subsys/mysql
fi
if test -s "$pid_file"
then
mysqlmanager_pid=`cat $pid_file`
if (kill -0 $mysqlmanager_pid 2>/dev/null)
then
echo $echo_n "Shutting down MySQL"
kill $mysqlmanager_pid
# mysqlmanager should remove the pid_file when it exits, so wait for it.
wait_for_pid removed "$mysqlmanager_pid"; return_value=$?
else
log_failure_msg "MySQL manager or server process #$mysqlmanager_pid is not running!"
rm $pid_file
fi
# delete lock for RedHat / SuSE
if test -f $lock_dir
then
rm -f $lock_dir
fi
exit $return_value
else
log_failure_msg "MySQL manager or server PID file could not be found!"
fi
;;
'restart')
# Stop the service and regardless of whether it was
# running or not, start it again.
if $0 stop $other_args; then
$0 start $other_args
else
log_failure_msg "Failed to stop running server, so refusing to try to start."
exit 1
fi
;;
'reload'|'force-reload')
if test -s "$server_pid_file" ; then
read mysqld_pid < $server_pid_file
kill -HUP $mysqld_pid && log_success_msg "Reloading service MySQL"
touch $server_pid_file
else
log_failure_msg "MySQL PID file could not be found!"
exit 1
fi
;;
'status')
# First, check to see if pid file exists
if test -s "$server_pid_file" ; then
read mysqld_pid < $server_pid_file
if kill -0 $mysqld_pid 2>/dev/null ; then
log_success_msg "MySQL running ($mysqld_pid)"
exit 0
else
log_failure_msg "MySQL is not running, but PID file exists"
exit 1
fi
else
# Try to find appropriate mysqld process
mysqld_pid=`pidof $libexecdir/mysqld`
if test -z $mysqld_pid ; then
if test "$use_mysqld_safe" = "0" ; then
lockfile=/var/lock/subsys/mysqlmanager
else
lockfile=/var/lock/subsys/mysql
fi
if test -f $lockfile ; then
log_failure_msg "MySQL is not running, but lock exists"
exit 2
fi
log_failure_msg "MySQL is not running"
exit 3
else
log_failure_msg "MySQL is running but PID file could not be found"
exit 4
fi
fi
;;
*)
# usage
echo "Usage: $0 {start|stop|restart|reload|force-reload|status} [ MySQL server options ]"
exit 1
;;
esac
exit 0
################ stop ################
$ chmod +x /etc/init.d/mysqld
$ /etc/init.d/mysqld start
$ ln -s /usr/local/mysql/bin/* /usr/local/bin/
$ mysql -u root -p -S /tmp/mysql.sock
PHP 5.3.8
安装依赖
$ cd /usr/local/software
$ tar -zxvf libiconv-1.14.tar.gz
$ cd libiconv-1.14/
$ ./configure && make && make install
$ cd /usr/local/software
$ 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 /usr/local/software
$ 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
$ /sbin/ldconfig
$ cd /usr/local/software
$ tar -zxvf mcrypt-2.6.8.tar.gz
$ cd mcrypt-2.6.8/
$ ./configure && make && make install
安装php(FastCGI模式)
$ groupadd nginx
$ useradd -g nginx nginx
$ cd /usr/local/software
$ tar -zxvf php5.3.8.tar.gz
$ cd php-5.3.8/
$ ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-gd --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-mhash --with-mcrypt --with-curl --with-curlwrappers --with-openssl --with-gettext --with-iconv-dir --with-libxml-dir --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-inline-optimization --enable-mbstring --enable-ftp --enable-gd-native-ttf --enable-zip --enable-sockets
$ make ZEND_EXTRA_LIBS='-liconv'
$ make install
配置php
$ cp php.ini-production /usr/local/php/etc/php.ini
$ cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
$ chown -R nginx:nginx /usr/local/php
$ chmod -R 755 /usr/local/php
编辑php-fpm启动脚本
$ vim /etc/init.d/php-fpm
############## start ##############
#!/bin/sh
### BEGIN INIT INFO
# Provides: php-fpm
# Required-Start: $remote_fs $network
# Required-Stop: $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts php-fpm
# Description: starts the PHP FastCGI Process Manager daemon
### END INIT INFO
prefix=/usr/local/php
exec_prefix=${prefix}
php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=${prefix}/etc/php-fpm.conf
php_fpm_PID=${prefix}/var/run/php-fpm.pid
php_opts="--fpm-config $php_fpm_CONF"
wait_for_pid () {
try=0
while test $try -lt 35 ; do
case "$1" in
'created')
if [ -f "$2" ] ; then
try=''
break
fi
;;
'removed')
if [ ! -f "$2" ] ; then
try=''
break
fi
;;
esac
echo -n .
try=`expr $try + 1`
sleep 1
done
}
case "$1" in
start)
echo -n "Starting php-fpm "
$php_fpm_BIN $php_opts
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
fi
wait_for_pid created $php_fpm_PID
if [ -n "$try" ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
stop)
echo -n "Gracefully shutting down php-fpm "
if [ ! -r $php_fpm_PID ] ; then
echo "warning, no pid file found - php-fpm is not running ?"
exit 1
fi
kill -QUIT `cat $php_fpm_PID`
wait_for_pid removed $php_fpm_PID
if [ -n "$try" ] ; then
echo " failed. Use force-quit"
exit 1
else
echo " done"
fi
;;
force-quit)
echo -n "Terminating php-fpm "
if [ ! -r $php_fpm_PID ] ; then
echo "warning, no pid file found - php-fpm is not running ?"
exit 1
fi
kill -TERM `cat $php_fpm_PID`
wait_for_pid removed $php_fpm_PID
if [ -n "$try" ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
restart)
$0 stop
$0 start
;;
reload)
echo -n "Reload service php-fpm "
if [ ! -r $php_fpm_PID ] ; then
echo "warning, no pid file found - php-fpm is not running ?"
exit 1
fi
kill -USR2 `cat $php_fpm_PID`
echo " done"
;;
*)
echo "Usage: $0 {start|stop|force-quit|restart|reload}"
exit 1
;;
esac
############## stop ##############
$ chmod +x /etc/init.d/php-fpm
安装php扩展
$ ln -s /usr/local/mysql/include/mysql/ /usr/include
$ ln -s /usr/local/mysql/lib/mysql/ /usr/lib
$ ln -s /usr/local/mysql/include/mysql /usr/local/mysql/include/mysql/psi
安装mysql扩展
$ cd ext/
$ cd mysql
$ /usr/local/php/bin/phpize
$ ./configure --with-php-config=/usr/local/php/bin/php-config --with-mysql=/usr/local/mysql/ && make && make install
安装mysqli扩展
$ cd ..
$ cd mysqli
$ ./configure --with-php-config=/usr/local/php/bin/php-config --with-mysqli=/usr/local/mysql/bin/mysql_config && make && make install
安装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
安装eaccelerator扩展
$ cd ..
$ tar -jxvf eaccelerator-0.9.6.1.tar.bz2
$ cd eaccelerator-0.9.6.1
$ /usr/local/php/bin/phpize
$ ./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config && make && make install
安装PDO_MYSQL扩展
$ cd ..
$ tar -zxvf PDO_MYSQL-1.0.2.tgz
$ cd PDO_MYSQL-1.0.2
$ /usr/local/php/bin/phpize
$ ./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql && make && make install
安装ImageMagick扩展
$ cd ..
$ tar -zxvf ImageMagick-6.8.2-10.tar.xz
$ xz -d ImageMagick-6.8.2-10.tar.xz
$ tar -xvf ImageMagick-6.8.2-10.tar
$ cd ImageMagick-6.8.2-10
$ ./configure && make && make install
$ cd ..
安装imagick扩展
$ tar -zxvf imagick-3.4.0.tgz
$ cd imagick-3.4.0
$ /usr/local/php/bin/phpize
$ ./configure --with-php-config=/usr/local/php/bin/php-config
$ make && make install
安装magickwand扩展
$ cd ..
$ tar -zxvf MagickWandForPHP-1.0.9-2.tar.gz
$ cd MagickWandForPHP-1.0.9
$ /usr/local/php/bin/phpize
$ ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install
安装redis扩展
$ cd ..
$ tar -zxvf phpredis-master.tar.gz
$ cd phpredis-master
$ /usr/local/php/bin/phpize
$ ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install
安装scws扩展
$ cd ..
$ tar -jxvf scws-1.2.3.tar.bz2
$ cd scws-1.2.3
$ ./configure --prefix=/usr/local/scws && make && make install
$ cd phpext/
$ /usr/local/php/bin/phpize
$ ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install
安装sphinx扩展
$ cd ..
$ tar -zxvf sphinx-2.2.11-release.tar.gz
$ cd sphinx-2.2.11-release/api/libsphinxclient/
$ ./configure --prefix=/usr/local/libsphinxclient && make && make install
$ cd ../../../
$ tar -zxvf sphinx-1.3.2.tgz
$ cd sphinx-1.3.2
$ ./configure --with-php-config=/usr/local/php/bin/php-config --with-sphinx=/usr/local/libsphinxclient/ && make && make install
打开php扩展
编辑php.ini配置文件,搜索"; extension_dir = “./”",在下面添加以下几行,以打开扩展
$ vim /usr/local/php/etc/php.ini
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/"
extension = "memcache.so"
extension = "pdo_mysql.so"
extension = "imagick.so"
extension = "magickwand.so"
extension = "redis.so"
extension = "mysql.so"
extension = "mysqli.so"
extension = "sphinx.so"
extension = "xhprof.so"
[scws]
extension = scws.so
scws.default.charset = gbk
scws.default.fpath = /usr/local/scws/etc
修改php配置文件
查找output_buffering = Off
修改为output_buffering = On
再查找; cgi.fix_pathinfo=1
修改为cgi.fix_pathinfo=0,防止Nginx文件类型错误解析漏洞。
配置eAccelerator加速PHP
$ mkdir -p /usr/local/eaccelerator_cache
$ vim /usr/local/php/etc/php.ini
[eaccelerator]
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/eaccelerator.so"
eaccelerator.shm_size="64"
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"
启动php-cgi进程
$ /etc/init.d/php-fpm start
启动时报 pm.min_spare_servers(0) must be a positive value 错误
$ vim /usr/local/php/etc/php-fpm.conf
pm.start_servers=20
pm.min_spare_servers=5
pm.max_spare_servers=35
Nginx
安装依赖
$ cd /usr/local/software
$ tar -zxvf pcre-8.10.tar.gz
$ cd pcre-8.10/
$ ./configure && make && make install
安装Nginx
$ cd /usr/local/software
$ tar -zxvf nginx-1.12.2.tar.gz
$ tar -zxvf headers-more-nginx-module-0.32.tar.gz
$ cd nginx-1.12.2/
$ ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --add-module=/usr/local/software/headers-more-nginx-module-0.32
$ make && make install
创建nginx配置文件
$ mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf_def
$ vim /usr/local/nginx/conf/nginx.conf
################# start #################
user nginx nginx;
worker_processes 8;
error_log logs/error.log crit;
pid logs/nginx.pid;
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
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;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server
{
listen 80;
server_name www.wege.com;
index index.html index.htm index.php;
root /web;
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
access_log logs/access.log main;
}
}
################# stop #################
启动nginx
$ /usr/local/nginx/sbin/nginx
设置开机自启
编辑/etc/rc.local文件,加入以下几行
$ vim /etc/rc.local
############### start ###############
/etc/init.d/mysqld start
ulimit -SHn 65535
/etc/init.d/php-fpm start
/usr/local/nginx/sbin/nginx
############### stop ###############
优化Linux内核参数
编辑/etc/sysctl.conf,在末尾增加以下内容
vim /etc/sysctl.conf
############### start ###############
# 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
############### stop ###############
使配置立即生效
$ /sbin/sysctl -p