Until you publish your Django application (+ MySQL) on AWS EC2 (+ RDS (+ S3))

How can I publish a web application with Django + Apache (+ mod-wsgi) + MySQL from the state where there are EC2 instance and RDS instance of AWS? It is assumed that there is a web application program. The information is as of January 1, 2020.

Also, I am using pyenv + Anaconda, but please adjust Python to yum install or Pipenv at this time according to your religion.

Version used, etc.

Python related

--Python 3.7.4 (using anaconda3-2019.10)

Apache,MySQL Client

$ httpd -v
Server version: Apache/2.4.41 ()
Server built:   Oct 22 2019 22:59:04
$ mysql --version
mysql  Ver 8.0.18 for Linux on x86_64 (MySQL Community Server - GPL)

MySQL uses version 8.0 series.

Install pyenv and Python3

Install pyenv

I don't want the default python to get dirty.

sudo git clone git://github.com/yyuu/pyenv.git /usr/local/pyenv
%The owner is sudo. Change recursively
sudo chown -r ec2-user:ec2-user pyenv

Rewrite ~ / .bash_profile

export PATH
export PYENV_ROOT="/usr/local/pyenv"
export GOPATH="$HOME/.go"
export PATH="$GOPATH/bin:$PYENV_ROOT/bin:$PYENV_ROOT/bin/pyenv:$PATH"
eval "$(pyenv init -)"

After rewriting, read it with source .bash_profile.

Install Python3

I want to have fun, so I'll add anaconda. I'm not involved in religious wars. I never use the installation with conda.

#Latest stable version at the time of writing
pyenv install anaconda3-2019.10
#Change the default
pyenv global anaconda3-2019.10

Introducing Django

This is easy. Just be careful with the Django version. Often changes that are not backwards compatible.

pip install django

Other useful guys

%Handle files
pip install django-storages
%Maintenance mode
pip install django-maintenance-mode
%Handle s3
pip install boto3

Web server introduction

Introduce a web server. Aim for apache + mod-wsgi.

apache installation

sudo yum install httpd
sudo systemctl start httpd
#If you want to check
sudo systemctl status httpd
#Service start
sudo systemctl start httpd.service

If you specify the global IP, the Apache test page will be displayed.

If you want something other than the Apache test page, rewrite /etc/httpd/conf/httpd.conf.

Referenced page: https://qiita.com/saki-engineering/items/5ea8e66b498fc7d4a399 https://miyabi-lab.space/blog/16#200

Introduction of mod-wsgi

A tool that connects Python and apache.

You will need apex and gcc when compiling for installation, but they are not included by default.

sudo yum install httpd-devel
sudo yum install gcc

This way mod-wsgi will come in with pip

pip install mod_wsgi

Link

The following assumes that you have a page to publish on Django. It can be a sample.

Partially referenced page: https://qiita.com/saki-engineering/items/5ea8e66b498fc7d4a399

Find the location where mod-wsgi is located. After moving to the root directory

$ find -name 'mod_wsgi*.so' 2> /dev/null
./usr/local/pyenv/versions/anaconda3-2019.10/lib/python3.7/site-packages/mod_wsgi/server/mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so

So it's in /usr/local/pyenv/versions/anaconda3-2019.10/lib/python3.7/site-packages/mod_wsgi/server/mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so.

For Amazon Linux 2, maybe at the end of /etc/httpd/conf/httpd.conf

# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf

Should be written, so in /etc/httpd/conf.d/myconf.conf

LoadModule wsgi_module /usr/local/pyenv/versions/anaconda3-2019.10/lib/python3.7/site-packages/mod_wsgi/server/mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so

WSGIScriptAlias / (wsgi.Where py was placed)
WSGIPythonPath (The root directory of a webapp program written in Django)
WSGIPythonHome (Parent directory of the Python executable you want to run;Example: /usr/local/pyenv/versions/anaconda3-2019.10)
Alias /static/ (Directory of static files for webapp programs written in Django)

<Directory (The root directory of a webapp program written in Django)>
    <Files wsgi.py>
        Order deny,allow
        Require all granted
    </Files>
</Directory>
<Directory (Directory of static files for webapp programs written in Django)>
    Require all granted
