[PYTHON] Steps to build a Django environment with Win10 WSL Ubuntu18.04 + Anaconda + Apache2

I will summarize the procedure to create a Django + Apache development environment on Ubuntu 18.04 of Windows 10 WSL


Allows apache2 to access python via mod_wsgi. Also, I will make VS code available as a remote IDE that can be used with WSL.

  1. Install anaconda3
  2. Install django
  3. Create a django project and migrate
  4. Install apache2
  5. Install mod_wsgi
  6. Added to apache2 configuration file
  7. Make vscode available

Since systemctl cannot be used in WSL, the operation of apache changes a little, when using anaconda, you have to change the reference setting by yourself unless you install mod_wsgi with pip, the setting of apache2.conf is difficult to understand, it is easy to stumble around I think it's the place.

1. Install WSL

Enable WSL and install Ubuntu 18.04 https://qiita.com/matarillo/items/61a9ead4bfe2868a0b86



sudo apt update
sudo apt upgrade

2. Install Apache2

Install and start apache2 and apache2-dev


sudo apt install apache2
sudo apt install apache2-dev
sudo /etc/init.d/apache2 start

Now apache should wait at http: // localhost. If you open it in a browser, index.html in the document root / var / www / html / will open. image.png To stop it is stop


sudo /etc/init.d/apache2 stop

When restarting to reflect the setting change, restart


sudo /etc/init.d/apache2 restart

3. Install Anaconda

Install pyenv


git clone https://github.com/yyuu/pyenv.git ~/.pyenv

Through PATH


echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
source ~/.bashrc

Check the version of anaconda you can install


pyenv install -l | grep anaconda

Install the version of anaconda of your choice. Below, the 2020.02 version is installed.


pyenv install anaconda3-2020.02
pyenv rehash
pyenv global anaconda3-2020.02

Put it in your PATH.


echo 'export PATH="$PYENV_ROOT/versions/anaconda3-2020.2/bin/:$PATH"' >> ~/.bashrc
conda init
source ~/.bashrc

Create a conda environment. Below we are creating a python3.7 environment named django. xxxx is the user name.


conda create -n django python=3.7
echo 'export PYTHONHOME=/home/xxxx/.pyenv/versions/anaconda3-2020.02' >> ~/.bashrc
echo 'export PYTHONPATH=/home/xxxx/.pyenv/versions/anaconda3-2020.02/bin:/home/xxxx/.pyenv/versions/anaconda3-2020.02/lib/python3.7/site-packages' >> ~/.bashrc
conda create django

4. Install django

Install and check version


pip install django
django-admin --version

Let's create a project and move it. You can create a project named hoge by creating a directory and doing django-admin startproject hoge.


mkdir django
cd django
django-admin startproject proj1

Execute manage.py created in the created directory from python to run a simple server and check the operation.


cd proj1
python manage.py migrate
python manage.py runserver

Open http: // localhost: 8000 in your browser and you should see the django sample page.

5. Install mod_wsgi

There is also a way to install mod_wsgi from apt, but mod_wsgi installed with apt is set to refer to python when apt install python is done instead of anaconda by default. If you use anaconda, I think it's easier to install from pip.


pip install mod_wsgi
pip install mod-wsgi-httpd

Put it in your PATH. xxxx is the user name.


echo 'export PATH=/home/xxxx/.pyenv/versions/anaconda3-2020.02/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

Mod_wsgi should now work. Let's run a simple server and check the operation. The following command will wait for the simple server at http: // localhost: 8000, so open it in your browser.


mod_wsgi-express start-server
If the mod_wsgi-express server is working properly, you will see an image that does not look like the sample page above.

Finally, get the information to write in the Apache2 configuration file.


mod_wsgi-express module-config


LoadModule wsgi_module "/home/xxxx/.pyenv/versions/anaconda3-2020.02/lib/python3.7/site-packages/mod_wsgi/server/mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so"
WSGIPythonHome "/home/xxxx/.pyenv/versions/anaconda3-2020.02"

Let's copy the above somewhere.

6. Associate apache and mod_wsgi

Add apache2.conf as officially done by django. https://docs.djangoproject.com/ja/2.0/howto/deployment/wsgi/modwsgi/

If you created a django project in / home / xxxx / django /, it would look like this: The first two lines are a copy of what is displayed in mod_wsgi-express module-config, with the PATH for wsgi.py created in the django project directory created by WSGIScriptAlias and the manage.py for the django project created by WSGIPythonPath. The directory PATH and Direcoty are the above wsgi.py directory PATH.


LoadModule wsgi_module /home/xxxx/.pyenv/versions/anaconda3-2020.02/lib/python3.7/site-packages/mod_wsgi/server/mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so
WSGIPythonHome /home/xxxx/.pyenv/versions/anaconda3-2020.02

WSGIScriptAlias / /home/xxxx/django/proj1/proj1/wsgi.py
WSGIPythonPath /home/xxxx/django/proj1

<Directory /home/xxxx/django/proj1/proj1>
<Files wsgi.py>
Require all granted

I changed the apache settings, so restart it.


