django 阿里云上 ubuntu-x64 python+django+mysql 开发环境搭建


https://github.com/nmgwddj/itcast_wechat//微信gethub网址
http://www.mycode.net.cn/platform/linux-unix/938.html //

阿里云 ubuntu-x64 python+django+mysql 开发环境搭


本文主要记录使用阿里云搭建 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
在非虚拟环境中,执行如下命令安装我们必备的组件
mycode@aliyun:~$ sudo apt-get install mysql-server mysql-client python-dev libxml2-dev libxslt-dev zlib1g-dev libmysqld-dev

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

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

创建新的 django 项目

(webchat)mycode@aliyun:~$ django-admin startproject itcast_webchat
 
   
django-admin startproject text

设置使用 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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值