2013年7月13日10:36:53:接上篇,bae部署django没成功,转战阿里云。。

阿里云服务器最便宜69/月,现在有个活动,新用户送20元现金券,我就花了RMB 49买了一个,操作系统选的是ubuntu1204安全加强版。

1.putty登陆远程主机

putty下载地址:http://the.earth.li/~sgtatham/putty/latest/x86/putty.zip,解压后直接运行putty.exe即可。

2.Xftp与远程主机传文件

Xftp下载地址http://www.onlinedown.net/soft/143.htm

以上两步看aliyun的帮助文档即可,easy,http://help.aliyun.com/manual?spm=0.0.0.0.ZERNiU&helpId=1846

3.安装apache+mysql+python+django

管理mysql就用phpmyadmin了,这样就得再装个php,命令如下

apt-get install apache2 php5 libapache2-mod-php5 mysql-server libapache2-mod-auth-mysql php5-mysql phpmyadmin

期间要求输mysql root用户密码和phpmyadmin的登陆密码

python系统自带了。需要装python-mysqldb

apt-get install python-mysqldb 

安装一个mod_wsgi,此物就是将Python应用部署到Apache上的。

apt-get install libapache2-mod-wsgi

链接phpmyadmin

ln -s /usr/share/phpmyadmin /var/www/phpmyadmin

python ubuntu已经自带了,需要安装一个python-mysqldb

apt-get install python-mysqldb  

django从官网下载,我当前下载的版本是1.5.1,然后按照官网的说明安装。

重启Apache2的命令:

sudo /etc/init.d/apache2 restart

4.启动一个django站点

切换到/srv目录下,启动一个django站点

django-admin.py startproject mysite  

5. 下面是配置apache了

django官方有一篇如何配置的文档,mod_wsgi官网也有如何配置的文档。主要一点mod_wsgi有两种工作模式,嵌入到Apache和作为线程的守护模式。官方推荐用守护模式,守护模式没研究,嵌入模式最简单,先用嵌入模式。

编辑Apache的配置文件

gedit /etc/apache2/sites-available/default
<VirtualHost *:80>
ServerAdmin webmaster@localhost DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory> WSGIScriptAlias /app/ /srv/mysite/mysite/wsgi.py
<Directory /srv/mysite/mysite>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory> ErrorLog ${APACHE_LOG_DIR}/error.log # Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory> </VirtualHost>

重点就是在原来文件的基础上增加了

WSGIScriptAlias /app/ /srv/mysite/mysite/wsgi.py
<Directory /srv/mysite/mysite>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

还有编辑/etc/apache2/httpd.conf文件,增加下面这行

WSGIPythonPath /srv/mysite

然后重启apache服务就OK了!

此时通过浏览器访问你的云服务器主机地址xx:xx:xx:xx/app/,就可一看到django默认页面了!

6.连接数据库

编辑mysite/settings.py,修改DATABASES一项

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'db_name', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': 'your_mysql_passwd', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}

django默认安装的应用有:

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
#'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)

运行下面的命令,同步数据库,为默认安装的应用创建table:

python manage.py syncdb

7.创建一个app+模型

输入下面的命令,创建一个叫polls的应用

python manage.py startapp polls

接下来创建模型(models),按照django官网教程的例子,编辑polls/models.py

from django.db import models

class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.question
class Choice(models.Model): poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
def __unicode__(self):
return self.choice

这里的意思创建了两个模型:Poll、Choice。__unicode__方法是定义模型默认显示的内容。

激活模型,编辑mysite/settings.py中INSTALLED_APP,如下:

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'polls',
)

同步一下数据库

python manage.py syncdb

可以在shell下输入一些python语句,为模型创建内容。进入shell环境的命令为:

python manage.py shell

例如:

>>> from polls.models import Poll, Choice   # Import the model classes we just wrote.

