在阿里云上使用Ubuntu16.04 + Nginx + Gunicorn部署Django项目
在本机上访问Django项目和在线上访问Django项目是两种不同的需求体验,前者主要要求是可以进行调试,通常是个人访问;但部署在后者上则会有人来访问,需要考虑并发问题,因此使用Django自带的runserver不能满足需要。
环境:阿里云服务器、已购买域名并备案、python 3.6.4 虚拟环境、Ubuntu16.04
请先参照我的这篇文章完成虚拟环境配置再进行以下操作。
https://blog.csdn.net/u014793102/article/details/80302975
0 生成requirements.txt
在开始之前,确保你自己的电脑是可以运行项目的。请在自己的电脑上进入虚拟环境后,使用 pip freeze > requirements.txt 命令生成requirements.txt。
(venv)duke@coding:/myproject$ pip freeze > requirements.txt
项目所需要的依赖环境都会在这个txt里,将此txt上传至git项目中,以便于在阿里云服务器中使用这些依赖环境。
1.1 阿里云安全组设置
通过阿里云控制台进行安全组的设置,开放以下端口。
开放端口后,需要在控制台重启服务器(不是进入服务器重启)。
1.2 安装相关软件
- 安装mysql
root@coding:~# apt-get update
root@coding:~# apt-get install mysql-server
The following NEW packages will be installed:
libcgi-fast-perl libcgi-pm-perl libencode-locale-perl libevent-core-2.0-5
libfcgi-perl libhtml-parser-perl libhtml-tagset-perl libhtml-template-perl
libhttp-date-perl libhttp-message-perl libio-html-perl
liblwp-mediatypes-perl liburi-perl mysql-client-5.7 mysql-client-core-5.7
mysql-common mysql-server mysql-server-5.7 mysql-server-core-5.7
0 upgraded, 19 newly installed, 0 to remove and 115 not upgraded.
Need to get 18.6 MB of archives.
After this operation, 162 MB of additional disk space will be used.
Do you want to continue? [Y/n]
Y
#输入两次MySQL密码
安装nginx
root@coding:~# apt-get install nginx The following NEW packages will be installed: fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libvpx3 libxpm4 libxslt1.1 nginx nginx-common nginx-core 0 upgraded, 10 newly installed, 0 to remove and 115 not upgraded. Need to get 2715 kB of archives. After this operation, 8752 kB of additional disk space will be used. Do you want to continue? [Y/n] Y
1.3 项目准备
首先要把本地项目放到服务器上,一种是直接用scp命令把本地项目复制到服务器上,另外一种是用git。在这里我建议大家使用git,至于其中的方便和好处实在太多,可以自己去多了解以下。
项目主页:https://coding.net/u/ssh_jie/p/b2c-shop(我这个是私有项目,因此下面要输入名户名和密码,如果你的项目是公有项目,会直接clone)