一聚教程网:一个值得你收藏的教程网站

热门教程

ubuntu系统中Nginx前端及Apache后端服务器配置

时间:2022-06-30 19:17:52 编辑:袖梨 来源:一聚教程网


配置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


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



    Listen 443


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

root@lylinux:~# cat /etc/apache2/sites-enabled/000-default

    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

apt-get install nginx

配置nginx

删除掉以前无用的配置

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

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

cat >/etc/nginx/sites-available/000-default <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 <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            = mysqln/' /etc/mysql/my.cnf

热门栏目