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.
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.
Enable WSL and install Ubuntu 18.04 https://qiita.com/matarillo/items/61a9ead4bfe2868a0b86
Update
terminal
sudo apt update
sudo apt upgrade
Install and start apache2 and apache2-dev
terminal
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. To stop it is stop
terminal
sudo /etc/init.d/apache2 stop
When restarting to reflect the setting change, restart
terminal
sudo /etc/init.d/apache2 restart
Install pyenv
terminal
git clone https://github.com/yyuu/pyenv.git ~/.pyenv
Through PATH
terminal
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
terminal
pyenv install -l | grep anaconda
Install the version of anaconda of your choice. Below, the 2020.02 version is installed.
terminal
pyenv install anaconda3-2020.02
pyenv rehash
pyenv global anaconda3-2020.02
Put it in your PATH.
terminal
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.
terminal
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
Install and check version
terminal
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.
terminal
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.
terminal
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.
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.
terminal
pip install mod_wsgi
pip install mod-wsgi-httpd
Put it in your PATH. xxxx is the user name.
terminal
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.
terminal
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.
terminal
mod_wsgi-express module-config
terminal
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.
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.
/etc/apache2/apache2.conf
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
</Files>
</Directory>
I changed the apache settings, so restart it.
terminal
sudo /etc/init.d/apache2 restart
Open http: // localhost in your browser and check. 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. 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.
terminal
tail -f /var/log/apache2/error.log
I think it's either because I forgot to put something in or the PATH is different.
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. 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.
terminal
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.
terminal
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