zoukankan      html  css  js  c++  java
  • ubuntu apache2服务器配置

    把django开发好的项目部署到apache2服务器。 记录我的配置过程。

    apache,django,mod_wsgi,python版本如下。不同版本大同小异。

    ii  apache2                           2.2.22-1ubuntu1.6                   Apache HTTP Server metapackage
    ii  python-django                     1.3.1-4ubuntu1.11                   High-level Python web development framework
    ii  libapache2-mod-wsgi               3.3-4ubuntu0.1                      Python WSGI adapter module for Apache
    ii  python                            2.7.3-0ubuntu2.2                    interactive high-level object-oriented language (default version)

    一、apache2配置说明

    ubuntu 用apt-get install apache2安装apache2后,配置文件都在/et/apache2目录下。

    基本原理

    apache2在启动的时候自动读取/etc/apache2/apache2.conf文件的配置信息,不同的配置项按功能分布在不同的文件中,然后被Include包含到apache2.conf这个主配置文件中,方便管理。就是说事实上apache2主配置文件只有一个,即apache2.conf,其他的都是被include进来的。可以把所有的配置都放在apache2.conf或者任何一个配置文件中,但是划分到不同文件会让我们管理起来方便很多,何乐而不为?

    1、apache2.conf配置文件

    该文件是apache的主配置文件,包括三个级别的配置。

    • 控制apache服务器执行过程的全局配置。
    • 定义主服务或者默认服务器的参数的配置,这些配置会响应virtual host不处理的请求。这类配置也为所有的virtual hosts配置提供默认值。
    • virtual hosts相关的配置,使得同一个apache服务进程处理向不同IP地址或者主机名发送的请求。
    ### Section 1: Global Environment
    #ServerRoot:apache服务器根目录。主配置文件,日志都在该目录。
    #注意路径结束时不要加斜杠,默认是/etc/apache2
    #ServerRoot "/etc/apache2"
    
    LockFile ${APACHE_LOCK_DIR}/accept.lock
    
    #apache服务启动时在该文件记录进程ID号
    # This needs to be set in /etc/apache2/envvars
    PidFile ${APACHE_PID_FILE}
    
    #连接超时,单位秒
    Timeout 300
    
    #是否允许持久连接
    KeepAlive On
    
    #持久连接时最大请求数
    MaxKeepAliveRequests 100
    
    KeepAliveTimeout 5
    
    
    # prefork MPM
    <IfModule mpm_prefork_module>
        StartServers          5
        MinSpareServers       5
        MaxSpareServers      10
        MaxClients          150
        MaxRequestsPerChild   0
    </IfModule>
    
    
    # worker MPM
    <IfModule mpm_worker_module>
        StartServers        10
        ServerLimit         100
        MinSpareThreads     50
        MaxSpareThreads     200
        ThreadsPerChild     64
        MaxClients          6400
        MaxRequestsPerChild   0
    </IfModule>
    
    # event MPM
    <IfModule mpm_event_module>
        StartServers          2
        MinSpareThreads      25
        MaxSpareThreads      75
        ThreadLimit          64
        ThreadsPerChild      25
        MaxClients          150
        MaxRequestsPerChild   0
    </IfModule>
    
    # These need to be set in /etc/apache2/envvars
    User ${APACHE_RUN_USER}
    Group ${APACHE_RUN_GROUP}
    
    #apache服务启动后在每个目录中查找后缀是.htaccess的文件,这些文件作为附加的配置
    AccessFileName .htaccess
    #下面几行防止.htaccess和.htpassword文件被web客户端访问
    <Files ~ "^.ht">
        Order allow,deny
        Deny from all
        Satisfy all
    </Files>
    
    #DefaultType是浏览器访问的MIME类型,设置为None,让浏览器自行解析
    DefaultType None
    
    # HostnameLookups: Log the names of clients or just their IP addresses
    # e.g., www.apache.org (on) or 204.62.129.132 (off).
    HostnameLookups Off
    
    #错误日志文件目录,默认是配置在此,可在<VirtualHost>中重载
    #日志文件取名时以'/'开头会造成冲突,不以'/'开头,会默认加上服务器根目录前缀
    #比如"foo.log"会被加上ServerRoor目录"/etc/apache2"变成"/etc/apache2/foo.log"
    ErrorLog ${APACHE_LOG_DIR}/error.log
    
    #Log日志记录哪些信息,取值可以是: debug, info, notice, warn, error, crit,alert, emerg.
    LogLevel warn
    
    #导入模块配置文件
    Include mods-enabled/*.load
    Include mods-enabled/*.conf
    #导入所有用户配置文件
    Include httpd.conf
    #导入端口监听配置文件
    Include ports.conf
    
    
    # The following directives define some format nicknames for use with
    # a CustomLog directive (see below).
    # If you are behind a reverse proxy, you might want to change %h into %{X-Forwarded-For}i
    #
    LogFormat "%v:%p %h %l %u %t "%r" %>s %O "%{Referer}i" "%{User-Agent}i"" vhost_combined
    LogFormat "%h %l %u %t "%r" %>s %O "%{Referer}i" "%{User-Agent}i"" combined
    LogFormat "%h %l %u %t "%r" %>s %O" common
    LogFormat "%{Referer}i -> %U" referer
    LogFormat "%{User-agent}i" agent
    
    #拦截访问
    <VirtualHost *:80>
    <Directory /***>
        Options None FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all 
    </Directory>
    </VirtualHost>
    
    
    #导入通用配置目录
    Include conf.d/
    
    #导入虚拟主机的配置
    Include sites-enabled/
    View Code

    2、apache2其他配置文件和子目录

    #2.2.22版本的apache2配置目录
    root@web-01:/etc/apache2# tree -L 1 . ├── apache2.conf 全局配置 ├── apache.pem ├── conf.d 一般性配置文件存放地 ├── envvars 环境变量 ├── httpd.conf ├── magic ├── mods-available 已安装的模块 ├── mods-enabled 已启用的模块 ├── ports.conf httpd服务端口信息 ├── sites-available 可用站点信息 ├── sites-enabled 已经启用的站点信息,当中的文件是到/etc/apache2/sites-available/ 文件的软连接。

    2.4.7版本的apache2配置文件目录如下

    字符集

    配置apache网站字符编码, /etc/apache2/conf.d/charset文件取消注释#AddDefaultCharset UTF-8

    ports.conf

    修改端口的话,/etc/apache2/ports.conf文件中修改NameVirtualHost *:80 改为NameVirtualHost x.x.x.x:80

    sites-available和sites-enabled目录

    sites-available目录是存放可用的内容,但不起作用,只有用ln 连到sites-enabled目录才可以起作用。sites-enabled目录存放真正起作用的配置文件,存放一些指向sites-available目录的符号链接。所以apache上配置了多个虚拟主机,每个虚拟主机的配置都放在sites-available下,那么对于虚拟主机的停用和启用就非常方便。当sites-enabled下建立一个指向某个虚拟主机配置文件的连接时,就启用了它。如果要关闭某个虚拟主机的话,只需要删除相应的符号链接即可,不用去改配置文件。

    mods-available和mods-enabled目录

    和上面说的sites-available、sites-enabled类似,这两个目录 是存放apache功能模块的配置文件和链接的。比如用apt-get install php5安装了PHP模块后,在这两个目录里就有了php5.load、php5.conf和指向这两个文件的链接。安装mod-wsgi也一样。这种目录结构对于启用、停用某个 Apache模块是非常方便的。

    配置完apache服务器后,重点就是要指定项目根目录的位置,Ubuntu默认是/var/www。可以在/etc/apache2/sites-available目录的default中看到

    root@web-01:/etc/apache2/sites-available# cat default
    <VirtualHost *:80>
            ServerAdmin webmaster@localhost
    
            DocumentRoot /var/www
            <Directory />
                    Options FollowSymLinks
                    AllowOverride None
            </Directory>
            <Directory /var/www/>
                    Options Indexes FollowSymLinks MultiViews
                    AllowOverride None
                    Order allow,deny
                    allow from all
            </Directory>
    
            ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
            <Directory "/usr/lib/cgi-bin">
                    AllowOverride None
                    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                    Order allow,deny
                    Allow from all
            </Directory>
    
            ErrorLog ${APACHE_LOG_DIR}/error.log
    
            # Possible values include: debug, info, notice, warn, error, crit,
            # alert, emerg.
            LogLevel warn
    
            CustomLog ${APACHE_LOG_DIR}/access.log combined
    
        Alias /doc/ "/usr/share/doc/"
        <Directory "/usr/share/doc/">
            Options Indexes MultiViews FollowSymLinks
            AllowOverride None
            Order deny,allow
            Deny from all
            Allow from 127.0.0.0/255.0.0.0 ::1/128
        </Directory>
    
    </VirtualHost>
    View Code

    这个默认的配置在正式发布时不需要,所以就不要加到sites-enable目录。需要访问自己的项目,就需要配置虚拟主机。

    二、配置虚拟主机

    这里配置的虚拟主机是同一台服务器同时处理超过一个域名。不同域名访问同一服务器(即同一IP)的相同或不同目录。

    有效的站点配置都在/etc/apache2/sites-available目录。

    1、<VirtualHost>语法

    <VirtualHost>和</VirtualHost>是用来分装一组仅作用于特定虚拟主机的配置。当服务器接收到一个特定虚拟主机的文档请求时,它会使用封装在<VirtualHost>配置段的配置。

    语法:

    <VirtualHost addr[:端口] [addr[:端口]]...>

    </VirtualHost>

    addr可以是:

    • 虚拟主机的IP地址
    • 虚拟主机IP地址对应的完整域名
    • 字符"*"
    • 字符串"_default_",与基于IP的虚拟主机联用以捕获所有没有匹配的IP地址

    2、通过wsgi配置djagno项目的虚拟主机

    第一步,在/etc/apache2/sites-available中创建xxx.org虚拟主机

    root@web-01:/etc/apache2/sites-available# cat xxx.org
    <VirtualHost *:80>                                                                    
    # 这里要注意,目录是工程目录,根据实际情况修改,后面的django.wsgi文件需要手动新建
    WSGIScriptAlias
    / /var/www/stack/wsgi/django.wsgi Alias /site_media /var/www/stack/static_resource ServerName www.xxx.org
    #Directory对根目录限制
    <Directory /var/www/stack/static_resource>
        Options  None FollowSymLinks #followsymlinks表示是否允许使用符号链接,默认为禁用
        AllowOverride None #标记禁止用户对目录配置文件(.htaccess进行修改)重载,普通站点不建议开启
        Order allow,deny #以allow优先处理,未明确说明允许的都拒绝
        Allow from all #明确指出允许所有访问
    </Directory>
    
    <Directory /var/www/stack/wsgi>
        #AuthType Basic  
        #AuthName "xxx"  
        #AuthUserFile /var/www/access  
        #Require valid-user 
        Order allow,deny
        Allow from all
    </Directory>
    ErrorLog   /etc/apache2/xxx.org.error.log
    LogLevel warn
    </VirtualHost>

    第二步,在/var/www/stack/wsgi目录中新建一个django.wsgi。

    root@web-01:/var/www/stack/wsgi# cat django.wsgi 
    import os
    import sys
    import django.core.handlers.wsgi
    from django.conf import settings
    
    # Add this file path to sys.path in order to import settings
    sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), '../..'))
    os.environ['DJANGO_SETTINGS_MODULE'] = 'stack.settings'
    sys.stdout = sys.stderr
    
    STACK_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
    
    sys.path.append(STACK_PATH)
    
    DEBUG = True
    
    application = django.core.handlers.wsgi.WSGIHandler()

    sys.path.insert()的参数是工程目录的上级目录,根据工程目录情况修改。

    os.environ['DJANGO_SETTINGS_MODULE']='stack.settings',这个stack.settings是工程目录下的setting文件,根据工程目录名称不同需要修改。

    django.wsgi文件名可以随便取,但是一定要与虚拟主机中WSGIScriptAlias配置的名称保持一致。

    资源链接:mod_wsgi实现一些配置命令的详细解释

    第三步,上一步配置好的内容只是“有效”虚拟主机,真正发挥效果的话得放到 /etc/apache2/sites-enabled 文件夹下面。所以使用ln命令来建立一对关联文件:

    ln -s /etc/apache2/sites-available/xxx.org /etc/apache2/sites-enabled/xxx.org

    第四步,检查语法,重启web服务

    谨慎起见,在重启服务前先检查语法:

    sudo apache2ctl configtest

    如果没有错误,再重启apache2

    # /etc/init.d/apache2 restart

    或者用service apache2 restart命令。现在就可以通过www.xxx.org来访问django项目了。

    3、配置wiki虚拟服务器

    【这里wiki使用的是mediawiki,用php写的,所以不用mod-wsgi】

    第一步,在/etc/apache2/sites-available中创建mediawiki的虚拟主机

    root@web-01:/etc/apache2/sites-available# cat wiki.xxx.org
    <VirtualHost *:80>                                                                                                                       
    Alias /wiki /var/lib/mediawiki
    Alias /index.php /var/lib/mediawiki/index.php
    #域名配置 ServerName wiki.xxx.org
    <Directory /var/lib/mediawiki/> Options FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> #log文件配置 ErrorLog /etc/apache2/wiki.xxx.org.error.log LogLevel warn </VirtualHost>

    第二步,上一步配置好的内容只是“有效”虚拟主机,真正发挥效果的话得放到 /etc/apache2/sites-enabled 文件夹下面。所以使用ln命令来建立一对关联文件:

    ln -s /etc/apache2/sites-available/wiki.xxx.org  /etc/apache2/sites-enabled/wiki.xxx.org

    第三步,检查语法,重启web服务

    谨慎起见,在重启服务前先检查语法:

    sudo apache2ctl configtest

    如果没有错误,再重启apache2

    # /etc/init.d/apache2 restart

    或者用service apache2 restart命令。现在就可以通过wiki.xxx.org/index.php来访问wiki了。

    Note:

    apache2中mediawiki的相关配置也可以这样配:

    /etc/mediawiki/apache.conf文件默认是mediawiki的apache2相关的配置,所以可以在/etc/apache2/sites-enabled中建一个软连接到/etc/mediawiki/apache.conf。

    # ln -s  /etc/mediawiki/apache.conf  /etc/apache2/sites-enabled/wiki.xxx.org

    具体的配置细节就在/etc/mediawiki/apache.conf中了。

    三、常见错误处理

    重启apache2时遇到以下问题

    1、AH00557

    AH00557: apache2: apr_sockaddr_info_get() failed for web01

    解决办法:在/etc/hosts中增加对web01的解析。然后重启apache不再报错AH00557。

    # cat /etc/hosts                      
    127.0.0.1       localhost web01

    2、AH00558:

    AH00557:apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message

     解决办法:在apache配置文件apache2.conf中添加ServerName localhost

    root@web01:/etc/apache2# vi apache2.conf 
    ServerName localhost

    3、排错过程

    配置虚拟主机,从一般到特殊。一般出错都是和配置项加载的顺序有关。

    对所有的域名或IP都进行拦截,让其访问一个不存在的目录。

    <VirtualHost *:80>
    <Directory /***>
         Options None FollowSymLinks
         AllowOverride None
         Order allow,deny
         Allow from all 
    </Directory>
    </VirtualHost>

    只针对具体的某个域名,在<VirtualHost>中进行配置。

    对于IncludeOptional sites-enabled/*.conf,一定要注意只导入以.conf结尾的配置文件,不要以为放在/sites-enabled/目录下就会被导入。

    为了避免错误,可以写成IncludeOptional sites-enabled/*

    四、资源链接

    apache虚拟主机文档

    apache虚拟主机配置

  • 相关阅读:
    unity抗锯齿效果
    DoTween联合动画Sequence的使用
    Unity3D获取模型在运动中任意帧的顶点坐标
    超长文件夹的删除。
    转 nandflash和norflash 片内执行~很详细
    (2)dsp emif 和 flash
    dsp emif 和 flash
    char and int
    DSP EMIF
    flash and sdram
  • 原文地址:https://www.cnblogs.com/starof/p/4278370.html
Copyright © 2011-2022 走看看