sudo /etc/init.d/apache2 restart

Open http: // localhost in your browser and check. image.png If it looks like the above, it's OK.

If the contents of apache2.conf are incorrect or some packages cannot be installed, an internal server error will occur as shown below. image.png It's hard to understand the cause with this alone, so let's judge by looking at the apache error log. If you keep the default settings, you can see it below.


tail -f /var/log/apache2/error.log

I think it's either because I forgot to put something in or the PATH is different.

7. Make it possible to develop with VScode

WSL does not have a GUI, but you can use VScode's WSL extension to develop with a GUI. Enable WSL extension after installing VScode on Windows 10. image.png When VS code is started from WSL with the following command, it seems that VS code on the Windows side will come to run from the WSL side.


cd test
code .


By the way, the permission limit is the same as Ubuntu, so you can edit with the authority when you execute code. For example, in / var / www / html / where the apahce static file is placed, the owner is root, so if you try to change it from the user account, you will get an error like the image below. You can change it by changing the owner, but I don't know if it's good for security, so please let me know. By the way, it seems that sudo code. Cannot be executed.


Unable to write file 'vscode-remote://wsl+ubuntu-18.04/var/www/html/test.html' (NoPermissions (FileSystemError): Error: EACCES: permission denied, open '/var/www/html/test.html')



https://docs.djangoproject.com/ja/2.0/howto/deployment/wsgi/modwsgi/ https://codelab.website/anaconda-django/ https://qiita.com/itisyuu/items/dafa535adc8197208af1 https://blog.capilano-fw.com/?p=369

Recommended Posts

Steps to build a Django environment with Win10 WSL Ubuntu18.04 + Anaconda + Apache2
Build a Django environment for Win10 (with virtual space)
I tried to build an environment with WSL + Ubuntu + VS Code in a Windows environment
Build a Django environment with Vagrant in 5 minutes
[Memo] Build a virtual environment with Pyenv + anaconda
Build a Django development environment with Doker Toolbox
Quickly build a Python Django environment with IntelliJ
[Linux] WSL2 Build an environment for laravel7 with Ubuntu 20.04
How to build a Django (python) environment on docker
Build a development environment with Poetry Django Docker Pycharm
Build python3 environment with ubuntu 16.04
Build a Django development environment with Docker! (Docker-compose / Django / postgreSQL / nginx)
How to build a new python virtual environment on Ubuntu
[Memo] Build a development environment for Django + Nuxt.js with Docker
Set up a web server with CentOS7 + Anaconda + Django + Apache
[Django] Build a Django container (Docker) development environment quickly with PyCharm
How to build a python2.7 series development environment with Vagrant
Build a LAMP environment with Vagrant (Linux + Apache + MySQL + PHP)
Build a Python environment with WSL + Pyenv + Jupyter + VS Code
Steps to develop Django with VSCode
Steps to create a Django project
Build a web application with Django
Build a Python environment on your Mac with Anaconda and PyCharm
# 3 Build a Python (Django) environment on AWS EC2 instance (ubuntu18.04) part2
Try to create a python environment with Visual Studio Code & WSL
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
Create an environment for Django x Apache x mod_wsgi with Vagrant (Ubuntu 16.04)
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
Change Python 64bit environment to 32bit environment with Anaconda
Easily build a development environment with Laragon
How to build a sphinx translation environment
Build a Tensorflow environment with Raspberry Pi [2020]
[Python] Create a virtual environment with Anaconda
I want to build a Python environment
Steps to install Python environment on Ubuntu
Build Python environment with Anaconda on Mac
[Linux] Build a jenkins environment with Docker
Build a python virtual environment with pyenv
Build a modern Python environment with Neovim
[Linux] Build a Docker environment with Amazon Linux 2
Steps to create a Python virtual environment with VS Code on Windows
Build a CentOS Linux 8 environment with Docker and start Apache HTTP Server
I tried to build a Mac Python development environment with pythonz + direnv
Summary of how to build a LAMP + Wordpress environment with Sakura VPS
Build Linux on a Windows environment. Steps to install Laradock and migrate
For beginners to build an Anaconda environment. (Memo)
Build a WardPress environment on AWS with pulumi
Build Django + NGINX + PostgreSQL development environment with Docker
Build the fastest Django development environment with docker-compose
Build python environment with pyenv on EC2 (ubuntu)
Build Python3.5 + matplotlib environment on Ubuntu 12 using Anaconda
[Latest] How to build Java environment on Ubuntu
Build a Django environment on Raspberry Pi (MySQL)
How to develop a cart app with Django
Building a Python environment with WLS2 + Anaconda + PyCharm
I tried to build an environment of Ubuntu 20.04 LTS + ROS2 with Raspberry Pi 4
Create a python3 build environment with Sublime Text3
Build a data analysis environment that links GitHub authentication and Django with JupyterHub
[0] TensorFlow-GPU environment construction built with Anaconda on Ubuntu
Ubuntu 16.04 LTS, beginner memorandum of environment construction to switch anaconda version with pyenv
Build a virtual environment with pyenv and venv