Nginx+uWSGI部署flask項目


uwsgi配置

uwsgi安裝

安裝uwsgi

pip install uwsgi

啟動uwsgi

uwsgin --ini uwsgi.ini

# 后台啟動
nohup uwsgi --ini uwsgi.ini &

uwsgi配置

[uwsgi]
# 是否作為主進程
master = true
# 啟動uwsgi的端口號,非項目端口,
http=:8080    # 坑:這里用http可以直接用外網訪問,如果要用nginx代理則需要改成socket
# 項目目錄
chdir = /home/paul/tb_commodity/
# 啟動文件
wsgi-file=/home/paul/tb_commodity/manage.py
# 實例Flask對象的app名如果是(all_app)則寫callable=all_app
callable=app
# 進程數
processes=4
# 線程數
threads=2
buffer-size = 65536
vacuum=true
# pid存儲的路徑
pidfile =./uwsgi.pid
# python環境
home=/home/paul/virtual_env/tb_commodity_env/
# 啟動文件名,如果是app則寫app
module=manage

純凈版uwsgi配置

[uwsgi]
master = true
http=:8080   
chdir = /home/paul/tb_commodity/
wsgi-file=/home/paul/tb_commodity/manage.py
callable=app
processes=4
threads=2
buffer-size = 65536
vacuum=true
pidfile =./uwsgi.pid
home=/home/paul/virtual_env/tb_commodity_env/
module=manage

# 其他可以根據需要配置

nginx安裝部署

nginx安裝

配置EPEL源

sudo yum install -y epel-release

sudo yum -y update

安裝Nginx

yum install -y nginx

啟動Nginx

systemctl start nginx

停止nginx

systemctl stop nginx

重啟nginx

systemctl restart nginx

查看nginx狀態

systemctl status nginx

禁止開機啟動nginx

systemctl disable nginx

nginx配置