</Directory>

Anyway. sudo service httpd restart.

I didn't have to specify WSGIPythonPath the last time I did it, why ...

Introduction of MySQL

It is assumed that the MySQL 8.0 server has already been started with RDS.

Put the client for MySQL 8.0 Referenced page: https://dev.classmethod.jp/cloud/aws/amazon-linux2-mysqlclient/

#Add repository
$ sudo yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
#8 in standard condition.It is set to 0, but it is valid for the time being
$ sudo yum-config-manager --enable mysql80-community
# Mysql Client Install
$ sudo yum install -y mysql-community-client
$ mysql --version
mysql  Ver 8.0.18 for Linux on x86_64 (MySQL Community Server - GPL)

Server start

$ whereis mysql_config
mysql_config:

Actually, at this point, I get angry if I don't have mysql_config.

$ sudo yum install mysql-devel

Then

$ whereis mysql_config
mysql_config: /usr/bin/mysql_config /usr/share/man/man1/mysql_config.1.gz

And install with pip to access the mysql client from Python

pip install mysqlclient

I want infinite pagination

$ pip install django-el-pagination

The one who entered with was old. It hasn't been released for about two years. Then drop it from git (maintenance is neat and Django 3.0 will be supported soon)

$ git clone https://github.com/shtalinberg/django-el-pagination.git
$ cd django-el-pagination/
$ pip install -e .

However, even if I do this, it tries to work, but I cannot access the website. It becomes ModuleNotFoundError el_pagination. Is it caused by poor linking? Or is it due to editable mode?

Then let's pip install directly from git.

pip install git+https://github.com/shtalinberg/django-el-pagination.git

It's done.

Other necessary settings (settings.py) https://django-el-pagination.readthedocs.io/en/latest/start.html See.

Securing swap area

Required because the default memory size for EC2 instances is impossibly small.

$ sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
2048+0 Record input
2048+0 record output
2147483648 bytes(2.1 GB)Copied, 31.4376 seconds, 68.3 MB/Seconds
$ sudo chmod 600 /swapfile
$ sudo mkswap /swapfile
Set swap space version 1. size= 2 GiB (2147479552 bytes)
No label, UUID=82de405a-d7ef-4ae3-ba93-16baa38c6e2c
$ sudo swapon /swapfile
#Verification
swapon -s
Filename Type Size Used Priority
/swapfile                              	file    	2097148	0	-2
#Swap area is enabled at startup
$ sudo vi /etc/fstab
#add to
# /swapfile swap swap defaults 0 0

Reference: https://aws.amazon.com/jp/premiumsupport/knowledge-center/ec2-memory-swap-file/

Avoid searching

In /var/www/html/robots.txt

User-agent : *
Disallow : /

Create and save

Recommended Posts

Until you publish your Django application (+ MySQL) on AWS EC2 (+ RDS (+ S3))
# 1 Until you deploy Django's web application (instance construction with EC2 on AWS)
Deploy your Django application on Heroku
Until you publish (deploy) a web application made with bottle on Heroku
[AWS EC2] How to install only MySQL client on Amazon Linux 2 and connect to RDS
Publish your Django app on Amazon Linux + Apache + mod_wsgi
Single sign-on to your Django application with AWS SSO
[AWS EC2] Settings you want to do on Amazon Linux 2
Deploy a Django application on EC2 with Nginx + Gunicorn + Supervisor
Install Django on your Mac
# 3 Build a Python (Django) environment on AWS EC2 instance (ubuntu18.04) part2
Create an AWS Cloud9 development environment on your Amazon EC2 instance
Launched a web application on AWS with django and changed jobs
Run TensorFlow on a GPU instance on AWS
Try Tensorflow with a GPU instance on AWS
Run GPU version tensorflow on AWS EC2 Spot Instances
Use jupyter on AWS GPU instance
What to do if you forget your login password on Manjaro Linux
June 2017 version to build Tensorflow / Keras environment on GPU instance of AWS
Until you publish your Django application (+ MySQL) on AWS EC2 (+ RDS (+ S3))
I installed TensorFlow (GPU version) on Ubuntu
Import your own functions on AWS Glue