1. 建立CentOS 8 Container
  2. 安裝python 3
  3. 安裝mod_wsgi模組
  4. 安裝django
  5. 建立djnago APP
  6. 佈署至www

1. 建立CentOS 8並安裝apache

docker run -itd -p 9090:80 -p 8000:8000 --privileged --name myCentos centos /usr/sbin/init
docker exec -it myCentos /bin/bash

註:開放權限和映射-p 9090:80 -p 8000:8000供測試使用

安裝apache

dnf install httpd

2. 安裝python 3

進入shell中安裝並測試版本

dnf module -y install python36
python3 -V

用python取代python3 command,輸入以下命令並選擇第二選項

alternatives --config python

3.安裝mod_wsgi模組

於Shell中建立python3_wsgi.conf

dnf -y install python3-mod_wsgi

用vi建立檔案 -> vi內容 ->重啟apache

vi /etc/httpd/conf.d/python3_wsgi.conf
# create new
WSGIScriptAlias /test_wsgi /var/www/html/test_wsgi.py
systemctl restart httpd

完成測試

vi /var/www/html/test_wsgi.py
# create new
def application(environ, start_response):
    status = '200 OK'
    html = '<html>\n' \
           '<body>\n' \
           '<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">\n' \
           'WSGI Test Page\n' \
           '</div>\n' \
           '</body>\n' \
           '</html>\n'.encode("utf-8")
    response_header = [('Content-type','text/html')]
    start_response(status,response_header)
    return [html]

成功畫面

註:docker 網域是localhost, port是9090

4. 安裝Django

dnf -y install python3-virtualenv

[cent@dlp ~]$ virtualenv venv
[cent@dlp ~]$ cd ~/venv
[cent@dlp venv]$ source bin/activate
(venv) [cent@dlp venv]$ pip install django==3.0
.....
.....
Installing collected packages: pytz, sqlparse, asgiref, django
Successfully installed asgiref-3.2.3 django-3.0 pytz-2019.3 sqlparse-0.3.0

(venv) [cent@dlp venv]$ django-admin --version
3.0
# exit from virtualenv
(venv) [cent@dlp venv]$ deactivate
[cent@dlp venv]$
[cent@dlp ~]$ cd ~/venv
[cent@dlp venv]$ source bin/activate
# create testproject
(venv) [cent@dlp venv]$ django-admin startproject testproject
(venv) [cent@dlp venv]$ cd testproject
# configure database (default is SQLite)
(venv) [cent@dlp testproject]$ python manage.py migrate
# create admin user
(venv) [cent@dlp testproject]$ python manage.py createsuperuser
Username (leave blank to use 'cent'): cent
Email address: cent@dlp.srv.world
Password:
Password (again):
Superuser created successfully.
(venv) [cent@dlp testproject]$ vi testproject/settings.py
# line 28: set if you allow to access to Django from other Hosts
# specify Hosts with comma separated
# if allow all, specify like follows
ALLOWED_HOSTS = ['*']
# start server
(venv) [cent@dlp testproject]$ python manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
December 13, 2019 - 03:49:56
Django version 3.0, using settings 'testproject.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

5.建立Django App

[cent@dlp ~]$ cd ~/venv
[cent@dlp venv]$ source bin/activate
(venv)[cent@dlp venv]$ cd testproject
(venv)[cent@dlp testproject]$ python manage.py startapp test_app
(venv)[cent@dlp testproject]$ vi test_app/views.py# add to the end
<!-- wp:paragraph -->
<p>from django.http import HttpResponse def main(request):     html = '&lt;html>\n' \            '&lt;body>\n' \            '&lt;div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">\n' \            'Django Test Page\n' \            '&lt;/div>\n' \            '&lt;/body>\n' \            '&lt;/html>\n'     return HttpResponse(html) </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong></strong>(venv)[cent@dlp testproject]$&nbsp;<a href="https://www.server-world.info/en/command/html/vi.html">vi</a>&nbsp;testproject/urls.py# line 16: add like follows</p>
<!-- /wp:paragraph -->
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('test_app/', include('test_app.urls')),
]

(venv)[cent@dlp testproject]$ vi test_app/urls.py# create new
from django.urls import path
from .views import main

urlpatterns = [
    path('', main, name='home')
]

(venv)[cent@dlp testproject]$ vi testproject/settings.py# line 33: add test application in [INSTALLED_APPS] section
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'test_app',
)

(venv)[cent@dlp testproject]$ python manage.py runserver 0.0.0.0:8000

使用http://localhost:8000/test_app/進行測試

6. 佈署至www目錄

copy檔案至www/cent目錄下,並修改權限成755

cp -R /root /var/www/cent
cdmod 755 -R cen

註:需視實際安裝目錄進行調整

進行apache設定並重啟

vi /etc/httpd/conf.d/django.conf
WSGIDaemonProcess test_app python-path=/var/www/cent/venv/testproject:/var/www/cent/venv/lib/python3.6/site-packages
WSGIProcessGroup test_app
WSGIScriptAlias /django /var/www/cent/venv/testproject/testproject/wsgi.py

<Directory /var/www/cent/venv/testproject>
    Require all granted
</Directory>
systemctl restart httpd

測試結果畫面

使用centOS8Docker架設有利於移植於VPS主機和NAS主機上進行測試。

參考資料

  1. https://www.server-world.info/en/note?os=CentOS_8&p=httpd&f=10
  2. https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-centos-8
  3. https://www.cnblogs.com/infoo/p/11900607.html

Leave a Reply

Your email address will not be published. Required fields are marked *