# No polls are in the system yet.
>>> Poll.objects.all()
[] # Create a new Poll.
>>> from django.utils import timezone
>>> p = Poll(question="What's new?", pub_date=timezone.now()) # Save the object into the database. You have to call save() explicitly.
>>> p.save() # Now it has an ID. Note that this might say "1L" instead of "1", depending
# on which database you're using. That's no biggie; it just means your
# database backend prefers to return integers as Python long integer
# objects.
>>> p.id
1

8.配置url+创建视图

编辑mysite/urls.py

urlpatterns = patterns('',
# Examples:
# url(r'^$', 'simple.views.home', name='home'),
# url(r'^simple/', include('simple.foo.urls')),
url(r'^polls/', include('polls.urls')), # Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
)

编辑polls/urls.py (此文件不存在,新建一个)

from django.conf.urls import patterns, url

from polls import views

urlpatterns = patterns('',
url(r'^$', views.index, name='home'),
url(r'^show/$', views.show_all, name='show'),
)

创建views.index视图,编辑polls/views.py

from django.http import HttpResponse  

def index(request):
return HttpResponse("Hello, world. You're at the poll index.")

此时访问主机名xx:xx:xx:xx/app/polls 就可以看到hello world视图了!

9.开启admin应用

首先编辑settings.py,打开admin应用

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'polls',
)

同步下数据库

python manage.py syncdb

