1. 程式人生 > >Ubuntu配置Django+ Apache2+ mysql

Ubuntu配置Django+ Apache2+ mysql

amp date database use api www. cati python3.5 lib

# 我的Ubuntu上自帶的python3.5,所以安裝一下 python3.6
sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt-get update
sudo apt-get install python3.6
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2

裝完可以檢查一下版本

>> python3 -V

# install pip3

sudo apt install python3-pip

# install Django
sudo pip3 install Django

裝完可以檢查一下django版本

>> python3

>> import django

>> django.VERSION

# framework 一般要用到Restful API,需要安裝
sudo -H pip3 install djangorestframework
sudo -H pip3 install markdown
sudo -H pip3 install django-filter

# 下面裝mysql
sudo apt-get -y install python3-mysqldb
sudo apt-get -y install mysql-server mysql-client
sudo apt-get -y install libmysqlclient-dev
sudo -H pip3 install mysqlclient
sudo -H apt-get -y install python3-pymysql
sudo pip3 install requests

# 配置數據庫mysql command
connect: mysql -u root -p
create database: create database 數據庫名 default charset=utf8;
create user: create user 你的用戶名 identified by ‘你的密碼‘;
grant: grant all privileges on 數據庫名.* to 你的用戶名@‘%‘ identified by ‘你的密碼‘;
show grants for ‘數據庫名‘;

以下配置apache

# Install Apache & WSGI, disable unneeded VirtualHosts
sudo apt-get update
sudo apt-get -y install apache2 libapache2-mod-wsgi-py3
sudo a2enmod wsgi
sudo service apache2 restart
sudo a2dissite 000-default
sudo service apache2 restart

# Add Project User and Add Yourself to His Group
sudo adduser lmy
sudo usermod --lock lmy
sudo mkdir /home/lmy/grouped
sudo chmod u=rwx,g=srwx,o=x /home/lmy/grouped
sudo chown -R lmy.lmy /home/lmy/

sudo find /home/lmy/grouped/ -type f -exec chmod -v ug=rw {} \;
sudo find /home/lmy/grouped/ -type d -exec chmod -v u=rwx,g=srwx {} \;

sudo adduser $(whoami) lmy
newgrp lmy

# Create a New VirtualHost for WSGI Django
sudoedit /etc/apache2/sites-available/項目名稱.conf

<VirtualHost *:80>
ServerName www.example.com

WSGIDaemonProcess 用戶名 user=用戶名 group=用戶名 threads=5 python-path="/home/用戶名/grouped/項目名/"
WSGIScriptAlias / /home/用戶名/grouped/項目名/項目名/wsgi.py
<Directory /home/用戶名/grouped/項目名/>
WSGIProcessGroup 用戶名
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
Require all granted
</Directory>
</VirtualHost>

sudo a2ensite 項目名
sudo service apache2 restart

以上,就已經配置完了

如果服務器遇到500錯誤

cat /var/log/apache2/error.log

或 tail -f /var/log/apache2/error.log

# 初始化數據庫
sudo -H python3 manage.py makemigrations
sudo -H python3 manage.py migrate

ref:

http://terokarvinen.com/2017/django-on-apache-with-python-3-on-ubuntu-16-04

https://zxtcode.com/main/37/

http://www.django-rest-framework.org/

https://docs.djangoproject.com/en/1.11/howto/static-files/

http://www.cnblogs.com/gide/p/6179975.html

http://www.runoob.com/django/django-first-app.html

Ubuntu配置Django+ Apache2+ mysql