Link Anaconda + Diango

Link Anaconda + Diango

In this article, we will create a LINE BOT by linking Anaconda + Diango. It's quite annoying so you shouldn't do it if you can lol

I will write from the point of creating the environment, please finish the basic Ubuntu settings before that.

Environment creation

Apache environment creation

Create an Apache environment. Install what you need

sudo apt-get install apache2 apache2-bin apache2-dev

Install Anaconda

Next, install Anaconda.

wget https://repo.anaconda.com/archive/Anaconda3-2020.02-Linux-x86_64.sh

sudo bash Anaconda3-2020.02-Linux-x86_64.sh

Follow the instructions to install. (To recognize conda as well)

Next, we will set up Anaconda. Install Django.

sudo conda install django

The version of Django is below:

python -m django --version
3.0.3

Django project creation

Next, we will create a Djanog project. The tutorial page was helpful: https://docs.djangoproject.com/ja/2.2/intro/tutorial01/

This time, execute as follows.

django-admin startproject linebot

This will create a folder called linebot under your current directory.

python manage.py runserver 0.0.0.0:8000

Now you can start the server, so take a look.

_2020-04-03_18.17.13.png

From here on, you can create Django applications and refer to the tutorials. (I will omit it here)

Django and Apache integration

Next, we will link Django, which is the number one demon, and Apache. To do this, use mod_wsgi to link Django and Apache.

First install

pip install mod_wsgi

Then find the installation location.

python -c "import sys; print(sys.path)"
['', '/home/vagrant/anaconda3/lib/python37.zip', '/home/vagrant/anaconda3/lib/python3.7', '/home/vagrant/anaconda3/lib/python3.7/lib-dynload', '/home/vagrant/.local/lib/python3.7/site-packages', '/home/vagrant/anaconda3/lib/python3.7/site-packages']

(base) vagrant@vagrant:~/.local/lib/python3.7/site-packages/mod_wsgi/server$ ls
__init__.py  apxs_config.py  management
__pycache__  environ.py      mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so

It depends on the environment, but this time I was doing it with vagrant, so it was in a place like this.

Put the path in /etc/apache2/mods-available/wsgi.load like this:

LoadModule wsgi_module /home/vagrant/.local/lib/python3.7/site-packages/mod_wsgi/server/mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so

You can also write this in /etc/apache2/mods-available/wsgi.conf.

<IfModule mod_wsgi.c>
    WSGIPythonHome /home/vagrant/anaconda3
    WSGIPythonPath /home/vagrant/anaconda3/bin
</IfModule>

Then enable wsgi.

sudo a2enmod wsgi

Enabling module wsgi.
To activate the new configuration, you need to run:
  systemctl restart apache2

It is okay if such a display appears.

Next, we will create a site configuration file.

/etc/apache2/sites-available/default-ssl.conf

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName mikuaka.ddo.jp
 
    RewriteEngine on
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
 
</VirtualHost>

NameVirtualHost *:443

<IfModule mod_ssl.c>
	<VirtualHost _default_:443>

    ServerAdmin [email protected]
    ServerName your.name.com
    ServerAlias www.your.name.com

    WSGIDaemonProcess name display-name=%{GROUP} user=www-data group=www-data python-path=/home/ubuntu/your/project
    WSGIProcessGroup name

    WSGIScriptAlias / /var/www/name/wsgi.py

    <Directory "/var/www/name">
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    Alias /static/ /var/www/name/static/

    <Directory /var/www/name/static/>
        Require all granted
    </Directory>

		ErrorLog ${APACHE_LOG_DIR}/error.log
		CustomLog ${APACHE_LOG_DIR}/access.log combined

		SSLEngine on
		SSLCertificateFile	/etc/letsencrypt/live/your.name.com/cert.pem
		SSLCertificateKeyFile	/etc/letsencrypt/live/your.name.com/privkey.pem
		SSLCertificateChainFile /etc/letsencrypt/live/your.name.com/chain.pem
		
 		<FilesMatch "\.(cgi|shtml|phtml|php)$">
				SSLOptions +StdEnvVars
		</FilesMatch>
		<Directory /usr/lib/cgi-bin>
				SSLOptions +StdEnvVars
		</Directory>

	</VirtualHost>
</IfModule>

I made it like this. ↑ also contains SSL settings.

Finally restart apache.

sudo service apache2 restart

How was it? The integration between Django and Apache is quite annoying, so I hope you understand what you don't want to do.

I am creating a LINE Bot using this environment, so if I can afford it, I would like to write an article there as well.

Recommended Posts

Link Anaconda + Diango
anaconda
Introduced anaconda
Anaconda 2.5 Release
Download Anaconda