国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python Web程序部署到Ubuntu服務(wù)器上的方法

2020-01-04 15:50:04
字體:
供稿:網(wǎng)友

在本文記錄了我在Ubuntu中部署Flask Web站點(diǎn)的過程, 其中包括用戶創(chuàng)建、代碼獲取、Python3環(huán)境的安裝、虛擬環(huán)境設(shè)置、uWSGI啟動(dòng)程序設(shè)置,并將Nginx作為前端反向代理。希望對(duì)各位有所幫助。

python,Web程序,部署,服務(wù)器

建立一個(gè)Python Web程序?qū)S觅~戶

adduser haseovim /etc/sudoers #將haseo用戶加入導(dǎo)sudo用戶清單中sudo usermod -a -G www-data haseo

安裝Python3并配置程序運(yùn)行環(huán)境

1.更新Ubuntu的軟件庫

sudo apt-get updatesudo apt-get -y upgradesudo apt-get install build-essential libssl-dev libffi-dev python3-dev #安裝一些必要的工具包

2.安裝python包管理工具

python3 -Vsudo apt-get install -y python3-pippip3 install virtualenv

配置Python 程序

1.創(chuàng)建程序目錄

mkdir -p /var/www/html/pricing-service

2.修改目錄權(quán)限

sudo chown haseo:haseo /var/www/html/pricing-service

3.創(chuàng)建一個(gè)SSH Key使得用戶可以同步GitHub的代碼

ssh-keygencat ~/.ssh/id_rsa.pub # 復(fù)制公鑰并增加到GitHub(https://github.com/settings/keys)

4.復(fù)制GitHub上的代碼

git clone git@xxx .

5.創(chuàng)建log目錄

mkdir log

6.創(chuàng)建虛擬目錄

pip3 install virtualenvpython3 -m virtualenv venv # 在pricing-service目錄下執(zhí)行./venv/bin/pip install -r requirements.txt./venv/bin/pip install uwsgi

配置uwsgi

1.測(cè)試一下python直接運(yùn)行程序是否可以訪問

vim ~/myproject/wsgi.pyfrom flask import Flaskapp = 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.創(chuàng)建WSGI入口文件

vim ~/myproject/wsgi.pyfrom myproject import appif __name__ == "__main__": app.run()

3.測(cè)試uWSGI是否正常運(yùn)行

uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi:app

4.創(chuàng)建uWSGI配置文件

前面測(cè)試沒問題之后我們開始創(chuàng)建uWSGI配置文件

vim ~/myproject/wsgi.ini[uwsgi]module = wsgi:appmaster = trueprocesses = 5socket = socket.sockchmod-socket = 660vacuum = truedie-on-term = true

5.創(chuàng)建systemd文件

sudo vim /etc/systemd/system/price_service.service[Unit]Description=uWSGI instance to serve price_serviceAfter=network.target[Service]User=haseoGroup=www-dataWorkingDirectory=/var/www/html/pricing-serviceEnvironment="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.啟動(dòng)并啟用wsgi服務(wù)

sudo systemctl start price_servicesudo systemctl enable price_service

配置Nginx

1.安裝nginx

apt-get install nginx

2.Nginx狀態(tài)查看及進(jìn)程管理

systemctl status nginxip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's///.*$//' #獲取服務(wù)器的IP地址sudo systemctl start nginxsudo systemctl reload nginxsudo systemctl disable nginx # 精致nginx在系統(tǒng)啟動(dòng)的時(shí)候啟動(dòng)sudo systemctl enable nginx

3.配置Nginx站點(diǎn)

vim /etc/nginx/sites-available/defaultserver {  listen 8080; #監(jiān)聽IP  real_ip_header X-Forwarded-For;  set_real_ip_from 127.0.0.1; # 告訴Python程序是誰發(fā)送的請(qǐng)求  server_name localhost;  location / { # 用戶訪問的根目錄比如 http://www.bihell.com/    include uwsgi_params; # Flask程序需要uwsgi解析    uwsgi_pass unix:/var/www/html/pricing-service/socket.sock; #uwsgi通過這個(gè)文件傳遞信息    uwsgi_modifier1 30;  }  # 404錯(cuò)誤頁面配置,下同  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.軟鏈接導(dǎo)nginx sites-enabled 目錄

sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled

5.測(cè)試配置

sudo nginx -t

6.重新啟動(dòng)nginx大功告成

sudo systemctl restart nginx

總結(jié)

以上所述是小編給大家介紹的Python Web程序部署到Ubuntu服務(wù)器上的方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VEVB武林網(wǎng)網(wǎng)站的支持!


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到python教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 温宿县| 赤水市| 齐河县| 蒙阴县| 岑巩县| 瓮安县| 大姚县| 广德县| 荔波县| 柳林县| 若尔盖县| 横峰县| 辽中县| 大厂| 江城| 曲周县| 门源| 讷河市| 康保县| 雷波县| 兴国县| 额敏县| 永丰县| 龙州县| 汤原县| 叶城县| 高州市| 富裕县| 鹿邑县| 弥勒县| 黔南| 武邑县| 宁德市| 梅州市| 宜州市| 老河口市| 福安市| 靖边县| 正安县| 礼泉县| 广平县|