Set up a web server with CentOS7 + Anaconda + Django + Apache

5 minute read

I wanted to run an image processing program made with Anaconda with Apache + Django, so a simple memo for myself. I don’t do anything like SSL or security.

Installation of CentOS 7

From the official website, click x86_64 of centos7, install CentOS-7-x86_64-DVD-2003.iso, and burn it to a USB memory. It’s 4.7GB, so it seems impossible with a DVD-R.
https://www.centos.org/download/

Insert the USB containing the image into the PC you want to install and start the CentOS installer.
When you start it, you will see various things such as language settings, but I think it will be easier to use if you select “GNOME Desktop” in the software settings.
You can set the host name in the network and host name settings, but you can change it later, so the default is fine, but I think it is better to set it in the FQDN format including the domain name.
I will install it as it is.
The explanation of this area is described in Install CentOS in an easy-to-understand manner (I also installed it while looking at this).
Set the root password along the way, but be sure to remember it.

Apache installation

After successfully installing CentOS, let’s start the terminal. Here, we will install Apache with the command yum.

$ su - 
password: (rootのpassword)
# yum list | grep httpd
httpd.x86_64
httpd-devel.x86_64
httpd-manual.noarch
httpd-ttools.x86_64
(Various below)

# yum -y install httpd httpd-tools httpd-devel httpd-manual
(Installation)

# yum list installed | grep httpd
httpd.x86_64
httpd-devel.x86_64
httpd-manual.noarch
httpd-ttools.x86_64

# systemctl start httpd
# systemctl status httpd
(Here, ● httpd.service - The Apache HTTP Server)If something like this appears, it has started normally.)

From here, set automatic startup
# systemctl enable httpd
# systemctl is-enabled httpd
enabled

Opening port 80
# iptables -I INPUT 7 -p tcp --dport 80 -j ACCEPT
# service iptables save

# firewall-cmd --add-service=http --permanent
# firewall-cmd --reload
success
# getenforce
Enforcing
# vi /etc/selinux/config
Change from enforcing to disabled
SELINUX=disabled

Restart your computer once

# getenforce
Disabled

This is ok. If you can do this far

# ifconfig
enp3s0: flags=***
        inet 192.168.***.***  netmask 255.255.240.0
        (various)

Check the IP address with, enter something like http : //192.168.〇〇〇.〇〇〇 in the browser, and if the welcome page “Testing 123 …” is displayed, you are done.

Installation of Anaconda

When installing Anaconda, include the version control tool pyenv.

Package update
# yum update -y

Get pyenv from git
# cd /usr/local/bin
# git clone git://github.com/yyuu/pyenv.git ./pyenv

Set the path for access
# echo 'export PYENV_ROOT="/usr/local/bin/pyenv"' >> ~/.bashrc
# echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
# echo 'eval "$(pyenv init -)"' >> ~/.bashrc

# source ~/.bashrc
# exec $SHELL -l
# pyenv --version
pyenv 1.2.20-7-gdd62b0d

When this happens, the installation of pyenv is complete.
Now, let’s install Anaconda.

# pyenv install --list | grep anaconda
(There will be various packages, so get the latest version.)
(Anaconda3 to match with the module I will use later-4.4.I chose 0)
# pyenv install anaconda3-4.4.0
# pyenv rehash
# pyenv global anaconda3-4.4.0
# pyenv version
anaconda3-4.4.0 (set by /usr/local/bin/pyenv/version)
# python -V
Python 3.6.1

This completes the Anaconda installation.

Django installation

I want to use Django, so I’ll install it.

update of conda
# conda update conda
# conda update --all
# conda install django

Since it’s a big deal, I will create a virtual environment.

# source create -n django
# source activate django

By the way, when leaving the virtual environment

# source deactivate django

Let’s say.

Check if you can actually use django.

# cd /var/www/
The name here is not komon, you can use any name you like(mysite etc.)
# django-admin startproject komon
# cd komon
# python manage.py makemigrations
# python manage.py migrate
# python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.

