用apache2部署Django應用

2021-08-05 18:36:20 字數 3576 閱讀 9849

環境要求:ubuntu14.04及以上,python3.4及以上

檢視系統版本:cat /etc/issue

python2可能導致importerror: no module named django錯誤

檢視python版本:python --version

一般ubuntu系統自帶python,在/usr/bin可檢視已安裝的版本

若為python2,在系統級別更改版本:

羅列出所有可用的 python 替代版本資訊:

update-alternatives --list python

更新替代列表:

update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1

update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2

--install選項使用了多個引數用於建立符號鏈結。最後乙個引數指定了此選項的優先順序,如果我們沒有手動來設定替代選項,那麼具有最高優先順序的選項就會被選中。

此時檢視所有的替代版本:

切換python版本:

sudo update-alternatives --config python

會列出可切換的列表,填入數字:

移除替代版本示例:update-alternatives --remove python /usr/bin/python2.7

安裝pip:sudo apt-get install python3-pip

安裝mysqlclient:sudo pip install mysqlclient

安裝django:sudo pip install django

如果出現mysql_config not found錯誤,安裝libmysqlclient-dev:sudo apt-get install libmysqlclient-dev

如果出現python.h:no such file or directory,安裝python-dev:sudo apt-get install python-dev

安裝mysql

方法一:

docker pull mysql

sudo docker run --name mysql -p 3306:3306 -e mysql \_root\_password=123456 -d mysql        執行時須指定root密碼

執行時可能出現mysql.socket不存在的問題

方法二:

sudo apt-get install mysql-server       安裝時會要求輸入root密碼

注意檢查mysql是否有時區表,沒有的話可能出現utc日誌轉換convert_tz出錯的情況

執行:select * from mysql.time_zone;

select * from mysql.time_zone_name;

若為空,執行

mysql_tzinfo_to_sql /usr/share/zoneinfo

注:mysql命令列登入

mysql -u root -p mysql

輸入root密碼

sudo apt-get install apache2

sudo apt-get install libapache2-mod-wsgi-py3

檢視apache版本:apachectl -v

我們希望在瀏覽器通過***.***.***訪問**

servername ***.***.***

documentroot /var/www/a/b 這裡是wsgi.py所在目錄

order allow,deny

allow from all

require all granted

alias /static /var/www/a/static

wsgiscriptalias / /var/www/a/b/wsgi.py

errorlog $/error_8800.log

customlog $/access_8800.log combined

/etc/apache2/ports.conf下也將80埠改為8800

這裡使用的埠是8800,因為我們這裡80埠被nginx占用

在nginx的配置檔案中修改

location /
重啟nginx:

sudo nginx -s reload

一般將根目錄及以下目錄許可權設定為755,檔案許可權設定為644

sudo chmod -r 644 a

sudo find a -type d | xargs chmod 755

如果有上傳需求,將上傳資料夾設定寫許可權

啟用**:sudo a2ensite sitename 或 sudo a2ensite sitename.conf

更改**程式需要重啟apache2,service apache2 restart若無效

ps -aux | grep apache2 將三個程序全部殺死

kill -s 9

再啟動service apache2 start

除錯

django的日誌在/your_project_name/settings.py檔案中配置。具體配置如下:

logging = ,

'detail'

: , },

'handlers'

: , 'file'

: ,: ,

: , },

'loggers'

: , # 自定義模組日誌

'users'

: , 'common'

: ,: ,

: , 'pushdata'

: , },

}

此配置分成三個部分:

注意:日誌的輸出級別是由loggers中的每個模組中level選項定義。如果沒有配置,那麼預設為warning級別。

然後在每個模組的views.py中,通過下面**使用:

import

logging

logger = logging.getlogger(__name__)

具體的輸出部分**為:

logger.debug("hello, world")

logger.info("hello, world")

logger.error("hello, world")

日誌在/var/log/apache2/

如果採用print來列印除錯資訊

python manage.py runserver --noreload
這樣print出來的資訊就可以在終端看到了,但是這樣作,每次python**有修改,就要重啟伺服器。

apache2部署多個django專案

可以通過監聽埠的不同來區分兩個專案。下面的這個配置會導致由於程序衝突而造成404錯誤 正確的方法在下面!wsgipythonpath wsgipythonpath path to dininghouse path to inte ce for club clubserverinte ce djang...

nuxt專案用pm2部署

1.專案根目錄下 新建ecosystem.config.js 2.配置 module.exports env production deploy ref origin master repo 專案倉庫 path 專案在伺服器上的路徑 post deploy yarn yarn run build p...

Fabio技術手冊 2 部署

fabio的主要用例是將來自internet的http s 和tcp請求分發給前端 fe 服務,後者可以處理這些請求。在這個場景中,前端提供服務然後使用consul中的服務發現特性來查詢它們需要的後端 be 服務,以便為請求提供服務。這意味著fabio目前沒有用作fe be或be be路由器來在服務...