pip3安裝uwsgi|python3安裝
環境/軟件版本:centos 7.3,yum安裝nginx,yum安裝mysql,yum安裝python3.4
安裝pip3
yum install python34-pip
pip3 安裝依賴
pip3 install -r requirements
安裝python34devel
yum install python34-devel
安裝uwsgi
pip3 install uwsgi
編寫test.py測試uwsgi,注意python3和python2的測試方法是不一樣的。
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] # python3 #return ["Hello World"] # python2
啟動uwsgi 並訪問
測試uwsgi啟動項目,項目啟動run.py文件代碼,
#!usr/bin/python # -*- coding: utf-8 -*- from app import app if __name__ == "__main__": app.run()
測試http啟動命令,訪問端口5000,能夠正常訪問網站,到此,python3,pip3,flask項目代碼,uwsgi,全部測試成功
uwsgi --socket 0.0.0.0:5000 --protocol=http -w run:app
編寫uwsgi.ini,以wsgi方式啟動uwsgi,此時無法通過web訪問的方式測試是否啟動,
查看控制台提示即可
uwsgi.ini-uwsgi啟動的配置文件
#uwsgi配置文件 [uwsgi] socket = 127.0.0.1:5000 #socket = /tmp/flask.sock chmod-socket = 660 vacuum = true chdir = /home/dc module = run:app wsgi-file = run.py callable = app
uwsgi啟動的linux shell命令,項目在/home/dc下
uwsgi --ini /home/dc/uwsgi.ini
如控制台出現以下提示,八成是成功了
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x12b9fa0 pid: 2402 (default app) mountpoint already configured. skip. uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** *** uWSGI is running in multiple interpreter mode *** spawned uWSGI worker 1 (and the only) (pid: 2402, cores: 1)
接下來將uwsgi啟動shell命令寫成系統服務,以便開機啟動
在 /usr/lib/systemd/system/ 下新建uwsgi.service
我的內容如下,自測成功。
注意,使用絕對路徑調用uwsgi
[Unit] Description=uwsgi project [Service] Type=simple PIDFile=/home/uwsgi.pid ExecStart=/usr/bin/uwsgi --ini /home/dc/uwsgi.ini ExecReload=/usr/bin/uwsgi --ini /home/dc/uwsgi.ini ExecStop= PrivateTmp=true [Install] WantedBy=multi-user.target
啟動uwsgi服務,設置開機啟動
systemctl start uwsgi
systemctl enable uwsgi
此時,除nginx以外的所有配置全部完成
nginx的配置
在/etc/nginx/conf.d下新建一個uwsgi.conf
# mysite_nginx.conf # configuration of the server server { # the port your site will be served on listen 80; # the domain name it will serve for server_name dc.blfly.com; # substitute your machine's IP address or FQDN charset utf-8; # max upload size client_max_body_size 75M; # adjust to taste # Django media # location /media { # alias /path/to/your/mysite/media; # end as required #} location /static { alias /home/dc/app/static; # your Django project's static files - amend as required } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass localhost:5000;#此處與uwsgi.ini的socket是對應的 #include /path/to/your/mysite/uwsgi_params; # the ued include uwsgi_params; #uwsgi_param UWSGI_CHDIR /home/dc; #//項目根目錄 #uwsgi_param UWSGI_SCRIPT run:app; } }
重啟nginx,訪問80端口,測試網站成功訪問
有什么問題,可以在下面留言問我