您的位置:首页 > 运维架构 > Apache

在Ubuntu 14.04 下部署Apache2服务器并发布Django 1.7.6 Web应用(本机)

2015-05-02 08:21 726 查看
各个组件版本如下

软件安装
Django176 安装

Apache2 安装分两步

环境配置
Apache配置

结果展示

最近需要在Ubuntu下发布Django应用,准备用Apache2作为服务器,找了一大堆资料终于找到解决方案,希望能帮助正在纠结这个问题的你。

各个组件版本如下:

Ubuntu 14.04

Python 2.7.8

Apache 2.4.10

*Django 1.7.6

软件安装

由于Python使用的是Ubuntu自带的版本,所以不做安装介绍。

Django1.7.6 安装

Django 1.7.6

从Django https://www.djangoproject.com/ 官网下载 Django-1.7.6.tar.gz文件,

tar -zxvf Django-1.7.6.tar.gz
cd Django-1.7.6/
sudo python setup.py install


Apache2 安装(分两步)

第一步 安装apache2

sudo apt-get install apache2


接下来可以通过本地访问127.0.0.1或者查看进程来确定Apache是否安装成功。

第二步 安装mod_wsgi模块

1.sudo apt-get install libapache2-mod-wsgi


如果是Python3的话,需要

2.sudo apt-get install libapache2-mod-wsgi-py3


如果同时安装了两个版本的模块,则Apache会优先使用针对python2.x的版本。

环境配置

目前为止,所有的软件都已安装完毕,并且保证可用,下面就进行具体的配置。

Apache配置

在Ubuntu14.04中安装的Apache2.4,配置文件的存在方式发生了变化,为了使配置文件阅读和存储更加方便有条理,Apache将配置信息由原来一个文件httpd.conf分割成了多个,存放在各个目录中,方便了管理和扩展。下面是Apache配置文件中的一个存放配置文件的目录示意图(不要找什么httpd.conf!Ubuntu下是没有的,这里哭死了。。。):

其中apache2.conf是主配置文件,具体的说明文件可以参考该文件。

接下来为写好的站点(站点名字叫 firstsite )配置一个虚拟主机:

1.cd /etc/apache2/sites-available


2.vim firstsite.conf  #配置文件内容如下


#/etc/apache2/sites-avaliable/firstsite.conf
<VirtualHost *:80>
ServerName www.firstsite.com
DocumentRoot /home/hadoop/Documents/pythonweb/firstsite
<Directory /home/hadoop/Documents/pythonweb/firstsite/firstsite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias / /home/hadoop/Documents/pythonweb/firstsite/firstsite/wsgi.py
Alias /firstsite/static/ /var/www/firstsite/static/
</VirtualHost>
WSGIPythonPath /home/hadoop/Documents/pythonweb/firstsite


/home/hadoop/Documents/pythonweb/firstsite是项目文件夹;

/home/hadoop/Documents/pythonweb/firstsite/firstsite是wsgi文件的文件夹;

WSGIScriptAlias / /home/hadoop/Documents/pythonweb/firstsite/firstsite/wsgi.py中第一个参数”/“是指将站点指定的哪个URL,第二个参数是指wsgi文件的位置

Alias /firstsite/static/ /var/www/firstsite/static/设置Apache将/firstsite/static/映射到/var/www/firstsite/static/,这是个放静态文件的目录,因为Django不处理静态文件,而是将静态文件留给web server来处理,所以需要设置这个;

WSGIPythonPath /home/hadoop/Documents/pythonweb/firstsite确保将项目目录引入到Python的path中,即执行目录中。

hadoop是我的主机名,此处记得替换掉,www.firstsite.com是站点名,需要添加到/etc/hosts文件中

3.vim /etc/hosts


#/etc/hosts
127.0.0.1       localhost
127.0.1.1       hadoop
127.0.0.1       www.firstsite.com


上面的配置就可以完成Django站点的基本配置,因为wsgi文件在Django1.7中是默认创建的,里边有一些基本信息,可以保证站点的运行,其他信息需要自己额外配置。

接下来,使用如下Apache命令,将该配置文件启用:

4.a2ensite /etc/apache2/sites-available/firstsite.conf # enable该站点
5.service apache reload   # 重新加载Apache服务器配置


结果展示

打开http://www.firstsite.com/admin/



至此,恭喜你已经完成了Ubuntu下Django项目在Apache2的部署,希望能帮到你~

主要参考网页:

http://blog.leanote.com/post/54c19eedbfdc7c6e14000001
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: