Last active
May 10, 2019 09:47
-
-
Save lize240810/2febd1d4cd1d45a348987533d3a287ab to your computer and use it in GitHub Desktop.
python的web项目 flask,django都部署
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1.先把项目传到服务器上 | |
| 文件上传 | |
| 1、把本机的文件传给目的服务器 | |
| ``` | |
| scp 文件 ze@192.168.1.147:/目录 | |
| ``` | |
| 2、在本机上执行scp,把远端的服务器文件拷贝到本机上: | |
| ``` | |
| scp root@192.168.1.147:目录 / | |
| ``` | |
| 3、拷贝目录下的所有文件: | |
| ``` | |
| scp -r /super/ root@192.168.1.145:/ | |
| ``` | |
| 2.创建配置文件 | |
| > sudo vim /home/ze/runinenv.sh | |
| > 不用修改直接使用 | |
| ``` | |
| #!/bin/bash | |
| export SOME_ENV=test-message | |
| VENV=$1 | |
| if [ -z $VENV ]; then | |
| echo "usage:runinenv [virtualenv_path] CMDS" | |
| exit 1 | |
| fi | |
| source ${VENV}/bin/activate | |
| shift 1 | |
| echo "Executing $@ in ${VENV}" | |
| exec "$@" | |
| deactivate | |
| ``` | |
| 3.安装必须的软件 | |
| ``` | |
| sudo apt-get install python3-pip virtualenv -y | |
| sudo apt-get install vim -y | |
| sudo apt-get install nginx supervisor -y | |
| ``` | |
| 创建虚拟环境、安装包、测试程序 | |
| ``` | |
| cd ~ | |
| virtualenv v3web --python=python3 | |
| # 激活环境 | |
| source /home/ze/v3web/bin/activate | |
| pip install Flask flask_sqlalchemy | |
| # 进入项目运行目录 | |
| cd /home/zuxia/HouseSystem-v3 | |
| python index.py | |
| # 释放虚拟环境 | |
| deactivate | |
| ``` | |
| 4.使用supervisor管理进程(后台运行) | |
| > cd /etc/supervisor/conf.d/ | |
| > vim demo.conf | |
| ``` | |
| [program:demo] | |
| user=ze # 运行用户 | |
| directory=/home/ze/HouseSystem-v3/ # 根目录 | |
| command=/bin/bash /home/ze/runinenv.sh /home/ze/v3web python /home/ze/HouseSystem-v3/index.py # 可以直接去除进入测试 | |
| autostart=true | |
| autorestart=true | |
| startsecs=5 | |
| stopsignal=HUP | |
| stopasgroup=true | |
| stopwaitsecs=5 | |
| stdout_logfile_maxbytes=20MB | |
| stdout_logfile=/var/log/supervisor/%(program_name)s-out.log # 运行文件 | |
| stderr_logfile_maxbytes=20MB | |
| stderr_logfile=/var/log/supervisor/%(program_name)s-err.log # 错误文件 | |
| ``` | |
| 配置要点: | |
| [program:<名称>] | |
| user=<运行用户> | |
| directory=<启动目录> | |
| command=<运行的命令> | |
| environment=<环境变量> 参考: Supervisor and Environment Variables | |
| 4.supervisor管理命令 | |
| ```shell | |
| # 重新加载配置 | |
| sudo supervisorctl reload | |
| # 查看进程状态 | |
| sudo supervisorctl status | |
| # 停止/启动/重启某个进程(此处为demo) | |
| sudo supervisorctl stop/start/restart demo | |
| ``` | |
| 5.Linux基础命令 | |
| ```shell | |
| # 查看和python相关的tcp连接 | |
| netstat -antp | grep python | |
| # 查看和python相关的进程 | |
| ps uax | grep python | |
| # 杀死指定名称的进程(此处为python) | |
| sudo pkill python | |
| ``` | |
| 6.配置nginx | |
| ```shell | |
| cd /etc/nginx/conf.d/ | |
| sudo vim demo.conf | |
| ``` | |
| `/etc/nginx/conf.d/demo.conf`内容如下: | |
| > 使用时需要删除# 注释 | |
| ``` | |
| server { | |
| listen 80; | |
| listen [::]:80; | |
| server_name localhost; # 域名 | |
| location / { | |
| proxy_set_header X-Real-IP $remote_addr; | |
| proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
| proxy_set_header Host $http_host; | |
| proxy_set_header X-NginX-Proxy true; | |
| proxy_pass http://127.0.0.1:5000/; # 运行地址 | |
| # proxy_pass http://10.0.0.10:8999/; | |
| proxy_redirect off; | |
| } | |
| # 静态文件 | |
| location /static/ { | |
| alias /home/zuxia/HouseSystem-v3/static/; | |
| } | |
| } | |
| ``` | |
| 7.nginx管理命令 | |
| ```shell | |
| # 重启nginx服务(会重新加载配置文件) | |
| sudo service nginx restart | |
| # 启动、停止服务器 | |
| sudo service nginx start/stop | |
| ``` | |
| ---- | |
| ### 注解 | |
| line 3 | |
| directory 指定了项目跟目录 | |
| line 4, | |
| 完整的command,可以直接在bash里面运行,如果没有错误,则是ok的 | |
| 1. 先手动source虚拟环境,运行,确保ok | |
| 2. 退出虚拟env | |
| 3. run full command, ensure it is ok | |
| 4. supervisorctl update (supervisor配置更新后需要执行update,restart没有,restart只针对有新增或删除的情况) | |
| /bin/bash /home/ze/runinenv.sh /home/ze/DjangoProject python ./manage.py runserver 0.0.0.0:8888 | |
| visit in browser | |
| /bin/bash /home/xyz/runinenv.sh /home/xyz/v3web | |
| source ${VENV}/bin/activate | |
| ENV: /home/xyz/v3web | |
| 调用runinenv.sh,激活环境 | |
| # command exec in virtual env : | |
| linux shell 中: | |
| shift 1 表示删除第几个参数 | |
| $@ 表示剩余的参数 | |
| exec 表示执行 | |
| $@: | |
| python /home/xyz/myblog/index.py | |
| tail -f <file-path here> | |
| 监控文件变化, -f == file | |
| all ok? | |
| ok | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment