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.
--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.
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
.
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
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
Introduce a web server. Aim for apache + mod-wsgi.
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
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
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 ...
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)
$ 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
$ 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.
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/
In /var/www/html/robots.txt
User-agent : *
Disallow : /
Create and save
Recommended Posts