ubuntu配置Nginx前端Apache后端服务器

前几天断断续续的试用过apache和nginx,总的来说各有千秋吧,今天无意中听同学说起可以使用nginx作为前端服务器,apache作为后端服务器来处理php.这种结合了Nginx高并发和静态页面高效率以及Apache稳定的动态页面处理特点.再也不用担心Nginx以FastCGI模式运行PHP时的502问题,也不必担心Apache处理静态页面过慢,负载过高的问题.
好了,下面介绍下具体的过程.

配置apache

搭建LAMP环境

首先,配置好LAMP环境.我一般图方便都是这么配置的.

sudo apt-get install tasksel
sudo tasksel

输入上面命令后就可以直接选择LAMP了.安装过程中会让你输入mysql的root用户密码.

修改apache配置

编辑apache的配置文件,修改成下面的样子.

root@lylinux:~# cat /etc/apache2/ports.conf 
# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default
# This is also true if you have upgraded from before 2.2.9-3 (i.e. from
# Debian etch). See /usr/share/doc/apache2.2-common/NEWS.Debian.gz and
# README.Debian.gz

NameVirtualHost *:8080
Listen 8080

<IfModule mod_ssl.c>
    # If you add NameVirtualHost *:443 here, you will also have to change
    # the VirtualHost statement in /etc/apache2/sites-available/default-ssl
    # to <VirtualHost *:443>
    # Server Name Indication for SSL named virtual hosts is currently not
    # supported by MSIE on Windows XP.
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>

同时修改/etc/apache2/sites-enabled/000-default文件.只修改端口号

root@lylinux:~# cat /etc/apache2/sites-enabled/000-default 
<VirtualHost *:8080>
	ServerAdmin webmaster@localhost
......
......

既然我们要使用nginx作为前端,所以apache配置文件中的KeepAlive 就可以关闭了,修改/etc/apache2/apache2.conf,将KeepAlive设置为Off.

KeepAlive Off

同时,禁用掉无用的模块

a2dismod deflate
a2dismod cgi
a2dismod autoindex
a2dismod negotiation
a2dismod ssl
 

安装rpaf模块,要不然apache日志文件中记录的来源地址都是127.0.0.1

apt-get install libapache2-mod-rpaf

重启apache

service apache2 restart

安装nginx

安装nginx

apt-get install nginx

配置nginx

删除掉以前无用的配置

rm -rf /etc/nginx/sites-enabled/*

创建新的默认虚拟主机配置

cat >/etc/nginx/sites-available/000-default <<EOF
server {
    access_log off;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:8080;
    }
}
EOF

ln -s /etc/nginx/sites-available/000-default /etc/nginx/sites-enabled/000-default

创建虚拟主机,使用apache作为后端.

cat >/etc/nginx/sites-available/domain.com <<EOF
server {
    server_name www.domain.com domain.com;
    root /var/www/domain.com/;

    access_log off;

    # Static contents
    location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
        expires max;
    }

    # Dynamic content, forward to Apache
    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:8080;
    }
}
EOF

ln -s /etc/nginx/sites-available/domain.com /etc/nginx/sites-enabled/domain.com

重启nginx

/etc/init.d/nginx restart

优化

最后,在简单对apache和mysql做一下优化

apache优化

sed -i -e "s/^[[:blank:]]*StartServers[[:blank:]]*.*/StartServers       5/"  /etc/apache2/apache2.conf
sed -i -e "s/^[[:blank:]]*MinSpareServers[[:blank:]]*.*/MinSpareServers    5/" /etc/apache2/apache2.conf
sed -i -e "s/^[[:blank:]]*MaxSpareServers[[:blank:]]*.*/MaxSpareServers    10/" /etc/apache2/apache2.conf
sed -i -e "s/^[[:blank:]]*ServerLimit[[:blank:]]*.*/ServerLimit       10/" /etc/apache2/apache2.conf
sed -i -e "s/^[[:blank:]]*MaxClients[[:blank:]]*.*/MaxClients        10/" /etc/apache2/apache2.conf
sed -i -e "s/^[[:blank:]]*MinSpareThreads[[:blank:]]*.*/MinSpareThreads    5/" /etc/apache2/apache2.conf
sed -i -e "s/^[[:blank:]]*MaxSpareThreads[[:blank:]]*.*/MaxSpareThreads    10/" /etc/apache2/apache2.conf

mysql优化

cp /etc/mysql/my.cnf /etc/mysql/my.cnf.`date +%s`
find /usr/share/doc/mysql* -name my-small.cnf -exec cp {} /etc/mysql/my.cnf \;
sed -i 's/\[mysqld\]/&\nuser            = mysql\n/' /etc/mysql/my.cnf

发表评论

电子邮件地址不会被公开。 必填项已用*标注