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