本文主要记录使用阿里云搭建 python+django+mysql 的开发环境,因为中间自己遇到了不少问题,整理了正确的步骤避免大家再碰壁而浪费时间。应该把精力花再更有价值的地方。

阿里云服务器已经预装好了 python2.7.6,所以我们无需再次安装  

1
2
3
4
root@aliyun:~# python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
\[GCC 4.8.2\] on linux2
Type "help", "copyright", "credits" or "license" for more information.

首先执行一下 apt-get update 命令来更新一下数据源,避免本地有些软件的地址是错误的导致安装过程中404错误。

mycode@aliyun:~$ sudo apt-get update

配置虚拟环境

1
2
3
4
5
mycode@aliyun:~$ sudo apt-get install python-virtualenv
mycode@aliyun:~$ sudo easy\_install virtualenvwrapper
mycode@aliyun:~$ mkdir $HOME/.virtualenvs
mycode@aliyun:~$ echo export WORKON\_HOME=$HOME/.virtualenvs >> ~/.bashrc
mycode@aliyun:~$ echo source /usr/local/bin/virtualenvwrapper.sh >> ~/.bashrc

断开终端连接,使用相同用户重新登录,创建虚拟环境

1
2
3
4
mycode@aliyun:~$ mkvirtualenv webchat
New python executable in webchat/bin/python
Installing setuptools, pip...done.
(webchat)mycode@aliyun:~$

使用 mkvirtualenv [虚拟环境名称] 命令创建虚拟环境后,命令行会直接切换到 webchat 的虚拟环境,在这个虚拟环境中安装的软件需要使用 pip 命令,并且安装的软件并不会在真实系统中,要退出虚拟环境,使用 deactivate 命令,要删除虚拟环境,使用 rmvirtualenv 命令。

在安装 django 环境前,我们还需要安装几个必须的组件。

1、mysql-server

2、mysql-client

3、python-dev

4、libxml2-dev

5、libxslt-dev

6、zlib1g-dev

7、libmysqld-dev

8、libjpeg-dev

9、libpcre3

10、libpcre3-dev

在非虚拟环境中,执行如下命令安装我们必备的组件

1
mycode@aliyun:~$ sudo apt-get install mysql-server mysql-client python-dev libxml2-dev libxslt-dev zlib1g-dev libmysqld-dev libjpeg-dev

切换到虚拟环境中安装 django 所需的各种组件,这些组件不会被直接安装到系统,因为可能每个项目所需这些组件的版本号不同,所以只在虚拟环境中安装,执行如下命令。

1
(webchat)mycode@aliyun:~$ pip install Django ipdb ipython lxml MySQL-python Pillow wheel djangorestframework httplib2 requests uWSGI wechat-sdk

创建新的 django 项目

1
(webchat)mycode@aliyun:~$ django-admin startproject itcast\_webchat

设置使用 mysql 数据库,进入django开发目录(itcast_webchat/itcast_webchat/settings.py),编辑 setting.py 配置文件,将如下内容注释

1
2
3
4
5
6
#DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE\_DIR, 'db.sqlite3'),
# }
#}

添加 mysql 的设置信息,添加完成后会使用 mysql 数据库,要注意,里面设置的 dbname 必须存在,否则你生成数据表的时候会报错找不到这个数据库

1
2
3
4
5
6
7
8
9
10
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dbname',
'USER': 'root',
'PASSWORD': 'password',
'HOST': '',
'PORT': '3306'
}
}

生成相关数据表

1
2
3
4
5
6
7
8
9
(webchat)mycode@aliyun:~$ cd itcast\_webchat/
(webchat)mycode@aliyun:~/itcast\_webchat$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, contenttypes, auth, sessions
Running migrations:
Applying contenttypes.0001\_initial... OK
Applying auth.0001\_initial... OK
Applying admin.0001\_initial... OK
Applying sessions.0001\_initial... OK

启动 django 服务

1
2
3
4
5
6
7
8
9
(webchat)mycode@aliyun:~/itcast\_webchat$ python manage.py runserver
Performing system checks...


System check identified no issues (0 silenced).
September 08, 2015 - 14:34:31
Django version 1.7.8, using settings 'itcast\_webchat.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

配置 django 关闭调试并设定可以让所有 IP 访问,进入django开发目录(itcast_webchat/itcast_webchat/settings.py),编辑 setting.py 配置文件,修改如下两个配置。

1
2
TEMPLATE\_DEBUG = False
ALLOWED\_HOSTS = \['\*',\]

安装 nginx-1.6.2,将 nginx-1.6.2 的压缩包上传到 linux 服务器或者直接从服务器上下载

解压 nginx 安装包

tar zxvf nginx-1.6.2.tar.gz

cd 进入解压后的 nginx 目录,安装所需组件

1
2
3
mycode@aliyun:/home/mycode/package/nginx-1.6.2$ sudo apt-get install libpcre3 libpcre3-dev
mycode@aliyun:/home/mycode/package/nginx-1.6.2$ sudo apt-get install libssl-dev
mycode@aliyun:/home/mycode/package/nginx-1.6.2$ sudo apt-get install openssl

执行 nginx 目录下的 configure 执行文件,该文件有两个作用,一个是环境检查,另外一个是生成 makefile

1
mycode@aliyun:/home/mycode/package/nginx-1.6.2$ ./configure

编译并安装 nginx

1
2
mycode@aliyun:/home/mycode/package/nginx-1.6.2$ make
mycode@aliyun:/home/mycode/package/nginx-1.6.2$ sudo make install

配置 nginx 反向代理,使用 VIM 编辑 /usr/local/nginx/conf/nginx.conf 文件,将如下行注释

1
2
3
4
#location / { 
# root html;
# index index.html index.htm;
#}

添加如下内容到被注释的文字后面,注意粗体标注的位置要改成你自己 django 启动时的地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
location / {
#Django address
proxy\_pass http://localhost:8000;

#Proxy Settings
proxy\_redirect off;
proxy\_set\_header Host $host;
proxy\_set\_header X-Real-IP $remote\_addr;
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;
proxy\_next\_upstream error timeout invalid\_header http\_500 http\_502 http\_503 http\_504;
proxy\_max\_temp\_file\_size 0;
proxy\_connect\_timeout 90;
proxy\_send\_timeout 90;
proxy\_read\_timeout 90;
proxy\_buffer\_size 4k;
proxy\_buffers 4 32k;
proxy\_busy\_buffers\_size 64k;
proxy\_temp\_file\_write\_size 64k;
}

启动 nginx 服务

1
mycode@aliyun:~$ /usr/local/nginx/sbin/nginx

访问你阿里云的外网 IP 地址,如果你看到了 It worked! 就是成功了。