把django开发好的项目部署到apache2服务器。 记录个人配置过程。php
apache,django,mod_wsgi,python版本以下。不一样版本大同小异。html
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)
ubuntu 用apt-get install apache2安装apache2后,配置文件都在/et/apache2目录下。python
基本原理web
apache2在启动的时候自动读取/etc/apache2/apache2.conf文件的配置信息,不一样的配置项按功能分布在不一样的文件中,而后被Include包含到apache2.conf这个主配置文件中,方便管理。就是说事实上apache2主配置文件只有一个,即apache2.conf,其余的都是被include进来的。能够把全部的配置都放在apache2.conf或者任何一个配置文件中,可是划分到不一样文件会让咱们管理起来方便不少,何乐而不为?apache
该文件是apache的主配置文件,包括三个级别的配置。django
### 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/
#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配置文件目录以下ubuntu
字符集浏览器
配置apache网站字符编码, /etc/apache2/conf.d/charset文件取消注释#AddDefaultCharset UTF-8服务器
ports.confapp
修改端口的话,/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>
这个默认的配置在正式发布时不须要,因此就不要加到sites-enable目录。须要访问本身的项目,就须要配置虚拟主机。
这里配置的虚拟主机是同一台服务器同时处理超过一个域名。不一样域名访问同一服务器(即同一IP)的相同或不一样目录。
有效的站点配置都在/etc/apache2/sites-available目录。
<VirtualHost>和</VirtualHost>是用来分装一组仅做用于特定虚拟主机的配置。当服务器接收到一个特定虚拟主机的文档请求时,它会使用封装在<VirtualHost>配置段的配置。
语法:
<VirtualHost addr[:端口] [addr[:端口]]...>
</VirtualHost>
addr能够是:
第一步,在/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配置的名称保持一致。
第三步,上一步配置好的内容只是“有效”虚拟主机,真正发挥效果的话得放到 /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项目了。
【这里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时遇到如下问题
AH00557: apache2: apr_sockaddr_info_get() failed for web01
解决办法:在/etc/hosts中增长对web01的解析。而后重启apache再也不报错AH00557。
# cat /etc/hosts 127.0.0.1 localhost web01
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
配置虚拟主机,从通常到特殊。通常出错都是和配置项加载的顺序有关。
对全部的域名或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/*