Mezzanine/Django-如何部署至DigitalOcean (Python 3.8+Ubuntu+Apache)

Posted by: bart30508 | in Mezzanine | 11 months ago |

Mezzanine/Django-如何部署至DigitalOcean (Python 3.8+Ubuntu+Apache)

前言: 最一開始我是架設在Heroku上(坑也不少), 但是他有關機機制, 加上Domain不是自己的, 因此打算移置VPS商, DigitalOcean.

原本我是打算在DigitalOcean開啟一台CentOS 7裝, 但坑實在太多, 最後我決定裝Ubuntu 20.04.

看了一篇文章, 如果要上cloud, 就裝Ubuntu吧, 順手, 坑比較少, os的效能差距基本上輪不到我這小小部落格去考慮.

這麼一測試, 還真的是呢^^

CentOS 7 Deploy一半的過程與坑會放在最下面.

多半的坑都與以下有關 1. Linux預設是Python2,而我的專案是使用Python3 2. mod-wsgi版本問題 3. psycopg2 版本問題 4. pip install -r requirements.txt 安裝時產生的問題 (如找不到某package)


DigitalOcean提供60天100美元額度, 使用以下連結註冊DigitalOcean, 可多獲得25元美金的額度. https://m.do.co/c/63ee88a49eec

註冊細節的話, 照著介面走應該不成問題, 大概提示注意以下幾點: 1. 一開始可以選擇最低月付方案即可, 5美元 2. 機房目前離台灣最近的是新加玻

如果你想要使用ssh, 可以參考我的另一篇文章"如何設定SSH以及使用FTP", 有說明如何在DigitalOcean設定ssh以及使用FTP.

強烈建議先看, 對我來說使用Windows編寫文件還是比vi要來的順手.


開啟Console登入DigitalOcean, 我們可以準備開始架設基礎環境:

1.安裝Apache網頁伺服器

```
apt-get update 
apt-get install apache2
```

成功後, 可確認嘗試連結你的ip是否(IP可在DigitalOcean控制台找到), 如果出現以下畫面則代表安裝成功

2.讓Apache可以執行Python

python 3 應該使用libapache2-mod-wsgi-py3 : sudo apt-get install libapache2-mod-wsgi-py3

如果你是Python2請使用 : apt-get install libapache2-mod-wsgi

你可以查看Apache的log, 如果用到python 2, 上面會有寫, 並且可能之後會報erro: ImportError: cannot import name signals 3.安裝git

```
apt-get install git 
git config --global user.name "xxx8@gmail.com" 
git config --global user.email "xxx@gmail.com"
```

4.安裝pip以及virtualenv

```
apt-get install python3-pip 
apt-get install virtualenv
```

5.進入Apache (通則)存放Web路徑, 建立venv, 並clone我們的專案

```
cd /var/www
virtualenv VENV
source VENV/bin/activate

git clone https://github.com/xxx/xxx.git
```

6.安裝網頁所需套件

```
cd InvestBlog

pip3 install -r requirements.txt
```

install發現錯誤psycopg2, 此為python3版本問題, 使用以下解:

```
-- psycopg2 python3版本問題解決
去除requirement.txt的psycopg2
apt-get install libpq-dev
pip3 install psycopg2
```

7.架設資料庫 由於我是使用Postgresql, 如何在Ubuntu架設請看我另一篇

8.測試網頁是否成功 python3 manage.py runserver yourdropletip:8000

註:此處如果沒有反應, 可以試著檢查以下幾點: 1. 檢查Appche的log 2. 是否Setting.py有錯誤 3. 是否Port/防火牆沒開 4. python manage.py check 檢查Djagno Project是否有問題 iptables -I INPUT -p tcp --dport 8000 -j ACCEPT #Postgresql iptables -I INPUT -p tcp --dport 5432 -j ACCEPT #查看port是否開放 iptables -L -n

補充:Linux執行網頁瀏覽器 補充:Linux執行網頁瀏覽器 (不過由於digitalocean只能使用一個console所以無法在linux測試, 不過就在自己的本機測試更直覺) apt-get install w3m w3m-img w3m 128.199.251.210:8000

9.修改000-defualt.conf設定 路徑: /etc/apache2/site-enabled/000-default.conf

```
<VirtualHost *:80> 
WSGIDaemonProcess InvestBlog python-path=/var/www/InvestBlog:/var/www/VENV/lib/python3.8/site-packages
WSGIProcessGroup InvestBlog 
WSGIScriptAlias / /var/www/InvestBlog/investblog/wsgi.py 
</VirtualHost>
```

註: 1. InestBlog是github上我們clone下來的名稱 2. investblog是有存放wsgi.py的那個路徑 3. WSGIDaemonProcess 是告訴apache, 我們要使用的python virtual env在哪 4. WSGIScriptAlias 是告訴apache, 當有請求從瀏覽器進來時, 我們要轉發到哪

  1. 重啟Apache

    service apache2 restart

  2. 輸入drolet ip 瀏覽網頁

到目前為止, 你應該可以不加port, 也不用再drolet啟動django專案, 就可以成功連結網頁了.

  1. 啟動多站點 篇幅關係, 寫在部落格另一篇文章喔
  2. 加入Domain 篇幅關係, 寫在部落格另一篇文章喔

