Toggle navigation


Django镜像手册

Version DJ1.11.10_Py2.7.14

由Websoft9提供一个已经安装好的官方Django&Python运行环境。 Django(djangoproject.com)是一个由Python编写的高性能Web应用程序开发框架,帮助开发者以最少代码快速的、简单的开出使用的应用程序。框架采用了MVC的模式,强调代码复用,遵循功能组件的即插即用。它最初是被开发来用于管理劳伦斯出版集团旗下的一些以新闻内容为主的网站的,即是CMS(内容管理系统)软件。并于2005年7月在BSD许可证下发布。这套框架是以比利时的吉普赛爵士吉他手Django Reinhardt来命名的。 * * *本文档适用于通过主流云市场购买和部署了Websoft9镜像的用户,文档内容重点指导用户进行安装配置、系统基本操作和常见运维问题处理。 文档适用性说明:

  • 适用的主流云平台包括:阿里云、腾讯云、百度云、华为云、Azure、青云
  • 适用的操作系统包括:Windows
  • 适用的阅读者包括:初学者、学生、站长、运维人员、产品经理、开发者、UI

##### 包含软件

– Nginx 1.12.2
– MySQL 5.6.39
– Python 2.7.5
– uwsgi 2.0.15
– Virtualenv 15.1.0
– Django 1.11 LTS

##### 目录结构

– Nginx

“`
配置文件路径:
/etc/nginx/nginx.conf
虚拟主机配置文件:
/etc/nginx/conf.d/default.conf
“`

– MySQL

“`
MySQL配置文件:

/etc/my.cnf

MySQL 数据库存储目录:

/data/mysql/
“`

– uwsgi

“`
uwsgi配置文件及目录:

/etc/uwsgi.ini

/etc/uwsgi.d/
“`

##### 网站推荐存放路径

`/data/wwwroot/`

服务器配置要求

本镜像1核1G内存即可运行

  1. 镜像需要与服务器配套使用,获取Websoft9的镜像有两种方式:
    方式一:若没有可用的云服务器,登录主流云厂商的云市场,找到由Websoft9提供的“PostgreSQL”相关免费镜像,点击“购买”(同时会配套购买云服务器,若只打算试用请选择“按量”方式购买,实现按小时使用,接近免费)
    方式二:若有可用的云服务器,登录到云厂商的控制面板,找到可用的云服务器,通过关机->更换系统盘(重装镜像),在更换过程中选取云市场镜像,获取本镜像
  2. 待镜像购买或更换完成后,镜像会自动安装到云服务器上,待实例处于“运行中”后,访问网址:http://服务器公网IP/,正常会出现相关界面

如何使用Django Stack的命令行?

From the Start Menu, click the Start -> Bitnami Django Stack -> Use Django shortcut.

How to start with the sample Django project?

Access the Bitnami Django Stack via your browser at http://localhost (Windows) or http://localhost:8080(Mac OS X and Linux).

Access the sample project via your browser at http://localhost/Project (Windows) orhttp://localhost:8080/Project (Mac OS X and Linux).

The Django project files are located at installdir/apps/django/django_projects.

Here is an example of what you will see:

How to create a new Django project?

the projects folder is located at C:\Users\USER\Bitnami DjangoStack Projects.

How to start a Django project?

For development, use the standalone server in Django by executing the following command inside your project folder:

$ python manage.py runserver IP:PORT

IP and PORT are optional parameters. 127.0.0.1:8000 will be used by default if you don’t specify any of them. If you plan to remotely access the server, you should use the IP address 0.0.0.0, or the server’s network IP address.

IMPORTANT: Remember to open the port used by the Django server in the server firewall. Refer to these instructions for more information.
Production deployment

For a production environment, we recomend configuring Apache with the mod_wsgi module before starting to serve your application. On Bitnami Cloud Images and Virtual Appliances, mod_wsgi is already installed and activated by default.

1. First, modify the WSGI application script file atinstalldir/apps/django/django_projects/PROJECT/PROJECT/wsgi.py. Remember to replace PROJECT with the actual path and name to your Django project.

Although the exact content may be different depending on your Django version, ensure that the content is similar to the code below and that you add your project to the path with sys.path.append.

import os
import sys
sys.path.append('installdir/apps/django/django_projects/PROJECT')
os.environ.setdefault("PYTHON_EGG_CACHE", "installdir/apps/django/django_projects/PROJECT/egg_cache")

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "PROJECT.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
2. Create a conf/ folder in the installdir/apps/django/django_projects/PROJECT/ directory.
$ mkdir installdir/apps/django/django_projects/PROJECT/conf

3. Create the following files:

touch installdir/apps/django/django_projects/PROJECT/conf/httpd-prefix.conf
touch installdir/apps/django/django_projects/PROJECT/conf/httpd-app.conf
4. On Linux and Mac OS X, you can run the application with mod_wsgi in daemon mode. Add the following code in installdir/apps/django/django_projects/PROJECT/conf/httpd-app.conf:
<IfDefine !IS_DJANGOSTACK_LOADED>
  Define IS_DJANGOSTACK_LOADED
  WSGIDaemonProcess wsgi-djangostack processes=2 threads=15 display-name=%{GROUP}
