Ubuntu配置Django+ Apache2+ mysql

來源:互聯網
上載者:User

標籤:ges   database   def   restful   apache2   edit   work   pos   ssi   

# 我的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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.