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

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.

Recommended Posts

Set up a web server with CentOS7 + Anaconda + Django + Apache
Set up a Samba server with Docker
Set up a simple HTTPS server with asyncio
Set up a local server with Go-File upload-
Set up a local server with Go-File download-
Set up reverse proxy to https server with CentOS Linux 8 + Apache mod_ssl
[Vagrant] Set up a simple API server with python
Build a web application with Django
Django + Apache with mod_wsgi on Windows Server 2016
Set up a mail server using Twisted
CentOS 6.4 with Python 2.7.3 with Apache with mod_wsgi and Django
I made a WEB application with Django
Send mail with mailx to a dummy SMTP server set up with python.
Build a CentOS Linux 8 environment with Docker and start Apache HTTP Server
Set up a local web server in 30 seconds using python 3's http.server
Steps to build a Django environment with Win10 WSL Ubuntu18.04 + Anaconda + Apache2
Set up a simple HTTPS server in Python 3
Start a simple Python web server with Docker
Set up a test SMTP server in Python.
Set up a UDP server in C language
Launch a web server with Python and Flask
How to set up a local development server
Set up a simple SMTP server in Python
Introduction and usage of Python bottle ・ Try to set up a simple web server with login function
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a Web server --2 PHP introduction
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a Web server ―― 1. Apache introduction
Looking back on creating a web service with Django 1
Set up a file server on Ubuntu 20.04 using Samba
I set up Django from scratch (Vagrant, Centos, Python3)
Looking back on creating a web service with Django 2
Set up a free server on AWS in 30 minutes
[Part 1] Let's set up a Minecraft server on Linux
Create a home music server with Centos8 + Universal Media Server
Launch Django on a Docker container with docker-compose up
Set up a Minecraft resource (Spigot) server via docker
Deploy a real-time web app with swampdragon x apache
Set up a Python development environment with Sublime Text 2
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a web server --3. Use MySQL
Set up a Python development environment with Visual Studio Code
Easy web server construction & deployment with EB CLI + git + Django
Create a homepage with django
Web application creation with Django
Setting up a CentOS 7 server hosted on Alibaba Cloud ECS
Build a speed of light web API server with Falcon
Set up your own web server within your Pepper app project
Reload the server set up with gunicorn when changing the code
Create a web API that can deliver images with Django
Real-time web with Django Channels
(For beginners) Try creating a simple web API with Django
If you know Python, you can make a web application with Django
I made a web server with Raspberry Pi to watch anime
Web App Development Practice: Create a Shift Creation Page with Django! (Shift creation page)
Set up Ubuntu as a Linux cheat sheet and https server
Launched a web application on AWS with django and changed jobs
Linux Web server construction (Ubuntu & Apache)
Set up pygit2 with static link
Creating a Flask server with Docker
Deploy a Django application with Docker
Django Tips-Create a ranking site with Django-
Set multiple WSGIPythonPath with Apache + mod_wsgi
Server construction with CONOHA VPS (CentOS)