python+nginx+uwsgi部署雲主機遇到的問題

問題後期會有次序的整理,目前整理一小部分html

1.部署以後出現403問題。

403權限問題的修改:python

(1)打開nginx.conf文件mysql

vim /etc/nginx/nginx.conf

(2)按鍵盤「i」進入編輯模式,修改第一行爲nginx

user root;

(3)按鍵盤「esc」退出鍵,輸入:wq,保存並推出。git

(4)查看80端口占用pid值web

lsof -i tcp:80

(5)殺死pid=14012的進程sql

kill 14012

(6)啓動nginxdjango

sudo /usr/sbin/nginx

查看頁面是否顯示正常vim

2.uwsgi的理解及簡單使用?

uwsgi中文文檔:http://uwsgi-docs-cn.readthedocs.io/zh_CN/latest/WSGIquickstart.htmlcentos

(1)簡單運用-利用文件啓動

新建uwsgitest.py的文件(python3+寫法)

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]

放置在服務器某個目錄下

在該目錄運行

uwsgi --http :9090 --wsgi-file uwsgitest.py

能夠看到打出的文字,uwsgi起做用,可是關閉終端後,沒法顯示。

須要新建uswgi.ini文件,並設定開機啓動

uwsgi.ini

[uwsgi]
socket = 127.0.0.1:9090
chdir = /root/projects/
wsgi-file = py_rest/uwsgitest.py
processes = 4
threads = 2
stats = 127.0.0.1:9191

更新ini文件 

uwsgi -i /root/projects/py_rest/uwsgi.ini &

 

(2)django的uwsgi設置

其中,conf目錄下2文件和uwsgi_params爲必須文件(py_rest/uwsgi.ini和py_uwsgitest.py爲上文必須文件)

conf目錄下放置本項目nginx和uwsgi配置文件

uc_nginx.conf

upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:80; # for a web port socket (we'll use this first)
}

server {
listen      80; #   監聽端口
server_name xx.xxx.xx.xx; #    填寫你的服務器
charset     utf-8;
autoindex on;

#   資源限制
client_max_body_size 75M;   #   adjust to taste
client_header_buffer_size 16k;

# Django media資源
location /media  {
    alias  /root/projects/py_rest/media; # 指向django的media目錄
}

# Django static資源
location /static {
    alias /root/projects/py_rest/static; # 指向django的static目錄
}

# 日誌
access_log /root/projects/py_rest/access_log;
error_log  /root/projects/py_rest/error_log;

location / {
# A.靜態頁面
#    root   /root/projects/py_rest/templates;
#    index  index index.html urls.py;

# B.django項目
    uwsgi_pass 127.0.0.1:8000; #這個和uwsgi中的socket要一致
    include uwsgi_params;

# C.其餘設置
#    autoindex on;
#    autoindex_exact_size on;
    autoindex_localtime on;
}
}

uwsgi.ini

# py_rest.ini file
[uwsgi]
#   Django的項目全路徑
chdir           = /root/projects/py_rest
#   Django's wsgi文件
#module          = py_rest.wsgi
wsgi-file            = py_rest/wsgi.py

master          = true
processes       = 10
#   socket鏈接
socket          = 127.0.0.1:8000 #  這個和uc_nginx.conf中的uwsgi_pass要一致
vacuum          = true  #   退出uwsgi是否清理中間文件,包含pid、sock和status文件
procname-prefix-spaced= py_rest  #   uwsgi的進程名稱前綴
py-autoreload=1 #   py文件修改,自動加載

#   虛擬環境路徑
home = /root/.virtualenvs/testvir3
virtualenv = /root/.virtualenvs/testvir3
#   日誌
logto = /tmp/mylog.log

文件上傳到服務器上

uswgi_prama(重要,ini文件設置location / {include uwsgi_params;}時須要該文件的路徑,如本例所示就要在chdir設置的目錄下,也就是   /root/projects/py_rest目錄下。如過放到別的路徑下,include後面須要給路徑

uwsgi_param QUERY_STRING        $query_string;
uwsgi_param REQUEST_METHOD      $request_method;
uwsgi_param CONTENT_TYPE        $content_type;
uwsgi_param CONTENT_LENGTH      $content_length;

uwsgi_param REQUEST_URI     $request_uri;
uwsgi_param PATH_INFO       $document_uri;
uwsgi_param DOCUMENT_ROOT       $document_root;
uwsgi_param SERVER_PROTOCOL     $server_protocol;
uwsgi_param UWSGI_SCHEME        $scheme;

uwsgi_param REMOTE_ADDR     $remote_addr;
uwsgi_param REMOTE_PORT     $remote_port;
uwsgi_param SERVER_PORT     $server_port;
uwsgi_param SERVER_NAME     $server_name;

1) conf更新

cd /etc/nginx/conf.d/

rm uc_nginx.conf

sudo ln -s /root/projects/py_rest/conf/nginx/uc_nginx.conf /etc/nginx/conf.d/

lsof -i tcp:80

kill 12344

sudo /usr/sbin/nginx

2) ini更新

uwsgi -i /root/projects/py_rest/conf/uwsgi.ini &

查看頁面已經能夠刷出

 

因爲設置的是80端口,在瀏覽器裏輸入主機ip就能夠看到主頁內容

不過對靜態資源的加載出現問題,須要設置。

3.ImportError: No module named 'MySQLdb'

 

解決1:

路徑:/root/.virtualenvs/env_rest/lib/python3.5/site-packages/django/db/backends/mysql/base.py

vim /usr/lib/python3.5/site-packages/django/db/backends/mysql/base.py

在from django.utils.safestring import SafeBytest,SafeTest下添加下面兩條

import pymysql
pymysql.install_as_MySQLdb()

結果:運行會報錯

 

解決2:

wget http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz
tar zxf MySQL-python-1.2.3.tar.gz && cd MySQL-python-1.2.3
python2 setup.py build
python2 setup.py install

centos提示ImportError: No module named MySQLdb解決辦法:https://lvtao.net/server/645.html

結果

 

 

參考文檔

1.nginx出現403 Forbidden解決方法:https://www.cnblogs.com/Alex-as/p/8963505.html

2.nginx 403 forbidden 二種緣由:https://blog.csdn.net/u011650048/article/details/54092881

3.項目部署(nginx + uwsgi + django) 和 開機自啓動django項目:http://www.javashuo.com/article/p-odmkibop-mn.html

4.uwsgi使用介紹:https://www.jianshu.com/p/c3b13b5ad3d7

5.python3.*報「ImportError: No module named ‘MySQLdb'」:https://www.cnblogs.com/TaleG/p/6735099.html

相關文章
相關標籤/搜索