在Centos上的步驟

註:只完成一半, 當作參考用, 了解後再使用, 不要照抄.

或許各位可以先參考這篇 Django - CentOS 7 + Apache

```
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel wget

wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz

tar Jxvf Python-3.7.1.tar.xz

cd Python-3.7.1

./configure prefix=/usr/local/python3

make && make install

vi .bashrc

alias python='/usr/local/python3/bin/python3'
alias pip='/usr/local/python3/bin/pip3'

source .bashrc

python -V

yum install python-pip

pip3 install virtualenv

yum install python37-mod-wsgi

git config --global user.name "xxx"
git config --global user.email "xxx@gmail.com"

#建立virtualenv
virtualenv VENV
#啟動virtualenv
source VENV/bin/activate

git clone

pip3 install -r requriement.txt

python3 manage.py runserver 178.128.57.226:80 --settings=investblog.settings_local

python manage.py runserver 127.0.0.1:8000 --settings=investblog.settings_local

python3 -m pip install --upgrade pip --target /usr/lib64/az/lib/python3.7/site-packages/

python manage.py runserver --settings=investblog.settings_local


<VirtualHost *:80> 
WSGIDaemonProcess InvestBlog python-path=/var/www/InvestBlog:/var/www/VENV/lib/python3.7/ 
site-packages 
WSGIProcessGroup InvestBlog 
WSGIScriptAlias / /var/www/InvestBlog/investblog/wsgi.py 
</VirtualHost>
systemctl restart httpd
```

參考來源: https://andy6804tw.github.io/2019/09/08/ubuntu-accept-port/ https://blog.csdn.net/weixin_42134789/article/details/80753001 https://stackoverflow.com/questions/37629929/apache2-using-python2-7-i-want-to-use-python3-4 https://stackoverflow.com/questions/58159194/an-issue-configuring-wsgipythonhome-in-a-django-mod-wsgi-virtual-environment https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/modwsgi/ https://stackoverflow.com/questions/5983100/importerror-cannot-import-name-signals https://codertw.com/%E4%BC%BA%E6%9C%8D%E5%99%A8/381229/ https://chenuin.blogspot.com/2019/01/django-ubuntuapache2modwsgi-django.html https://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html http://sakananote2.blogspot.com/2018/04/digital-ocean.html https://askubuntu.com/questions/700389/how-to-configure-django-with-apache2 https://ubuntuqa.com/zh-tw/article/10544.html

CentOS部署Django相關參考:

https://richarlin.tw/blog/centos-apache-serve/ https://serverfault.com/questions/710076/centos-7-firewall-cmd-not-found https://stackoverflow.com/questions/42004986/how-to-install-mod-wgsi-for-apache-2-4-with-python3-5-on-centos-7 https://kirin.idv.tw/python-install-python3-in-centos7/ https://blog.jiebu-lang.com/centos-7-install-python-3-7/ https://devops.ionos.com/tutorials/how-to-install-django-with-apache-on-centos-7/

Venv https://www.liquidweb.com/kb/how-to-setup-a-python-virtual-environment-on-centos/

apache https://serverfault.com/questions/1036214/how-to-enable-load-mod-wsgi-in-apache-server-in-centos-8-vps

postgre https://devops.ionos.com/tutorials/install-postgresql-on-centos-7/ https://blog.csdn.net/CGD_ProgramLife/article/details/80475920

python http://blog.e-happy.com.tw/python-%E5%A6%82%E4%BD%95%E6%9B%B4%E6%96%B0-pip-%E5%88%B0%E6%9C%80%E6%96%B0%E7%89%88%E6%9C%AC%EF%BC%9F/ https://pip.pypa.io/en/stable/installing/#upgrading-pip

error: pip install cryptography no module named setuptools_rust> https://stackoverflow.com/questions/19128540/django-improperlyconfigured-the-secret-key-setting-must-not-be-empty LoadModule wsgi_module modules/mod_wsgi.so> Issues installing mod_wsgi - cannot find Makefile.in> https://stackoverflow.com/questions/40946211/django-apache-wsgi-changes-python-version

tags: DigitalOcean Django
Currently unrated
 or 

Subscribe

* indicates required

Recent Posts

Archive

2022
2021

Categories

Apache 1

Data Science 2

Dbfit 1

Design Pattern 1

Devops 3

DigitalOcean 1

Django 1

English 3

Excel 5

Flask 3

Git 1

HackMD 1

Heroku 1

Html/Css 1

Linux 4

Machine Learning 2

Manufacture 1

Mezzanine 18

Oracle 1

Postgresql 7

PowerBI 4

Powershell 4

Python 21

SEO 2

SQL Server 51

SQLite 1

Windows 1

database 8

work-experience 1

其他 1

自我成長 1

資料工程 1

Tags

SEO(1) Github(2) Title Tag(2) ML(1) 李宏毅(1) SQL Server(18) Tempdb(1) SSMS(1) Windows(1) 自我成長(2) Excel(1) python Flask(1) python(5) Flask(2)

Authors

bart30508 (146)

Feeds

RSS / Atom

Mezzanine/Django-如何部署至DigitalOcean (Python 3.8+Ubuntu+Apache)

© COPYRIGHT 2011-2022. Max的文藝復興. ALL RIGHT RESERVED.