ubuntu环境部署项目
安装python3.6
第一步:sudo add-apt-repository ppa:jonathonf/python-3.6
如果报错为:sudo: add-apt-repository: command not found
则执行:sudo apt-get install software-properties-common python-software-properties 之后,再次执行上一步
第二步:apt-get update
第三步:apt-get install python3.6
python3.6版本的pip安装
https://blog.csdn.net/xiaoming0018/article/details/83146191
先安装 apt-get中 需要安装的包,然后再安装 pip中的包
apt-get中需要安装的包:
sudo apt-get install python3.6-dev
sudo apt-get install mysql-server
service mysql start
sudo apt-get install mysql-client
sudo apt-get install redis-server
安装 nginx: https://www.cnblogs.com/EasonJim/p/7806879.html
部署虚拟环境
现在非虚拟环境下安装好python3.6
sudo pip install virtualenv #安装虚拟环境需要的包
virtualenv -p /usr/bin/python3.6 venv #创建带有python3.6的虚拟环境(自动包含pip对应版本,一定要指定python运行版本)
virtualenv -p /usr/bin/python2.7 venv #创建带有python2.7的虚拟环境(自动包含pip对应版本,安装supervisor时需要,后续对supervisor的操作,也要在此环境中)
cd venv #进入到虚拟环境的目录
source bin/activate #进入虚拟环境 成功后,命令行开头 有 (venv)
安装pip的包
pip install -r file.txt #安装pip相关包(linux系统中去掉pywin32的包安装命令,并且出现 successfully字样 才是安装成功),先进入虚拟环境
在安装 pip install mysqlclient 报错如下:
/bin/sh: 1: mysql_config: not found
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-install-sr_0u6nl/mysqlclient/setup.py", line 16, in <module>
metadata, options = get_config() File "/
先安装 apt-get install libmysqlclient-dev
安装supervisor
如果 调换了python版本的优先级,把python3.6设置为最高优先级,在使用pip2.7命令时,如果报错如下:
root@zhimeng:/usr/bin# pip2 Traceback (most recent call last): File "/usr/bin/pip2", line 9, in <module> load_entry_point('pip==8.1.1', 'console_scripts', 'pip2')() File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 542, in load_entry_point return get_distribution(dist).load_entry_point(group, name) File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2568, in load_entry_point raise ImportError("Entry point %r not found" % ((group, name),)) ImportError: Entry point ('console_scripts', 'pip2') not found
则 打开错误提示中的“/usr/bin/pip2”文件,把文件中第一行的“#!/usr/bin/python”改为“#!/usr/bin/python2” 即可
原文网址如下:https://blog.csdn.net/llf_cloud/article/details/80160494
./bin/pip2.7 install supervisor #通过pip2.7安装 superviosr(因为supervisor不支持python3)
echo_supervisord_conf > supervisord.conf #生成superviosr配置文件 https://blog.csdn.net/wr166/article/details/79051725 在python2的环境下用supervisor来运行python3的web项目
vi supervisor.conf #打开后,输入相关配置
supervisord -c supervisord.conf #通过配置文件启动supervisor服务
deactivate #退出虚拟环境 # https://blog.csdn.net/charlie_heng/article/details/60573688 supervisor 在python3下的简易解决方案
防火墙/端口 开启和关闭
开启防火墙: ufw enable
关闭防火墙:ufw disable
开启防火墙的端口 :ufw allow 端口号; 如:ufw allow 5000
关闭防火墙的端口:ufw deny 端口号;
重启防火墙:ufw reload
查看防火墙端口的状态: ufw status
测试远程主机的端口是否开启: telnet 192.168.1.103 80
启动服务并外网可以访问:
supervisor 中的配置文件(修改后,一定要 supervisorctl reload 命令 进行 重载配置文件):
[program:fws] #项目名
directory= /home/ubuntu/fws #项目位置
environment = PATH='home/ubuntu/venv/bin' #环境
command= /home/ubuntu/venv/bin/python3.6 run.py #运行命令
autostart = true #自动启动
startsecs = 1
autorestart = true #自动重启
stopasgroup = true
killasgroup = true
user = ubuntu
stdout_logfile = /home/ubuntu/fws/logs/supevisor.log #输出日志
stderr_logfile = /home/ubuntu/fws/logs/supevisor_err.log #错误日志
run.py文件内容:
from fws import app
if __name__ == '__main__':
app.run(host='10.104.136.123',port=5000) #host为内网地址,端口号要检查外网是否可以访问,并且 阿里云 安全组要有此端口
nginx配置内容:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main escape=json '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$request_body"';
access_log /home/ubuntu/fws/logs/nginx.log main;
error_log /home/ubuntu/fws/logs/nginx_err.log;
sendfile on;
keepalive_timeout 65;
server {
listen 8080;
location / {
proxy_pass http://10.104.136.123:5000;
proxy_set_header X-Real-Ip $remote_addr;
}
location ^~ /index.html {
alias /home/web/fws/fws/static/dist/;
}
location /static {
alias /home/web/fws/fws/static/;
}
}
}
然后启动 supervisor,和nginx即可
gunicorn在 supervisor中启动的方式():
[program:oly] directory = /data/wwwroot/project
# 如果指定gunicorn为虚拟环境,则gunicorn路径为虚拟环境路径
# command =/data/wwwroot/olympus/venv/bin/gunicorn -w 4 -b 0.0.0.0:5000 -t 160 manage:app
user = root startsecs = 3 autorestart =true redirect_stderr =true stdout_logfile_maxbytes = 50MB stdout_logfile_backups = 10 stdout_logfile=/data/wwwlogs/olympus/olympus_supervisor.log loglevel=info
linux系统,mysql开启远程连接(先保证安全组和防火墙对应的3306端口开启) :https://www.cnblogs.com/chenjw-note/p/5887908.html
netstat -tunlp |grep 端口号,用于查看指定的端口号的进程情况,如查看8000端口的情况,netstat -tunlp |grep 8000
其他相关命令记录
pip list --format freeze # pip 列表
pip install -r file.txt #pip批量安装
pip -V #查看对应的python编译版本
whereis python3.6 #查看安装路径
https://www.cnblogs.com/yjlch1016/p/8641910.html Ubuntu怎样安装Python3.6,pip
https://blog.csdn.net/San_South/article/details/80715682 Ubuntu16.04上pip报错ModuleNotFoundError: No module named 'pip._internal'
https://blog.csdn.net/wangtaoking1/article/details/51554959 安装Python mysqlclient出现“OSError: mysql_config not found”错误
https://blog.csdn.net/meteor_s/article/details/79115360 Error记录--ImportError: No module named apt_pkg
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1 需要根据python版本安装对应的python-dev包,如apt-get install pyton3.6-dev