首先,添加安装源
Centos 默认官方源中没有nginx 和 php-fpm,所以必须添加第三方源,否则就只能自己编译了。
添加[CentALT]源
在/etc/yum.repo.d 目下创建 alt.ru.repo 文件内容如下:
name=CentALT Packages for Enterprise Linux 5 - $basearch
baseurl=http://centos.alt.ru/repository/centos/5/$basearch/
enabled=1
gpgcheck=0
启用 EPEL
rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
32系统的用下面命令
rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm
安装 nginx +PHP-FPM +eAccelerator +mysql
yum update
yum install nginx php-fpm php-cli php-pdo php-mysql php-mcrypt php-mbstring php-gd php-tidy php-xml php-xmlrpc php-pear php-pecl-memcache php-eaccelerator
配置优化Nginx
修改 /etc/nginx/nginx.conf,下面只列出修改的部分。
#根据CPU 核心processes,VPS下几个核心几个processes,独立服务器可x2
#启用epoll
worker_rlimit_nofile 51200;
events {
worker_connections 51200;
use epoll;
}
#参数调整
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
keepalive_timeout 50;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
#fastcgi优化
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
#开启gzip并优化
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;
php-fpm 调整
修改/etc/php-fpm.conf 将max_children 调为20
<value name="max_children">20</value>mysql 调整
修改/etc/my.conf [mysqld] 段加入如下指令
skip-innodb
skip-locking
skip-bdb
key_buffer = 16K
max_allowed_packet = 3M
thread_stack = 64K
thread_cache_size = 8
table_cache = 3
query_cache_limit = 1M
query_cache_size = 16M
系统内核调整
在/etc/sysctl.conf 加入
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 5000 65000
net.ipv4.tcp_max_tw_buckets = 6000
echo “ulimit -SHn 65535” >> /etc/rc.local
styctl -p 开启服务并设置系统启动默认启动
/etc/init.d/nginx start
/etc/init.d/php-fpm start
/etc/init.d/mysqld start
chkconfig nginx on
chkconfig php-fpm on
chkconfig mysqld on
您可能感兴趣的文章:
nginx下设置php-fpm使用socket文件的方法分享
nginx中php-fpm使用sock方式配置的例子
深入理解php-fpm.conf中的两个重要参数
ngnix与php-fpm 安装一例
nginx中php-fpm调优方法
有关nginx+php-fpm配置文件的组织结构
在nginx中查看php-fpm工作状态
第一步:在nginx.conf中配置反向代理时把真实IP带上,例如:
server {
listen 80;
server_name boyan.com;
location ~ ^/(WEB-INF)/ {
deny all;
}
location / {
proxy_pass http://localhost:8888;
proxy_set_header X-Real-IP $remote_addr;
}
}
第二步:应用程序中用 String ip = request.getHeader("X-Real-IP");替代String ip = request.getRemoteAddr();即可
注:
和Apache有点类似
X-Forwarded-For
WL-Proxy-Client-IP
把Nginx加为linux的系统服务,其实就是创建一个启动管理脚本,类似于httpd可以使用service启动与停止,可以使用chkconfig配置服务一样。
#!/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
使用示例:
[root@example ~]# cp nginx /etc/init.d/
[root@example ~]# chmod 755 /etc/init.d/nginx
[root@example ~]# chkconfig --add nginx