Django项目部署

连接linux的ip
  • linux下
    先更新:sudo apt-get update
    安装ssh:sudo apt install ssh

  • secureCRT连接linux
    在这里插入图片描述

移动项目

secureCRT中,alt+p进入SFTP

lcd 项目文件在win下的路径
cd 想要放置项目的ubuntu路径
put -r 项目文件夹名
配置项目虚拟环境
  • 设置python下载源
    复制pip.conf到linux的.pip文件夹下
## pip.conf

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=pypi.tuna.tsinghua.edu.cn
  • 安装virtualenv
wsm@ubuntu:~/.pip$ cat pip.conf 
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=pypi.tuna.tsinghua.edu.cnwsm@ubuntu:~/.pip$ pip3 install virtualenv
  • 创建虚拟环境virtualenv venv
    如果提示python版本的问题:virtualenv -p python3 venv
  • linux激活虚拟环境source venv/bin/activate
  • windows激活虚拟环境venv/script/activate
  • 在项目虚拟环境中安装requirements
# requirements.txt

django
mysqlclient
django-redis
django-celery
celery-with-redis
alipay-sdk-python

(venv) wsm@ubuntu:~/python_project/taobao$ pip3 install -r requirements.txt
如果mysql_client报错:
1、将自己上传的虚拟环境中的 site-packages/ 下的 mysqlclient-1.4.4.dist-info 和 MySQLdb 两个目录删除
2、在linux上安装
sudo apt install libssl-dev
sudo apt install libmysqld-dev

启动uwsgi
  • 安装uwsgi
    sudo apt install uwsgi-core
    sudo apt install uwsgi-plugin-python3

  • 新建uwsgi.ini

[uwsgi]
chdir=/home/wsm/python_project/taobao
module=taobao.wsgi:application   
master=true
pidfile=taobao.pid
processes=1 # number of worker processes
max-requests=5000
daemonize=taobao.log
vacuum=true
env=DJANGO_SETTINGS_MODULE=taobao.settings 
venv = /home/wsm/python_project/taobao/venv
pythonpath=/home/wsm/python_project/taobao/venv/Lib/site-packages 
socket=ip地址:端口号
plugin=python3
  • 上传至linux
sftp> cd python_project/taobao/
sftp> put D:\stage3\taobao\uwsgi.ini
  • 启动uwsgi
    在项目根目录运行
    wsm@ubuntu:~/python_project/taobao$ uwsgi --ini uwsgi.ini
    [uWSGI] getting INI configuration from uwsgi.ini
    会新增两个文件:
    -rw-r----- 1 wsm wsm 1950 Dec 5 18:49 taobao.log
    -rw-rw-rw- 1 wsm wsm 6 Dec 5 18:49 taobao.pid
    cat taobao.log
    uwsgi --stop taobao.pid
    ps -ef|grep uwsgi
    查看日志
    wsm@ubuntu:~$ tail -f ~/python_project/taobao/taobao.log
    **uwsgi操作:
 uwsgi  --ini   uwsgi.ini    (启动)
 
 uwsgi --stop  taobao.pid (停止)
 
 uwsgi --reload taobao.pid (重启)
 
 ps  -aux | grep uwsgi    (查看进程)
启动nginx
  • 安装
    sudo apt install nginx
    检查nginx是否安装成功:
    浏览器输入http://ip地址(该IP为安装linux服务器对应的IP地址)
    在这里插入图片描述
  • 修改nginx.conf文件
    查看:
wsm@ubuntu:~/python_project/taobao$ cd /etc/nginx
wsm@ubuntu:/etc/nginx$ ll
total 80
drwxr-xr-x   8 root root  4096 Dec  5 19:12 ./
drwxr-xr-x 125 root root 12288 Dec  5 19:12 ../
drwxr-xr-x   2 root root  4096 Aug 20 05:46 conf.d/
-rw-r--r--   1 root root  1077 Apr  5  2018 fastcgi.conf
-rw-r--r--   1 root root  1007 Apr  5  2018 fastcgi_params
-rw-r--r--   1 root root  2837 Apr  5  2018 koi-utf
-rw-r--r--   1 root root  2223 Apr  5  2018 koi-win
-rw-r--r--   1 root root  3957 Apr  5  2018 mime.types
drwxr-xr-x   2 root root  4096 Aug 20 05:46 modules-available/
drwxr-xr-x   2 root root  4096 Dec  5 19:12 modules-enabled/
-rw-r--r--   1 root root  1482 Apr  5  2018 nginx.conf
-rw-r--r--   1 root root   180 Apr  5  2018 proxy_params
-rw-r--r--   1 root root   636 Apr  5  2018 scgi_params
drwxr-xr-x   2 root root  4096 Dec  5 19:12 sites-available/
drwxr-xr-x   2 root root  4096 Dec  5 19:12 sites-enabled/
drwxr-xr-x   2 root root  4096 Dec  5 19:12 snippets/
-rw-r--r--   1 root root   664 Apr  5  2018 uwsgi_params
-rw-r--r--   1 root root  3071 Apr  5  2018 win-utf

修改/etc/nginx下的nginx.conf为:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
	worker_connections 768;
	# multi_accept on;
}

http {
	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;
	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;
	gzip on;
	
	# config server
	server {

		server_name IP地址;
		listen 80;

		location / {
			uwsgi_pass IP地址:端口号;
			include uwsgi_params;
			}

		# 解决静态资源文件
		location /static/ {
				 # 设置静态资源的位置
				 alias /home/wsm/python_project/taobao/static/ ;
				}    
}
  • 检查配置文件是否正确:
    wsm@ubuntu:/etc/nginx$ sudo nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
  • 重启
    wsm@ubuntu:/etc/nginx$ sudo nginx -s reload
  • 查看日志
    wsm@ubuntu:~$ tail -f /var/log/nginx/access.log

**nginx操作

sudo  nginx   (启动) 
sudo  nginx  -t  (检查配置文件是否正确)
sudo nginx  -s  reload  (重启)
ps  -aux | grep nginx  (查看进程)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值