[PYTHON] Create an environment for Django x Apache x mod_wsgi with Vagrant (Ubuntu 16.04)

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.

environment

Vagrantfile settings

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

Launch Vagrant and enter

$ vagrant up
$ vagrant ssh

Install apache2

$ sudo apt-get update
$ sudo apt-get install -y vim apache2 apache2-dev

Install mod_wsgi

$ sudo apt-get install -y libapache2-mod-wsgi-py3

Install Django and create an app

$ 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

apache2 settings

Create django.conf

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.

Enable django.conf

Disable the default settings and enable django.conf.

$ sudo a2dissite 000-default
$ sudo a2ensite django

Restart apache2.

$ sudo systemctl restart apache2

Recommended Posts

Create an environment for Django x Apache x mod_wsgi with Vagrant (Ubuntu 16.04)
Various commands for building an environment with Apache
Create an environment with virtualenv
Create an API with Django
[Linux] WSL2 Build an environment for laravel7 with Ubuntu 20.04
Create an environment for test automation with AirtestIDE (Tips)
Create an environment for "Deep Learning from scratch" with Docker
Create a Todo app with Django ① Build an environment with Docker
Create a USB boot Ubuntu with a Python environment for data analysis
Django + Apache with mod_wsgi on Windows Server 2016
CentOS 6.4 with Python 2.7.3 with Apache with mod_wsgi and Django
Create an update screen with Django Updateview
Steps to build a Django environment with Win10 WSL Ubuntu18.04 + Anaconda + Apache2
Create execution environment for each language with boot2docker
Build a Django environment with Vagrant in 5 minutes
Create a dashboard for Network devices with Django!
Create Nginx + uWSGI + Python (Django) environment with docker
Try building an environment for MayaPython with VisualStudioCode
Procedure for creating an application with Django with Pycharm ~ Preparation ~
Procedure when building Ubuntu as a virtual environment with Vagrant + Virtualbox for LPIC Level 1.
Building an environment for "Tello_Video" on Mac OS X
Create a python development environment with vagrant + ansible + fabric
Building an environment for natural language processing with Python
Create an environment for MkDocs on Amazon Linux (attempted)
Create an OpenAI Gym environment with bash on Windows 10
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)
Build a Django environment for Win10 (with virtual space)
I tried to build an environment for machine learning with Python (Mac OS X)
Rebuilding an environment for machine learning with Miniconda (Windows version)
Linux beginners create Ubuntu 16.04 environment on Docker (for Mac)-first half-
[Memo] Build a development environment for Django + Nuxt.js with Docker
Create an LCD (16x2) game with Raspberry Pi and Python
Minimum Makefile and buildout.cfg to create an environment with buildout
I'm trying to create an authentication / authorization process with Django
Create an authentication feature with django-allauth and CustomUser in Django
Build a LAMP environment with Vagrant (Linux + Apache + MySQL + PHP)
[Django] Memo to create an environment of Django + MySQL + Vue.js [Python]
Create a social integration API for smartphone apps with Django
[Python] Create a screen for HTTP status code 403/404/500 with Django
Using X11 with ubuntu18.04 (C)
Python3 + Django ~ Mac ~ with Apache
Create a homepage with django
Build python3 environment with ubuntu 16.04
Django --Apache mod_wsgi virtualhost deployment
(Note) Django in Vagrant environment
Create a Python execution environment for Windows with VScode + Remote WSL
Create an alias for Route53 to CloudFront with the AWS API
When creating an environment that uses python django on Ubuntu 12.04 LTS
Create a development environment for Go + MySQL + nginx with Docker (docker-compose)
I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry
Create an arbitrary machine learning environment with GCP + Docker + Jupyter Lab
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
Load Django modules with an interpreter
Django beginners tried building an environment
[For beginners] Django -Development environment construction-
Create a virtual environment with Python!
Create an Excel file with Python3
Set multiple WSGIPythonPath with Apache + mod_wsgi
Run python3 Django1.9 with mod_wsgi (deploy)
Create API using hug with mod_wsgi
Created an environment for Anaconda & Jupyter