本文主要记录使用阿里云搭建 python+django+mysql 的开发环境,因为中间自己遇到了不少问题,整理了正确的步骤避免大家再碰壁而浪费时间。应该把精力花再更有价值的地方。
阿里云服务器已经预装好了 python2.7.6,所以我们无需再次安装
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
配置虚拟环境
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
断开终端连接,使用相同用户重新登录,创建虚拟环境
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
在非虚拟环境中,执行如下命令安装我们必备的组件
mycode@aliyun:~$ sudo apt-get install mysql-server mysql-client python-dev libxml2-dev libxslt-dev zlib1g-dev libmysqld-dev libjpeg-dev
切换到虚拟环境中安装 django 所需的各种组件,这些组件不会被直接安装到系统,因为可能每个项目所需这些组件的版本号不同,所以只在虚拟环境中安装,执行如下命令。
(webchat)mycode@aliyun:~$ pip install Django ipdb ipython lxml MySQL-python Pillow wheel djangorestframework httplib2 requests uWSGI wechat-sdk
创建新的 django 项目
(webchat)mycode@aliyun:~$ django-admin startproject itcast_webchat
设置使用 mysql 数据库,进入django开发目录(itcast_webchat/itcast_webchat/settings.py),编辑 setting.py 配置文件,将如下内容注释
#DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
#}
添加 mysql 的设置信息,添加完成后会使用 mysql 数据库,要注意,里面设置的 dbname 必须存在,否则你生成数据表的时候会报错找不到这个数据库
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dbname',
'USER': 'root',
'PASSWORD': 'password',
'HOST': '',
'PORT': '3306'
}
}
生成相关数据表
(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 服务
(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 配置文件,修改如下两个配置。
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 目录,安装所需组件
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
mycode@aliyun:/home/mycode/package/nginx-1.6.2$ ./configure
编译并安装 nginx
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 文件,将如下行注释
#location / {
# root html;
# index index.html index.htm;
#}
添加如下内容到被注释的文字后面,注意粗体标注的位置要改成你自己 django 启动时的地址
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 服务
mycode@aliyun:~$ /usr/local/nginx/sbin/nginx
访问你阿里云的外网 IP 地址,如果你看到了 It worked! 就是成功了。
相关