Install and Config Django with Apache

这篇文章要介绍在Ubuntu上面如何从零开始配置Django和Apache. 要说明的一点是我这里使用的是python3. 下一篇文章将介绍如何连接Django和MySQL.

Install pip

1
sudo apt-get install python3-pip.

Install virtualenv

1
2
3
pip3 install virtualenv
virtualenv ./.virtualenv/mysite -p python3
source .virtualenv/mysite/bin/activate

To exit the virtual env:

1
deactivate

Install Apache

1
sudo apt-get install apache2

Some useful comments to restart apache service:

1
2
3
apachectl stop
apachectl start
apachectl restart

Install mod_wsgi

1
sudo apt-get install libapache2-mod-wsgi-py3

Install Django

Under the virtual env:

1
2
pip install Django
python -m django --version

Create your first Django project and config it

1
django-admin startproject mysite

Go to mysite/settings.py. Add your host ip to ALLOWED_HOSTS and add STATIC_ROOT:

1
2
3
4
5
6
7
8
9
ALLOWED_HOSTS = [
    '35.232.xxx.xxx'
]

# ...
# some other configs
# ...

STATIC_ROOT = os.path.join(BASE_DIR, "static")

Apache Config

If you want to config the mod_wsgi as embedded mode

Go to Apache config. For Ubuntu the file path is /etc/apache2/apache2.conf. For other systems, please check Apache website. Add following config into the file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
WSGIScriptAlias / /home/yourusername/mysite/mysite/wsgi.py
WSGIPythonHome /home/yourusername/.virtualenv/mysite
WSGIPythonPath /home/yourusername/mysite

Alias /robots.txt /home/fhuanming/mysite/static/robots.txt
Alias /favicon.ico /home/fhuanming/mysite/static/favicon.ico

Alias /media/ /home/fhuanming/mysite/media/
Alias /static/ /home/fhuanming/mysite/static/

<Directory /home/fhuanming/mysite/static/>
  Require all granted
</Directory>

<Directory /home/fhuanming/mysite/media>
  Require all granted
</Directory>

<Directory /home/yourusername/mysite/mysite>
  <Files wsgi.py>
    Require all granted
  </Files>
</Directory>

If you want to config the mod_wsgi as deamon mode

Add a new Apache VirtualHost config file (you can also modify the exsiting default file). For Ubuntu the file path is under /etc/apache2/sites-available. For other systems, please check Apache website:

1
sudo vim /etc/apache2/sites-available/mysite.conf

Then you can add following config into the file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<VirtualHost *:80>
  WSGIDaemonProcess mysite processes=2 threads=15 display-name=%{GROUP} python-home=/home/fhuanming/.virtualenv/mysite python-path=/home/fhuanming/mysite

  Alias /robots.txt /home/fhuanming/mysite/static/robots.txt
  Alias /favicon.ico /home/fhuanming/mysite/static/favicon.ico

  Alias /media/ /home/fhuanming/mysite/media/
  Alias /static/ /home/fhuanming/mysite/static/

  <Directory /home/fhuanming/mysite/static/>
    Require all granted
  </Directory>

  <Directory /home/fhuanming/mysite/media>
    Require all granted
  </Directory>

  WSGIProcessGroup mysite
  WSGIApplicationGroup %{GLOBAL}
  
  WSGIScriptAlias / /home/fhuanming/mysite/mysite/wsgi.py

  <Directory /home/fhuanming/mysite/mysite>
    <Files wsgi.py>
      Require all granted
    </Files>
  </Directory>
</VirtualHost>

Then disable the default VirtualHost config and enable your new added.

1
2
3
4
sudo a2dissite 000-default.conf
sudo systemctl reload apache2
sudo a2ensite mysite
sudo systemctl reload apache2

Check your website

Then go to your browser to visit http://35.232.xxx.xxx:80. You will find your config works. :D

Check you admin page

If you went you http://35.232.xxx.xxx:80/admin and find out that your admin page does not serve css file. Please run cmd below. This cmd will copy all your admin static files to the STATIC_ROOT directory which you configured above.

1
python manage.py collectstatic