--- title: 'Ubuntu Django + apache2 +mod_wsgi' tags: Django disqus: hackmd --- # Ubuntu Django + apache2 +mod_wsgi sudo mysql_secure_installatio 系統環境: * Ubuntu 16.04 * Python 3.5.2 * Django 2.1.5 安裝apache2 ``` sudo apt update sudo apt upgrade sudo apt install apache2 vim ``` 安裝mysql ``` sudo apt install mysql-server -y ``` 安裝python3 以及 Django ``` sudo apt install python3-pip sudo apt install Django ``` 創立專案 ``` django-admin.py startproject project cd project mkdir static mkdir templates ``` 安裝apache2 wsgi套件(使用python3) ``` sudo apt install libapache2-mod-wsgi-py3 ``` 修改Django設定檔 路徑:(/home/an/project/project/settings.py) ``` ALLOWED_HOSTS = ['<your_ip>'] ... DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'OPTIONS': { # 這邊註解掉(原因:apache2無法正確吃到.cnf所引到的mysql資料庫) #'read_default_file': './mysite/my.cnf', # 添加以下,直接告訴Django資料庫使用者以及資料庫 'database': 'blood_sugar', 'user': 'root', 'password': 'root', }, } } ``` 修改apache2監聽port 路徑:(/etc/apache2/ports.conf) ``` Listen 80 Listen 8000 #聆聽port號 ``` 修改apache設定 路徑:(/etc/apache2/sites-availabel/000-default.conf) ``` <VirtualHost *:8000> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined # 通過wsgi.py讓Apache識別這是一個Django工程 WSGIScriptAlias / /home/<your_Name>/<your_project>/<your_project>/wsgi.py WSGIDaemonProcess project python-path=/home/<your_Name>/<your_project> WSGIProcessGroup project # 靜態文件(js/css/images)的存放位置 Alias /static/ /home/<your_Name>/<your_project>/static/ Alias /templates/ /home/<your_Name>/<your_project>/templates/ <Directory /home/<your_Name>/<your_project>> Options FollowSymLinks Order deny,allow Require all granted </Directory> <Directory /home/<your_Name>/<your_project>/<your_project>> <Files wsgi.py> Order deny,allow Require all granted #賦予它所有權限 Require ip <your_ip> </Files> </Directory> </VirtualHost> ``` 啟用你的配置文件 ``` sudo a2ensite /etc/apache2/sites-available/000-default.conf ``` 重啟apache2 ``` sudo service apache2 restart ``` 查看錯誤資訊 ``` cat /var/log/apache2/error.log ``` 最後成果: 
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up