编辑mysite/urls.py,打开admin的视图

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover() urlpatterns = patterns('',
# Examples:
# url(r'^$', 'simple.views.home', name='home'),
# url(r'^simple/', include('simple.foo.urls')),
url(r'^$', 'polls.views.index'),
url(r'^polls/', include('polls.urls')), # Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)

设置admin的静态文件。首先编辑apache2的配置文件,让apache提供静态文件的服务。编辑完之后,需要重启apache。

<VirtualHost *:80>
ServerAdmin webmaster@localhost DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory> AliasMatch ^/([^/]*\.css) /srv/mysite/static/styles/$1 Alias /media/ /srv/mysite/media/
Alias /static/ /srv/mysite/static/ <Directory /srv/mysite/static>
Order deny,allow
Allow from all
</Directory> <Directory /srv/mysite/media>
Order deny,allow
Allow from all
</Directory> WSGIScriptAlias / /srv/mysite/mysite/wsgi.py
<Directory /srv/mysite/mysite/>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory> ErrorLog ${APACHE_LOG_DIR}/error.log # Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory> </VirtualHost>

在/srv/mysite/下新建一个static和media文件夹,将django/contrib/admin/static/admin这个文件夹复制到/srv/mysite/static/下。

在浏览器试一下,xx:xx:xx:xx/admin,此时有admin界面了。

注:这里有个建议,删掉polls/urls.py,将链接分发统一放到mysite/urls.py这一个文件中去。原因是,过程中遇到一些莫名的问题,放在一个文件中就OK,此问题遗留。

下面把我们刚才创建的模型注册到admin中去,(admin的作用无需解释,走一遍django tutorial就了解了)。

在polls/下新建一个admin.py,编辑其内容如下即可:

from django.contrib import admin
from polls.models import Poll admin.site.register(Poll)

至此,本文结束,阿里云+django搭建完毕,剩下的是django的开发工作了。

阿里云+django实战记录的更多相关文章

  1. ***阿里云ECS实战配置虚拟主机 + Apache 配置虚拟主机三种方式

    阿里云ECS实战配置虚拟主机 买了一台ECS阿里云服务器,性能感觉有点富余,想着可以陪着虚拟主机多一些WWW目录好放一些其他的程序.比如DEMO什么的. 今天研究了下,主要是就是做基于不同域名的虚拟主 ...

  2. 阿里云ESC服务器配置记录

    购买服务器 上周赶着活动购买了一年阿里云服务器,记录一下配置过程: 选择服务器类型: linux服务器,网上说一般都用centOS的"比较新"的版本: 重置密码: 重置密码之后一定 ...

  3. 阿里云 django的一次web维护记录

    首先, 丢给我一个阿里云的server的账号/password,之前没有玩过阿里云,想想应该也是ssh服务来远程登陆. 环境: centos+nginx+uwsgi+python2.7+django. ...

  4. 基于freescale i.Mx6(ARM)的阿里云oss调试记录

    交叉编译阿里OSS调试记录 1.1 开通oss服务 具体参考以下链接: https://help.aliyun.com/document_detail/31884.html?spm=a2c4g.111 ...

  5. 阿里云服务器实战(二): Linux MySql5.6数据库乱码问题

    在阿里云上了买了一个云服务器, 部署了一个程序,发现插入数据库后乱码了,都成了'????'.  一开始怀疑是Tomcat7的原因 , 见文章 : http://blog.csdn.net/johnny ...

  6. laravel项目使用appnode部署linux系统到阿里云服务器流程记录(待补充)

    使用 SSH 连接工具,如 PuTTY.XShell.SecureCRT 等,连接 Linux 服务器后(阿里云服务器命令行内直接输入appnode安装命令,版本:mysql选5.7.php选7.2) ...

  7. 阿里云nas使用记录

    公司买了阿里云的nas服务用来共享存储,多个web服务器共同挂载同一个nas服务.挂载过程中出现如下报错 NAS报错: [root@BJ-SBC fs]# mount -t nfs 10.10.8.1 ...

  8. 阿里云服务器实战(一) : 在Linux下Tomcat7下使用连接池

    云服务器 的环境如下: Tomcat7+MySql5.6 一,如果自定义了程序的文件目录 , 下面的/alidata/xxx 就是自定义的目录 在Linux的Tomcat的server.xml里的Ho ...

  9. 阿里云 Django部署参考

    Linux下安装Python3和django并配置mysql作为django默认服务器 CentOS7.3安装Python3.6 yum except KeyboardInterrupt, e: 错误 ...

随机推荐

  1. ireport报表,打印时,报表加载失败的解决方法

    1.报表加载失败图示 2.解决方法 原创作者:DSHORE 作者主页:http://www.cnblogs.com/dshore123/ 原文出自:http://www.cnblogs.com/dsh ...

  2. Effective C++笔记(五):实现

    参考:http://www.cnblogs.com/ronny/p/3754755.html 条款26:尽可能延后变量定义式的出现时间 有些对象,你可能过早的定义它,而在代码执行的过程中发生了导常,造 ...

  3. hdu 5833(欧拉路)

    The Best Path Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  4. IEEEXtreme 10.0 - Mancala'h

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Mancala'h 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank.c ...

  5. 【PAT】1006. 换个格式输出整数 (15)

    1006. 换个格式输出整数 (15) 让我们用字母B来表示“百”.字母S表示“十”,用“12...n”来表示个位数字n(<10),换个格式来输出任一个不超过3位的正整数.例如234应该被输出为 ...

  6. CodeForces 803F Coprime Subsequences

    $dp$. 记$dp[i]$表示$gcd$为$i$的倍数的子序列的方案数.然后倒着推一遍减去倍数的方案数就可以得到想要的答案了. #include <iostream> #include ...

  7. 洛谷P3168 [CQOI2015]任务查询系统 [主席树,差分]

    题目传送门 任务查询系统 题目描述 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任 ...

  8. XV6操作系统代码阅读心得(三):锁

    锁是操作系统中实现进程同步的重要机制. 基本概念 临界区(Critical Section)是指对共享数据进行访问与操作的代码区域.所谓共享数据,就是可能有多个代码执行流并发地执行,并在执行中可能会同 ...

  9. java抽象类、多态、接口

    抽象类 抽象类的产生 当编写一个类时,我们往往会为该类定义一些方法,这些方法是用来描述该类的功能具体实现方式,那么这些方法都有具体的方法体. 但是有的时候,某个父类只是知道子类应该包含怎么样的方法,但 ...

  10. Mybatis 源码分析之一二级缓存

    一级缓存 其实关于 Mybatis 的一级缓存是比较抽象的,并没有什么特别的配置,都是在代码中体现出来的. 当调用 Configuration 的 newExecutor 方法来创建 executor ...