# 完整的nginx.conf
user nginx
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid
include /usr/share/nginx/modules/*.conf

events{
    
}
http{
    server {
    	# 監聽你外網訪問的端口號
        listen 80;
    	# 配置域名或者ip
        server_name  121.199.68.77;
    location /{
      include uwsgi_params;
      # 轉發到那個地址,轉發到uwgi的地址,在通過uwsgi來啟動我們的項目
      uwsgi_pass 0.0.0.0:8999;
      uwsgi_connect_timeout 60;
    }
}   
}

需要代理一個服務,則在http中添加一個server

server {
    	# 監聽你外網訪問的端口號
    listen 80;
    	# 配置域名或者ip
    server_name  121.199.68.77;
    location /{
      include uwsgi_params;
      # 轉發到那個地址,轉發到uwgi的地址,在通過uwsgi來啟動我們的項目
      uwsgi_pass 0.0.0.0:8999;
      uwsgi_connect_timeout 60;
    }
}  

# 根據自己的情況,更改就好

uwsgi配置中的坑

1.如果要看uwsgi啟動沒有,可以用用命令netstat -anp|grep 端口號來查看,這里的端口號,不是你項目中的端口號,而是你在uwsgi中配置的端口號

2.uwsgi中,如果你用的是http=:8080則可以通過外網直接來訪問,但是用nginx代理會報錯,我報的錯誤是504502,而如果要用nginx來反向代理的話需要改成socket=:8080,但是改成socket后,直接訪問uwsgi訪問不了。(個人碰到比較坑的地方)

3.如果是雲服務器,一定不要忘記安全組要放行端口,剛開始就是直接啟動服務,怎么都訪問不了。能ping通ip的話,端口訪問不了,就從服務器控制台-安全組,防火牆,SElinux來排查。

4.最近遇到一個坑:nginx.service start-post operation timed out. Stopping.

大概就是在啟動nginx時,systemctl start nginx會夯住幾分鍾,這幾分鍾ngxin是可以訪問的,但是后面就會報錯,如下:

[root@iZwz9fnlmotggn5gy1wsffZ conf]# systemctl start nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

根據提示查看報錯:systemctl status nginx.service

systemctl status nginx.service
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Fri 2019-11-15 11:43:10 CST; 15s ago
     Docs: http://nginx.org/en/docs/
  Process: 4363 ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=1/FAILURE)
  Process: 4257 ExecStartPost=/bin/sleep 0.1 (code=exited, status=0/SUCCESS)
  Process: 4253 ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
  Process: 4251 ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 3183 (code=exited, status=0/SUCCESS)

Nov 15 11:43:10 iZwz9fnlmotggn5gy1wsffZ kill[4363]: -p, --pid              print pids without signaling them
Nov 15 11:43:10 iZwz9fnlmotggn5gy1wsffZ kill[4363]: -l, --list [=<signal>] list signal names, or convert one to a name
Nov 15 11:43:10 iZwz9fnlmotggn5gy1wsffZ kill[4363]: -L, --table            list signal names and numbers
Nov 15 11:43:10 iZwz9fnlmotggn5gy1wsffZ kill[4363]: -h, --help     display this help and exit
Nov 15 11:43:10 iZwz9fnlmotggn5gy1wsffZ kill[4363]: -V, --version  output version information and exit
Nov 15 11:43:10 iZwz9fnlmotggn5gy1wsffZ kill[4363]: For more details see kill(1).
Nov 15 11:43:10 iZwz9fnlmotggn5gy1wsffZ systemd[1]: nginx.service: control process exited, code=exited status=1
Nov 15 11:43:10 iZwz9fnlmotggn5gy1wsffZ systemd[1]: Failed to start nginx - high performance web server.
Nov 15 11:43:10 iZwz9fnlmotggn5gy1wsffZ systemd[1]: Unit nginx.service entered failed state.
Nov 15 11:43:10 iZwz9fnlmotggn5gy1wsffZ systemd[1]: nginx.service failed.

根據提示查看報錯:journalctl -xe

[root@iZwz9fnlmotggn5gy1wsffZ conf]# journalctl -xe
Nov 15 11:40:59 iZwz9fnlmotggn5gy1wsffZ polkitd[2513]: Unregistered Authentication Agent for unix-process:4105:344564595 (system bus 
Nov 15 11:40:59 iZwz9fnlmotggn5gy1wsffZ root[4149]: [euid=root]:root pts/2 2019-11-15 09:10 (59.33.51.12):[/usr/local/nginx/conf]2019
Nov 15 11:40:59 iZwz9fnlmotggn5gy1wsffZ root[4158]: [euid=root]:root pts/2 2019-11-15 09:10 (59.33.51.12):[/usr/local/nginx/conf]2019
Nov 15 11:41:09 iZwz9fnlmotggn5gy1wsffZ root[4176]: [euid=root]:root pts/2 2019-11-15 09:10 (59.33.51.12):[/usr/local/nginx/conf]2019
Nov 15 11:41:10 iZwz9fnlmotggn5gy1wsffZ root[4186]: [euid=root]:root pts/2 2019-11-15 09:10 (59.33.51.12):[/usr/local/nginx/conf]2019
Nov 15 11:41:13 iZwz9fnlmotggn5gy1wsffZ root[4197]: [euid=root]:root pts/2 2019-11-15 09:10 (59.33.51.12):[/bin]2019-11-15 11:41:13 r
Nov 15 11:41:14 iZwz9fnlmotggn5gy1wsffZ root[4209]: [euid=root]:root pts/2 2019-11-15 09:10 (59.33.51.12):[/bin]2019-11-15 11:41:14 r
Nov 15 11:41:40 iZwz9fnlmotggn5gy1wsffZ polkitd[2513]: Registered Authentication Agent for unix-process:4234:344570141 (system bus na
Nov 15 11:41:40 iZwz9fnlmotggn5gy1wsffZ systemd[1]: Stopped nginx - high performance web server.
-- Subject: Unit nginx.service has finished shutting down
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit nginx.service has finished shutting down.
Nov 15 11:41:40 iZwz9fnlmotggn5gy1wsffZ systemd[1]: Starting nginx - high performance web server...
-- Subject: Unit nginx.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit nginx.service has begun starting up.
Nov 15 11:41:40 iZwz9fnlmotggn5gy1wsffZ nginx[4251]: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
Nov 15 11:41:40 iZwz9fnlmotggn5gy1wsffZ nginx[4251]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Nov 15 11:41:40 iZwz9fnlmotggn5gy1wsffZ systemd[1]: PID file /var/run/nginx.pid not readable (yet?) after start-post.
Nov 15 11:43:10 iZwz9fnlmotggn5gy1wsffZ systemd[1]: nginx.service start-post operation timed out. Stopping.

實際上是nginx服務起來后,又自動停止了。

可以看到:

nginx.service start-post operation timed out. Stopping.

PID file /var/run/nginx.pid not readable (yet?) after start-post.

解決:

1.檢查nginx.conf文件,發現是nginx.conf中沒有指定pid的路徑

2.nginx.conf中pid需要和nginx.service配置的pid路徑一致

3.找到nginx.service文件:find / -name 'nginx.service'

4.最后配置nginx.conf中的配置

5.systemctl start nginx 

6.正常啟動

技術支持

centos7安裝python3.6.5技術支持:https://www.cnblogs.com/oden/p/11765251.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM