使用Apache与mod_wsgi部署Django应用到服务器

使用Apache与mod_wsgi部署Django应用到服务器

前言

之前没有做过网络应用,觉得它非常神奇,竟然可以共享全世界那么多资源和应用!  
看到别人好多都有自己的博客网站,心里也是痒痒得。
今天花了些时间把它搭建起来!

准备

环境说明
公有IP服务器例如,我的域名:121.42.53.153
服务器操作系统Linux
Python版本 2.7.6
Django1.9.7
pip版本 8.1.2

环境配置

  1. 远程登录服务器

    通过类Linux命令行远程服务器,如(mac端):


    cslzy@mac$ ssh root@121.42.53.153

    然后会有提示输入密码。
    root用户登录,权限是最高级的。这样在安装工具时会方便很多,但一定要慎重(尤其是在修改、删除文件之前)。

  2. 安装pip

    (sudo) apt-get install python-pip
    不是root用户要加上sudo,下面不加说明都是用的超级权限。

  3. 使用pip安装Django

    pip install django

    注意 可能会出现这样的错误信息:

    “SSLError: The read operation timed out” when using pip

    stackoverflow上找到的解决方法:
    pip --default-timeout=100 install django

    使用下面的方法检查是否安装成功:

    root ~ $ python
    Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
    [GCC 4.8.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import django
    >>> print django.get_version()
    1.9.7
    >>> 
  4. 安装Apache2、mod_wsgi及libapache2-mod-wsgi

    安装命令如下:

    root ~ $ apt-get install apache2 libapache2-mod-wsgi

    安装参考

创建测试Django空工程

  1. 创建文件所在目录的权限可能会导致网站访问出错。所以一般不推荐在主目录下创建。或者,直接用

    root ~ $ chmod 655 [your director]

    更改文件目录的读、写、可执行权限。其主要目的是为了确保工程文件下的wsgi.py文件对apache运行的用户具有可读权限。

  2. 创建

    比较多的教程用都是在/var/www/目录下创建的,我们也在这创建。

    root www $ django-admin.py startproject [your appname]

    [your appname]随便写一个就行,可以是hello world, again~,我用的是serverTest.

    看一下它的读写权限:

    root www $ ls -l
    drwxr-xr-x 3 root root 4096 Jun 29 12:28 serverTest

Apache中对mod_wsgi的配置

  1. 编辑.conf文件

    打开目录/etc/apache2,里面有个sites-availbale目录(如果没有,自己mkdir一个),在这里面为自己的工程添加一个wsgi配置文件。

    root sites-available $ vim servertest2.conf 
    
    WSGIScriptAlias /test2 /var/www/serverTest/serverTest/wsgi.py
    WSGIPythonPath /var/www/serverTest
    
    <Directory /var/www/serverTest/serverTest>
    <Files wsgi.py>
    Order deny,allow
    Allow from all
    </Files>
    </Directory>

    编辑完成后,保存退出。
    其中,/test2,跟在域名或者IP之后的名字,如:121.42.35.153/test2。在服务器中输入这个就能打开相关网页(当然,要执行下面的重启才行,同时,在django里面的urls.py的映射里也要写入相应规则。为了方便起见,下面的配置里,使用/代替/test2)。其它详细解释点我

  2. 激活、重启服务

    root sites-available $ vim servertest2.conf 
    root sites-available $ a2ensite servertest2.conf 
    Site servertest2 already enabled
    root sites-available $ service apache2 reload
     * Reloading web server apache2                                                      * 
  3. 关联域名和服务器IP

    这个过程是向DNS服务器提交域名和IP的关联规则,每个域名服务商都会提供这个免费的服务。
    我是在阿里买的域名,设置过程如下:

    • 登录「管理控制台」,点击云解析
    • 点击域名我条目后面的「解析」
    • 跳转到新页面后,点击「解析设置」,添加解析
    • 把服务器的IP填上就行。

    基本上一会就可能使用域名来访问网站了。Amazing!
    参考文章
    细节可能会有些出入,大体还是一样的。

我遇到的错误

  1. 找不到settings.py路径

    Internal Server Error
    
    The server encountered an internal error or misconfiguration and was unable to complete your request.
    
    Please contact the server administrator at webmaster@localhost to inform them of the time this error occurred, and the actions you performed just before this error.
    
    More information about this error may be available in the server error log.
    
    Apache/2.4.7 (Ubuntu) Server at sofamiri.com Port 80

    解决方法

    wsgi.py中,把当前工程的路径加到系统路径中。
    修改之后如下:

    """
    WSGI config for hellodjango project.
    
    It exposes the WSGI callable as a module-level variable named ``application``.
    
    For more information on this file, see
    https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/
    """
    
    import os
    import sys
    
    root = os.path.join(os.path.dirname(__file__), '..') # add parent path
    sys.path.insert(0, root) # add to sys path
    
    from django.core.wsgi import get_wsgi_application
    
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "serverTest.settings")
    
    application = get_wsgi_application()
    
  2. Not Found:

    Not Found
    
    The requested URL / was not found on this server.
    
    Apache/2.4.7 (Ubuntu) Server at 121.42.35.153 Port 80

    试一下在后面加上admin,如果有后台管理界面出现,说明,你的服务器已经配置好了。只不过没有在django里面写入对应的路径解析。如,默认的urls.py中的内容:

    from django.conf.urls import url
    from django.contrib import admin
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls), #在这后面加上 `/`对就的规则
    ]

    详见这里

  3. Forbidden

    
    You don't have permission to access / on this server.
    
    Apache/2.4.7 (Ubuntu) Server at 121.42.35.153 Port 80

    这个需要检查一下相应wsgi.py的读写权限。

    root serverTest $ chmod 644 wsgi.py
    root serverTest $ ls -l | grep wsgi
    -rw-r--r-- 1 root root  591 Jun 29 17:26 wsgi.pyc

    只需要修改wsgi.py让其它用户也具有可读权限即可。

  4. Bad Request

    出现Bad Request (400)是因为关联到域名之后没有及时修改settings.py中的ALLOWED_HOSTS,如,修改你的相应域名。我的:ALLOWED_HOSTS = ['.sofamiri.com',]

参考

PyConAU 2010: Getting started with Apache/mod_wsgi这个里面讲了一些常见的错误,比较实用。

It worked!

It worked!
Congratulations on your first Django-powered page.

Of course, you haven't actually done any work yet. Next, start your first app by running python manage.py startapp [app_label].
You're seeing this message because you have DEBUG = True in your Django settings file and you haven't configured any URLs. Get to work!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值