阿里云Ubuntu部署Django2.0

1. 使用python3.5

apt-get update
rm -rf /usr/bin/python
ln /usr/bin/python3.5 /usr/bin/python

2.pip

apt-get install python3-pip
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py

3.安装环境

pip install django==2.0  #安装django
pip install pymysql       # 安装pymysql

4.xshell安装远程文件传输工具命令

apt-get install lrzsz

4.uwsgi (处理动态)

pip3 install uwsgi
vi /home/test.py
uwsgi --http :8000 --wsgi-file test.py

5.测试uwsgi是否成功

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b'hello world']

uwsgi --http :8000 --wsgi-file test.py

6.uwsgi配置文件

uwsgi.ini文件放置在项目路径下

[uwsgi]
chdir = /home/checksystem
module = checksystem.wsgi:application
socket = 127.0.0.1:8000
processes = 4     # 进程数
max-requests = 5000   #最多请求数,超过后会重启进程,防止内存泄漏
harakiri = 60   # 最大请求时间60 S
master = true
daemonize = /home/checksystem/run.log
disable-logging = true
vacuum = True
# 不用root用户
uid = 1000
gis = 2000
wsgi-file = /home/checksystem/checksystem/wsgi.py
pidfile= /home/checksystem/uwsgi.pid
# 配置好后启动uwsgi
uwsgi --reload /home/checksystem/uwsgi.ini
uwsgi --ini /home/checksystem/uwsgi.ini
ps -aux | grep uwsgi

杀死所有uwsgi进程

ps -aux | grep uwsgi | awk '{print $2}' | xargs kill -9

项目上传解压

// 拖拽到 /home
安装解压文件工具
apt-get install zip
unzip checksystem.zip

Nginx(处理静态文件)

apt-get install nginx
cd /etc/nginx
ls
cd sites-available
vi checksystem.conf  # 创建配置文件如下
# 链接
ln -s /etc/nginx/sites-availabl/checksystem.conf /etc/nginx/sites-enabled/checksystem.conf

查看是否成功

检查语法是否正确

nginx -t

Nginx
重启nginx服务器
service nginx start
service nginx reload

service nginx restart
service nginx stop

7.nginx配置

server{
    listen 80 default_server;
	listen [::]:80 default_server;
	charset utf-8;
	root /var/www/html;
	
	# Add index.php to the list if you are using PHP
	index index.html index.htm index.nginx-debian.html;
	server_name 47.101.152.165;
	location / {
		include /etc/nginx/uwsgi_params;
        uwsgi_pass 127.0.0.1:8000; 
	}
	location /static {
		alias /home/checksystem/static;
	}
    location /media {
        alias /home/checksystem/media
    }
}

8.收集静态文件并重启服务

项目中settings.py中添加
STATIC_ROOT = '/home/checksystem/static'
在项目路径下checksystem项目下
python manage.py collectstatic
重启服务
service nginx reload
uwsgi --reload /home/checksystem/uwsgi.ini
uwsgi --ini /home/checksystem/uwsgi.ini
查看进程命令:netstat -tnlp
发现Nginx和uwsgi进程开启状态
杀死进程:pkill -9 nginx
pkill -9 uwsgi

外部本地Navicat远程访问阿里云数据库参考:https://blog.csdn.net/snailmann/article/details/79054547

nginx 状态查看

systemctl status nginx 
ps -ef | grep nginx

你可能感兴趣的:(Django)