If you want to run Python on a web server such as Apache or Nginx, you need to put a mechanism called WSGI in between. The mechanism of WSGI seems to be simple, but I haven't investigated it in detail yet. It's a bit more annoying than PHP because it has WSGI in between. But once you remember it, there is almost no difference in preparation man-hours. In this post, I'll try running a web application on Django and Apache on Vagrant's Ubuntu 16.04. This time, Pyenv is not included, and it is set using Python 3.5 that is already installed.
Use the Ubuntu 16.04 box (ubuntu / xenial64).
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.network "forwarded_port", guest: 80, host: 9090, host_ip: "127.0.0.1"
end
$ vagrant up
$ vagrant ssh
$ sudo apt-get update
$ sudo apt-get install -y vim apache2 apache2-dev
$ sudo apt-get install -y libapache2-mod-wsgi-py3
$ sudo apt-get install -y python3-pip
$ sudo pip3 install --upgrade pip
$ sudo pip3 install django
$ mkdir /vagrant/django
$ cd /vagrant/django
$ django-admin startproject mysite
Create a new /etc/apache2/sites-available/django.conf
.
/etc/apache2/sites-available/django.conf
<VirtualHost *:80>
WSGIDaemonProcess mysite python-home=/usr python-path=/vagrant/django/mysite
WSGIScriptAlias / /vagrant/django/mysite/mysite/wsgi.py process-group=mysite
<Directory /vagrant/django/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
Each parameter of WSGI looks like the following. There is a detailed manual here [https://modwsgi.readthedocs.io/en/develop/user-guides.html), so if you have any questions or errors, you should read it.
--The pyhton-home of WSGIDaemonProcess can be found below.
$ python3
>>> import sysconfig
>>> sysconfig.get_config_var('prefix')
'/usr'
--python-path is the path to the root folder of your Django project. --WSGIScriptAlias, the first / is the URL path on which the application is based. In the case of /, it becomes WEB ROOT. The second part is the location of the WSGI file.
Disable the default settings and enable django.conf
.
$ sudo a2dissite 000-default
$ sudo a2ensite django
Restart apache2.
$ sudo systemctl restart apache2
Recommended Posts