本文主要记录使用阿里云搭建 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:~$
libjpeg-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 = ['*',]
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! 就是成功了。
“添加如下内容到被注释的文字后面,注意粗体标注的位置要改成你自己 django 启动时的地址 ”
这里不清楚,启动nginx报错:
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
访问ip报错:
Sorry, the page you are looking for is currently unavailable.
Please try again later.
If you are the system administrator of this resource then you should check the error log for details.
Faithfully yours, nginx.
报错内容是绑定80失败,检查哪个程序占用了80端口。
您好楼主,我在nginx中make和make install的时候出现了make
: Leaving directory `/root/myweb_vipa/nginx-1.6.2’,不太明白这样是不是没有编译成功
这个问题我没有碰到过,google 一下吧。