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.

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
(Various below)

# yum -y install httpd httpd-tools httpd-devel httpd-manual

# yum list installed | grep httpd

# 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

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
# getenforce
# vi /etc/selinux/config
Change from enforcing to disabled

Restart your computer once

# getenforce

This is ok. If you can do this far

# ifconfig
enp3s0: flags=***
        inet 192.168.***.***  netmask

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:// ./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 makemigrations
# python migrate
# python 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 migrate' to apply them.

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

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

Now let’s actually display the characters.

# python startapp myapp
# cd myapp
# vi ->Edit
# vi  ->Edit

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

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

from django.urls import path
from . import views

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

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

DEBUG = False  <-From True to False

  '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)"
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/
Because it becomes
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.


LoadModule wsgi_module /usr/local/bin/pyenv/versions/anaconda3-4.4.0/lib/python3.6/site-packages/mod_wsgi/server/ (The one from earlier)
ServerName 192.168.***.*** (IP address)
WSGIScriptAlias / /var/www/komon/komon/
WSGIPythonPath /var/www/komon:/usr/local/bin/pyenv/versions/anaconda3-4.4.0/bin/python

<Directory /var/www/komon/komon>
    Require all granted
# 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

Referenced URL / site

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.