以下内容为原创,转载请注明出处!
1:安装:apt-get install apache2
2:我们不使用自带的配置文件,新建一个新的文件并且配置以下:
vim /etc/apache2/sites-available/mysite.conf
请注意apache2.2和apache2.4配置是有区别的:
apache2.2:
Order deny,allow
Allow from all
apache2.4:
Require all granted
错误apache AH01630:client denied by server configuration就是因为上面错误导致
最终整体配置如下:
WSGIScriptAlias / /home/kefeng/kefeng/wsgi.py
WSGIPythonPath /home/kefeng:/home/virtualenv/lib/python2.7/site-packages
WSGIDaemonProcess mydjangosite processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup mydjangosite
Alias /static/ /home/kefeng/static/
Alias /media/ /home/kefeng/media/
ServerName www.520pf.cn
ServerAlias 520pf.cn
DocumentRoot /home/kefeng
RewriteEngine on # 开启rewrite引擎
RewriteCond %{HTTP_HOST} ^www.520pf.cn$ [OR]
RewriteCond %{HTTP_HOST} ^520pf.cn$ [OR]
RewriteRule ^/(.*)$ https://www.520pf.cn/$1 [R=301,L]<Directory /home/kefeng/static>
Require all granted
</Directory>
<Directory /home/kefeng/media>
Require all granted
</Directory>
<Directory /home/kefeng>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
3:修改django中的wsgi.py文件,配置如下:
import os
import sys
root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.insert(0, os.path.abspath(os.path.join(root_path, 'kefeng')))
sys.path.insert(0, root_path)
sys.path.append( '/home/virtualenv/lib/python2.7/site-packages')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "kefeng.settings")
os.environ['PATH'] = "%s"%(os.environ['PATH'])
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
4: a2ensite激活apache2配置文件中的站点:
cd /etc/apache2/site-available/
a2ensite mysite.conf
按照提示启动apache2
5:每次更新代码之后需要重启:service apache2 restart
6:当2个项目同时监听80端口时,配置如下:
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName www.phonefan.cn
ServerAlias phonefan.cn
DocumentRoot /mnt/WWW/phonefan
<Directory /mnt/WWW/phonefan>
Require all granted
</Directory>
ServerAdmin webmaster@localhost
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
<VirtualHost *:80>
WSGIScriptAlias / /home/keke/xuanxiu/xuanxiu/wsgi.py
#WSGIPythonPath /home/keke/xuanxiu:/home/keke/virtualenv/lib/python2.7/site-packages #这里WSGIPythonPath 不能出现在<VirtualHost *:80>包裹中,所以在下面配置python-path
WSGIDaemonProcess mydjangosite processes=2 threads=15 display-name=%{GROUP} python-path=/home/keke/xuanxiu:/home/keke/virtualenv/lib/python2.7/site-packages
WSGIProcessGroup mydjangosite
Alias /static/ /home/keke/xuanxiu/static/
Alias /media/ /home/keke/xuanxiu/media/
ServerName www.mcncn.com
DocumentRoot /home/keke/xuanxiu
<Directory /home/keke/xuanxiu/static>
Require all granted
</Directory>
<Directory /home/keke/xuanxiu/media>
Require all granted
</Directory>
<Directory /home/keke/xuanxiu>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
错误AttributeError: 'module' object has no attribute 'lru_cache':
说明用的是python3, 则需要重新安装:apt-get install libapache2-mod-wsgi-py3
apache ssl证书配置请参照:https://www.520pf.cn/article/102.html
负载均衡,会话保持请参照:https://www.520pf.cn/article/162.html