*Month**, 2020 - **:**:**
Django version 3.1, using settings 'komon.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-

Try typing http : //127.0.0.1:8000 in your browser, and if you see a rocket-like welcome page, you’re done.

Now let’s actually display the characters.

# python manage.py startapp myapp
# cd myapp
# vi views.py ->Edit
# vi urls.py  ->Edit

views.py


from django.shutcuts import render
# create your views here.
from django.http import HttpResponse

def index(request):
  return HttpResponse("Hello, World!")

urls.py


from django.urls import path
from . import views

urlpatterns = [
  path('', views.index, name='index'),
]
# cd ..
# cd komon
# vi urls.py  ->Edit
# vi settings.py  ->Edit

urls.py


from django.contrib import admin
from django.urls import path, include
urlpatterns = [
  path('admin/', admin.site.urls),
  path('', include('myapp.urls')),
]

settings.py


DEBUG = False  <-From True to False
ALLOWED_HOSTS = ['*']

INSTALLED_APPS - [
  'django.contrib.admin',
  'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.messages',
  'django.contrib.staticfiles',
  'myapp',  <-add to
]

ROOT_URLCONF = 'komon.urls'

WSGI_APPLICATION = 'komon.wsgi.application'

This completes the settings.

WSGI installation

I think you can install django relatively well, but the stumbling block is the installation of WSGI. Apache doesn’t support python by default, so it seems like using wsgi to recognize django.
I have installed various things with yum so far, but it seems that installing with yum will connect with the python2 system that is included in CentOS by default, so let’s use pip.

# pip install mod_wsgi
# python -c "import sys;print(sys.path)"
['','/usr/local/bin/pyenv/versions/anaconda3-4.4.0/lib/python36.zip', 
'/usr/local/bin/pyenv/versions/anaconda3-4.4.0/lib/python3.6', 
'/usr/local/bin/pyenv/versions/anaconda3-4.4.0/lib/python3.6/lib-dynload', 
'/usr/local/bin/pyenv/versions/anaconda3-4.4.0/lib/python3.6/site-packages']
Because it comes out
/usr/local/bin/pyenv/versions/anaconda3-4.4.0/lib/python3.6/site-Type packages with the following command.

# ls /usr/local/bin/pyenv/versions/anaconda3-4.4.0/lib/python3.6/site-packages/mod_wsgi/server/
mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so
Because it becomes
/usr/local/bin/pyenv/versions/anaconda3-4.4.0/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so
Let's remember.

From here, I’ll connect it to the Django project I created earlier.

# cd /etc/httpd/conf.d/
# vi django.conf

Since it will be a blank text with nothing written here, I will write the following.

django.conf


LoadModule wsgi_module /usr/local/bin/pyenv/versions/anaconda3-4.4.0/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so (The one from earlier)
ServerName 192.168.***.*** (IP address)
WSGIScriptAlias / /var/www/komon/komon/wsgi.py
WSGIPythonPath /var/www/komon:/usr/local/bin/pyenv/versions/anaconda3-4.4.0/bin/python

<Directory /var/www/komon/komon>
  <Files wsgi.py>
    Require all granted
  </Files>
</Directory>
# systemctl restart httpd

Start your browser, enter http : //192.168.〇〇〇.〇〇〇, and if Hello, World! Is displayed, it’s okay. If it is the same network, you can see it from other PCs.
Continued at https://qiita.com/hinoma/items/9c57a1cf214ebf137ace

Referenced URL / site

https://www.rem-system.com/centos-install/
https://hombre-nuevo.com/python/python0032/
https://engineers.weddingpark.co.jp/?p=1031
https://qiita.com/dekosuke-menti/items/e416f198980c0fd6e75b

As I wrote at the beginning, I haven’t done anything about security. I think that errors etc. occupy the part related to mod_wsgi, so I think it is good to check the error code etc.