Python相关命令

Python部署最全命令

虚拟环境相关

1 升级python包管理工具pip

pip install --upgrade pip

备注:当你想升级一个包的时候 pip install --upgrade 包名

2 python虚拟环境安装

sudo apt-get install python-virtualenv

sudo easy_install virtualenvwrapper

上述工具装好后找不到mkvirtualenv命令,需要执行以下环境变量设置。

1.创建目录用来存放虚拟环境

mkdir $HOME/.virtualenvs

2.在~/.bashrc中添加行:

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

4.运行:

source ~/.bashrc

5.创建python虚拟环境

mkvirtualenv [虚拟环境名称]
workon [虚拟环境名称]

6.退出虚拟环境 离开

deactivate

7.删除(慎用)

rmvirtualenv [虚拟环境名称]

$ source venv/bin/activate 

$ .venv/bin/deactivate

pip freeze > requirements.txt

pip install -r requirements.txt

如果MySQL-python报错,执行 sudo apt-get install libmysqlclient-dev

scrapy 相关

创建项目 scrapy startproject mySpider

创建文件 scrapy genspider tencent "tencent.com"

执行 scrapy crawl tencent

通过下面的命令可以快速创建 CrawlSpider模板 的代码:

scrapy genspider -t crawl tencent tencent.com

django 相关

python manage.py makemigrations

python manage.py migrate

python manage.py createsuperuser

python manage.py runserver 127.0.0.1:8000

python manage.py collectstatic # 注意配置STATIC_ROOT

数据库相关

mysql -uroot -p

show databases;

use mm;

show tables;

select user();

sudo service mysql restart

执行sql文件

source /home/kangbing/mm_mmtype.sql

source /home/kangbing/mm_mmitem.sql

上传文件相关

从本地往服务器上传文件

scp mm_mmtype.sql kangbing@xxx:/home/kangbing/mm_mmtype.sql

scp mm_mmitem.sql kangbing@xxx:/home/kangbing/mm_mmitem.sql

文件夹加 -r 即 scp -r

下载到本地调换位置

scp -r kangbing@xxx:/var/www/mmsite/static/images/ /Users/rrkj/Desktop/images/

ssh 相关

  1. 登录1 cd ~/.ssh ssh -i cloudkey kangbing@xx.xx.x.xx

  2. 登录2 ssh kangbing@xx.xx.x.xx

uwsgi相关

  • 安装:pip install uwsgi

uwsgi.ini 配置文件

[uwsgi]
socket=:8000  # Nginx 
#http = :8000 # http
chdir = /home/kangbing/mmsite
wsgi-file = mmsite/wsgi.py
module = mmsite.wsgi
processes = 4
threads = 2
master = True
pidfile = uwsgi.pid
daemonize = uwsgi.log
  • 启动:uwsgi --ini uwsgi.ini
  • 停止:uwsgi --stop uwsgi.pid
  • 重启:uwsgi --reload uwsgi.pid
  • 查看PID: ps aux | grep uwsgi
  • 测试: uwsgi --http :8000 --module your_proj_name.wsgi

nginx相关

安装sudo apt-get install nginx

sudo service nginx start

sudo service nginx reload

sudo service nginx stop

sudo service nginx status

配置文件 路径 /etc/nginx/sites-enabled/default

# Default server configuration
server {
        listen 80 default_server;
        #listen 35.194.243.78:80 default_server;
        server_name www.meicai.tw meicai.tw;
        root /var/www/html;

        location / {
		# uwsgi
                include uwsgi_params;
                # uwsgi.ini 配置的socket
                uwsgi_pass xx.xx.xx.xx:8000;
        }

	# 静态资源文件目录
	# Django settings.py
	# STATIC_ROOT='/var/www/mmsite/static/'
        location /static {
                alias /var/www/mmsite/static/;
        }

}

# 重定向
server {
	listen 80;
	server_name www.jjlin.tw jjlin.tw;
	rewrite ^/ https://www.instagram.com/jjlin/;
}

# tornado 
upstream tornadoes {
    server 127.0.0.1:8020;
    server 127.0.0.1:8021;
    server 127.0.0.1:8022;
}

upstream websocket {
    server 127.0.0.1:8020;
}

server {
    listen 80;
    server_name www.korbin.tw korbin.tw;
    location /static/ {
        root /home/kangbing/mmsubmit/;
        if ($query_string) {
            expires max;
        }
    }

    location /chat {
        proxy_pass http://websocket/chat;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;  # 协议 http https
        proxy_pass http://tornadoes;
    }
}

