【转】手把手教你:Ubuntu14+apache2+django1.7+python2.7下网页/网站部署

本人亲自尝试了网上众多的部署网页/网站方法,绝大多数都未能试验成功,这次的项目光部署这块遇到了很多问题,大概耗费了我一个星期。

本着:王道论坛中的赠人玫瑰,手留余香的精神。我把自己一路所走的历程发布出来。网上的部署过程十分复杂,我这里把能简化的部分全部简化,努力达到让各位快速部署的目的。

步骤1:

环境:

需要安装:django,mod_wsgi

执行命令:

 

sudo apt-get install python-setuptools

sudo apt-get install python-pip

 

sudo apt-get install apache2

easy_install django

sudo apt-get install libapache2-mod-wsgi

 

 

步骤2:

将你的网站/网页项目拷贝至Ubuntu目录下,比如我的django项目名叫search,拷贝后的目录为/home/dong/education/search

执行命令:ifconfig

在eth0中中岛inet addr后面的就是你的ip地址。比如我的ip地址是:inet addr:192.168.32.128

执行命令:vim /etc/hosts

在末尾添加如下内容

127.0.0.1       search.com

192.168.32.128 search.com

 

 

步骤3:

执行命令:

vim /etc/apache2/apache2.conf

 

在末尾添加这几行

LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so

#Server Name
ServerName 127.0.0.1

#this is the domain which you want to visit
ServerName search.com

 

步骤4:

执行命令:vim /etc/apache2/sites-available/search.conf

添加如下内容:

<VirtualHost *:80>
    ServerName www.app.com
    ServerAlias app.com
    ServerAdmin luzhushen@gmail.com
  
    <Directory /home/chaoma/education/search>
        Require all granted
    </Directory>
  
    WSGIScriptAlias / /home/chaoma/education/search/search/wsgi.py
  
    <Directory /home/chaoma/education/search/search>
    <Files wsgi.py>
        Require all granted
    </Files>
    </Directory>
</VirtualHost>

保存

 

注意:如果没有域名和公网ip,是部署在虚拟机中,直接用虚拟机的ip地址(ifconfig可以查看到)来代替 ServerName 和 ServerAlias 后面的内容。如下面红色部分所示

<VirtualHost *:80>
    ServerName 192.168.32.138
    ServerAlias 192.168.32.138
    ServerAdmin luzhushen@gmail.com
  
    <Directory /home/chaoma/education/search>
        Require all granted
    </Directory>
  
    WSGIScriptAlias / /home/chaoma/education/search/search/wsgi.py
  
    <Directory /home/chaoma/education/search/search>
    <Files wsgi.py>
        Require all granted
    </Files>
    </Directory>
</VirtualHost>

 

用户只需把上述红色ip地址替换成你自己的虚拟机或者Ubuntu中的ip地址即可访问

 

 

 

步骤5:

执行命令:a2ensite search.conf

执行命令:service apache2 reload

执行命令:vim /home/chaoma/education/search/search/wsgi.py

将该文件中内容删除,然后用下面内容替代:

import os


PROJECT_DIR = os.path.dirname(os.path.dirname(__file__))#3
import sys # 4
sys.path.insert(0,PROJECT_DIR) # 5


os.environ["DJANGO_SETTINGS_MODULE"] = "search.settings" # 7


from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

 

注意:如果你们的项目名不是叫search,那可以将以上的search统一用你们自己的项目名替换

 

至此,全文结束。

验证:

在ubuntu的浏览器中输入(注意不是你的主机的浏览器):你自己网站或网页的地址,可以访问了

 

posted @ 2017-12-02 17:50  A·DONG  阅读(257)  评论(0编辑  收藏  举报