</IfDefine>

WSGIScriptAlias /PROJECT 'installdir/apps/django/django_projects/PROJECT/PROJECT/wsgi.py'
<Directory "installdir/apps/django/django_projects/PROJECT/PROJECT/">
    WSGIProcessGroup wsgi-djangostack
    WSGIApplicationGroup %{GLOBAL}
    <IfVersion < 2.3 >
        Order allow,deny
        Allow from all
    </IfVersion>
    <IfVersion >= 2.3>
        Require all granted
    </IfVersion>
</Directory>
On Windows, the daemon mode for mod_wsgi is not support and the configuration will be slightly different:
WSGIScriptAlias /PROJECT 'installdir/apps/django/django_projects/PROJECT/PROJECT/wsgi.py'
<Directory "installdir/apps/django/django_projects/PROJECT/PROJECT/">
    WSGIApplicationGroup %{GLOBAL}
    <IfVersion < 2.3 >
        Order allow,deny
        Allow from all
    </IfVersion>
    <IfVersion >= 2.3>
        Require all granted
    </IfVersion>
</Directory>

5. In the installdir/apps/django/django_projects/PROJECT/conf/httpd-prefix.conf file, add this code:

Include "installdir/apps/django/django_projects/PROJECT/conf/httpd-app.conf"

6. Add the line below to the installdir/apache2/conf/bitnami/bitnami-apps-prefix.conf file:

Include "installdir/apps/django/django_projects/PROJECT/conf/httpd-prefix.conf"

7. Restart the Apache server.

$ installdir/ctlscript.sh restart apache

You should now be able to browse to your project at http://[server-IP-address]/PROJECT (Windows) orhttp://[server-IP-address]/PROJECT (Linux and Mac OS X).

 

If you wish to configure a database for your project, configure the settings.py file with the following settings. The examples below show how to configure the database connection with a database called “djangostack”. Remember to replace /opt/bitnami with the actual path to your stack installation directory for native installers.

For MySQL:

 DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'djangostack',
        'HOST': '/opt/bitnami/mysql/tmp/mysql.sock',                                                 
        'PORT': '3306',
        'USER': 'USERNAME',
        'PASSWORD': 'PASSWORD'
    }
}

For PostgreSQL:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'djangostack',
        'HOST': '/opt/bitnami/postgresql',
        'PORT': '5432',
        'USER': 'USERNAME',
        'PASSWORD': 'PASSWORD'
    }
}
NOTE: The database connection in the above configuration file is configured to use a socket. If you are using the Bitnami Django Stack on Windows, you should instead replace the HOST parameter with ‘HOST’: ‘127.0.0.1’.

For SQLite:

 DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': '/opt/bitnami/apps/django/django_projects/Project/Project/Project.db',
    }
}

应用程序安装、使用和维护中需要用到两种账户:

1、MySQL账号和密码:默认账号是root,密码为123456

2、PostgreSQL账号和密码:默认账号是postgres,密码为123456


2、服务器操作系统账号和密码:默认账号是administrator,密码是自行设置的

  • Windows服务器的操作主要分为两个部分:
    • 远程桌面连接:通过PC自带的远程桌面工具远程连接到服务器,完全控制服务器的操作
    • FTP文件管理:通过FTP客户端和Windows服务器上的FTP服务端进行通讯,实现文件管理的操作的目的

    具体参考:

网站的定期备份和重要操作之前的备份工作(如系统升级或重要修改)无比重要,可以做到防范于未然,让发生了意外而能够进行复原,这是网站运维工作的重中之重。

PostgreSQL的备份有基于服务器快照自动备份、手动本地备份两种方式,两种凡是各有利弊,建议结合起来使用

服务快照自动备份

云服务器厂商都会提供一个叫“快照”的功能,快照是针对于服务器的磁盘来说的。快照工具可以记录指定时间点硬盘的数据,全部备份起来,并可以实现一键恢复。

如果您对磁盘进行了自动的快照设定,如下场景您便可以比较从容的面对:
1、工作了几天的成果,被误删了
2、黑客把把网站网站弄的面目全非
3、现在的内容已经乱七八糟了,我要回到一个正常的服务器状态

做了快照备份,一键恢复到备份时间点的状态。也就是现场还原,很好的功能。

不同的云厂商快照设置稍有差别,具体参考:

待续…

服务随操作系统自动启动,如果手工修改配置参数后,需要重新启停服务,有两种方法。

方法一:在“开始”-> “管理工具”->“服务”中重启django***服务。

方法二:在C:\websoft9\django*中找到“manager-windows”。

双击打开“manager-windows”,可以进行服务启停、参数配置。

本文档由长沙网久软件有限公司原创,为了保证文档的严肃性和可用性,任何人不得盗用或修改后二次分发。需要转载请标注来源,否则我们保留给予法律追究的权利。

我们会定期更新文档,请访问在线文档以获取最新版本

本文档在线地址:http://docs.websoft9.com/xdocs/django-bitnami-image-guide/
合作或技术支持:http://www.websoft9.com