将你的Python Web程序部署到Ubuntu服务器上
在本文记录了我在Ubuntu中部署Flask Web站点的过程, 其中包括用户创建、代码获取、Python3环境的安装、虚拟环境设置、uWSGI启动程序设置,并将Nginx作为前端反向代理。希望对各位有所帮助。
建立一个Python Web程序专用账户
1
2
3
|
adduser haseo
vim /etc/sudoers #将haseo用户加入导sudo用户清单中
sudo usermod -a -G www-data haseo
|
安装Python3并配置程序运行环境
1.更新Ubuntu的软件库
1
2
3
|
sudo apt-get update
sudo apt-get -y upgrade
sudo apt-get install build-essential libssl-dev libffi-dev python3-dev #安装一些必要的工具包
|
2.安装python包管理工具
python3 -V
sudo apt-get install -y python3-pip
pip3 install virtualenv
配置Python 程序
1.创建程序目录
mkdir -p /var/www/html/pricing-service
2.修改目录权限
sudo chown haseo:haseo /var/www/html/pricing-service
3.创建一个SSH Key使得用户可以同步GitHub的代码
ssh-keygen
cat ~/.ssh/id_rsa.pub # 复制公钥并增加到GitHub(https://github.com/settings/keys)
4.复制GitHub上的代码
git clone git@xxx .
5.创建log目录
mkdir log
6.创建虚拟目录
1
2
3
4
|
pip3 install virtualenv
python3 -m virtualenv venv # 在pricing-service目录下执行
./venv/bin/pip install -r requirements.txt
./venv/bin/pip install uwsgi
|
配置uwsgi
1.测试一下python直接运行程序是否可以访问
vim ~/myproject/wsgi.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "<h1 style='color:blue'>Hello There!</h1>"
if __name__ == "__main__":
app.run(host='0.0.0.0')
python wsgi.py
2.创建WSGI入口文件
vim ~/myproject/wsgi.py
from myproject import app
if __name__ == "__main__":
app.run()
3.测试uWSGI是否正常运行
uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi:app
4.创建uWSGI配置文件
前面测试没问题之后我们开始创建uWSGI配置文件
vim ~/myproject/wsgi.ini
[uwsgi]
module = wsgi:app
master = true
processes = 5
socket = socket.sock
chmod-socket = 660
vacuum = true
die-on-term = true
5.创建systemd文件
sudo vim /etc/systemd/system/price_service.service
[Unit]
Description=uWSGI instance to serve price_service
After=network.target
[Service]
User=haseo
Group=www-data
WorkingDirectory=/var/www/html/pricing-service
Environment="PATH=/var/www/html/pricing-service/venv/bin"
ExecStart=/var/www/html/pricing-service/venv/bin/uwsgi --ini wsgi.ini
[Install]
WantedBy=multi-user.target
6.启动并启用wsgi服务
sudo systemctl start price_service
sudo systemctl enable price_service
配置Nginx
1.安装nginx
apt-get install nginx
2.Nginx状态查看及进程管理
1
2
3
4
5
6
|
systemctl status nginx
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//' #获取服务器的IP地址
sudo systemctl start nginx
sudo systemctl reload nginx
sudo systemctl disable nginx # 精致nginx在系统启动的时候启动
sudo systemctl enable nginx
|
3.配置Nginx站点
vim /etc/nginx/sites-available/default
server {
listen 8080; #监听IP
real_ip_header X-Forwarded-For;
set_real_ip_from 127.0.0.1; # 告诉Python程序是谁发送的请求
server_name localhost;
location / { # 用户访问的根目录比如 http://www.bihell.com/
include uwsgi_params; # Flask程序需要uwsgi解析
uwsgi_pass unix:/var/www/html/pricing-service/socket.sock; #uwsgi通过这个文件传递信息
uwsgi_modifier1 30;
}
# 404错误页面配置,下同
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
4.软链接导nginxsites-enabled
目录
sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled
5.测试配置
sudo nginx -t
6.重新启动nginx大功告成
sudo systemctl restart nginx
将你的Python Web程序部署到Ubuntu服务器上的更多相关文章
- 如何将web项目部署到Ubuntu服务器上
情景回顾: 前几天在下本着人道主义原则帮我老师的一个朋友做了个小网页,(啥人道不人道的,主要是给钱了),做完之后本来是想偷懒直接把网页扔给他自己部署去吧,结果让我帮忙部署一下,得,偷懒也偷不成了,搞吧 ...
- Asp.net web api部署在某些服务器上老是404
asp.net web api部署在Windows服务器上后,按照WebAPI定义的路由访问,老是出现404,但定义一个静态文件从站点访问,却又OK. 这时,便可以确定是WebAPI路由出了问题,经调 ...
- 把 web 项目部署到 Linux 服务器上
1.打开 eclipse,在已经完成的 web 项目上面点击右键,选择 export,然后选择导出成 war 包. 以部署 SMBMS 项目为例 2.项目打包成 war ,选择项目导出到的位置. ...
- Linux系统(将web应用部署到tomcat服务器上)
一:tomcat服务开机自启动 将启动命令路径配置到/etc/profile文件中在/etc/profile 文件最后配置 /usr/tomcat/apache-tomcat-6.0.45/bin/s ...
- 在 eclipse 中将 web 项目部署到 tomcat 服务器上
1.在 eclipse 中,选择 Window--->Preferences--->Server--->Runtime Environments,选择 Add 按钮 2.在弹出的对话 ...
- 让你的SilverLight程序部署在任意服务器上
是的,即使是免费的只支持HTML的空间,同样可以部署SilverLight应用.众所周知,SilverLight的部署问题其实就是.xap文件名是否能被服务器支持的问题.解决的方法无非就是添加MIME ...
- 将eclipse上的web项目部署到Tomcat服务器上经验总结
1. 将Tomcat插件添加到eclipse上 Window --> Preferences --> Server --> Runtime Environment --> A ...
- Java Project部署到Tomcat服务器上
所有的JAVA程序员,在编写WEB程序时,一般都通过工具如 MyEclipse,编写一个WEB Project,通过工具让这个WEB程序和Tomcat关联.其实在我们可以通过JAVA程序部署到Tomc ...
- Python Web 程序使用 uWSGI 部署
Python Web 程序使用 uWSGI 部署 WSGI是什么? WSGI,全称 Web Server Gateway Interface,或者 Python Web Server Gateway ...
随机推荐
- ThinkPHP3.2基础知识(三)
1.如何开启调试模式,开启调试模式有什么用处? // 开启调试模式 建议开发阶段开启 部署阶段注释或者设为false define('APP_DEBUG',True); 开启调试模式的用处:方便及时发 ...
- 《You dont know JS》类型篇总结
类型 javaScript中的类型和熟知的一些强类型语言的有关类型的定义是不一样的.在js中,类型的含义是值的内部特征,它定义了值得行为,以使其区别于其他值.(a type is an intrins ...
- 一步一步从原理跟我学邮件收取及发送 5.C语言的socket示例
说到 C 语言版本的程序,首先要解决的问题就是兼容性. 作为 20 年开发有 10 多年是在服务端的程序员,我深刻地感受到服务端平台的两极分化之严重,linux 派对 windows 那是超级的不屑一 ...
- 动态查询:getBy字段名
http://www.php.cn/php/php-getBy.html 根据字段名动态查询:getBy字段名( ) 该方法很有意思,手册的说得很简略,我们根据源码来好好说道说道~~ 1. 功能:根据 ...
- 基于VUE选择上传图片并在页面显示(图片可删除)
demo例子: 依赖文件 : http://files.cnblogs.com/files/zhengweijie/jquery.form.rar HTML文本内容: <template> ...
- Tp-link路由器怎么设置端口映射 内网端口映射听语音
https://jingyan.baidu.com/article/ca00d56c710ef9e99eebcf85.html 只有一台能上网的电脑就可以自己免费搭建服务器,本经验简单介绍家用tp-l ...
- CUDA与OpenGL互操作
当处理较大数据量的时候,往往会用GPU进行运算,比如OpenGL或者CUDA.在实际的操作中,往往CUDA实现并行计算会比OpenGL更加方便,而OpenGL在进行后期渲染更具有优势.由于CUDA中的 ...
- 关于mybatis 注解sql sum(参数)传参写法
新手出道 验证了很久sum()里面带参数方式 #{参数}一直不行日志显示参数已经传进 但就是加不上去 返回的始终是0 后面换成$(参数)之后就行了 @Select("select sum($ ...
- subList ArrayList LinkedList
List<E> subList(int fromIndex,int toIndex) 该方法返回原有集合从fromIndex 到 toIndex之间一部分数据,组成一个新的集合,这两个集合 ...
- ELK入门级介绍--打造实时日志查询系统
这几天一直在研究ElasticSearch,在网上看到一篇好的文章和大家分享. ELK平台介绍 在搜索ELK资料的时候,发现这篇文章比较好,于是摘抄一小段: 以下内容来自:http://baidu.b ...