SSL

安装Certbot

sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-nginx

在创建证书之前,需要确保 Web 服务器可以在根目录下的该路径下提供静态文件:/.well-know。由于 django 应用程序引入的所有路径都通过 wsgi 提供服务,因此我们需要修改 nginx 配置便于静态地提供这些资源:将以下内容添加到域名的 nginx 配置文件中:

location /.well-known/acme-challenge {
	# 项目根目录
	alias /home/kangbing/meicai/.well-known/acme-challenge;
}

生成证书

certbot certonly --webroot -w /home/kangbing/meicai -d meicai.tw -d www.meicai.tw

成功后会输出

	IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/meicai.tw/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/meicai.tw/privkey.pem
   Your cert will expire on 2018-12-13. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

最后修改 nginx 配置如下

server {
	listen 80 default_server;
	server_name www.youcai.tk youcai.tk www.meicai.tw meicai.tw;
	root /var/www/html;
	return 301 https://meicai.tw$request_uri;
}
	
server {
        listen 443 default ssl;
        server_name www.youcai.tk youcai.tk www.meicai.tw meicai.tw;
        root /var/www/html;

	# SSL
	ssl_certificate /etc/letsencrypt/live/meicai.tw/fullchain.pem;
	ssl_certificate_key /etc/letsencrypt/live/meicai.tw/privkey.pem;


	location / {
        # uwsgi
        include uwsgi_params;
        # uwsgi.ini 配置的socket
        uwsgi_pass 127.0.0.1:9000;
	}
	
	# 静态资源文件目录
	# Django settings.py
	# STATIC_ROOT='/var/www/mmsite/static/'
	location /static {
		alias /var/www/tiantian/static/;
	}
		
	location /.well-known/acme-challenge {
	        alias /home/kangbing/tiantian/.well-known/acme-challenge;
	}

}

静态文件相关

  • 新建文件夹

    cd /var/www

    sudo mkdir mmsite

  • 修改目录权限

    sudo chmod 777 /var/www/mmsite

  • 创建static目录

    mkdir static

  • 修改settings.py文件

    STATIC_ROOT='/var/www/mmsite/static/'

  • 收集所有静态文件到static_root指定目录

    python manage.py collectstatic

  • 重启nginx、uwsgi

supervisor

为了统一管理Tornado的多个进程,安装supervisor工具。

sudo pip install supervisor

运行echo_supervisord_conf命令输出默认的配置项,可以如下操作将默认配置保存到文件中

echo_supervisord_conf > supervisord.conf

vim 打开编辑supervisord.conf文件,修改文件包含路径方便自己配置

[include]
files = relative/directory/*.ini

修改为:

[include]
files = /etc/supervisor/*.conf

将编辑后的supervisord.conf文件复制到/etc/目录

然后在/etc目录下新建子目录supervisor(刚配置文件里include的选项相同),并在/etc/supervisor/中新建tornado管理的配置文件tornado.conf。

配置tornado.conf

[group:tornados]
# 声明三个tornado的进程配置
programs=tornado-0,tornado-1,tornado-2

# 分别定义
[program:tornado-0]
# 执行命令
command=python /home/kangbing/mmsubmit/server.py --port=8020
directory=/home/kangbing/mmsubmit/
user=kangbing
# 自动重启
autorestart=true
redirect_stderr=true
# 日志路径
stdout_logfile=/home/kangbing/log/tornado0.log
loglevel=info

[program:tornado-1]
command=python /home/kangbing/mmsubmit/server.py --port=8021
directory=/home/kangbing/mmsubmit/
user=kangbing
autorestart=true
redirect_stderr=true
stdout_logfile=/home/kangbing/log/tornado1.log
loglevel=info

[program:tornado-2]
command=python /home/kangbing/mmsubmit/server.py --port=8022
directory=/home/kangbing/mmsubmit/
user=kangbing
autorestart=true
redirect_stderr=true
stdout_logfile=/home/kangbing/log/tornado2.log
loglevel=info

启动supervisor

supervisord -c /etc/supervisord.conf

查看是否已运行

ps aux | grep supervisord

停止运行

kill pid

supervisorctl

利用supervisorctl来管理supervisor。

supervisorctl

查看状态

status | stop | start | restart | update

修改nginx配置文件,详见上面nginx相关

压缩

tar -zcvf a.tar.gz b.txt 

tar -zxvf a.tar.gz

# save
:w !sudo tee %