sudo apt-get install -y postgresql-9.5 postgresql-contrib-9.5 postgresql-server-dev-9.5 nginx git circus make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils python-setuptools
sudo adduser app
sudo passwd -l app
sudo adduser www-data app
sudo -u app -i
git clone https://github.com/yyuu/pyenv.git ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
exit
sudo -u app -i
pyenv install 3.7.0
pyenv local 3.7.0
pyenv versions
which python
git clone https... app
cd app
pip install --upgrade pip
pip install -r requirements.txt
python manage.py makemigrations
python manage.py migrate
python manage.py runserver 0.0.0.0:8000
pip install gunicorn
gunicorn -b 0.0.0.0:8000 app.wsgi
cd
sudo nano /etc/circus/conf.d/app.ini
Edit app.ini with:
[watcher:app]
cmd = gunicorn
args = -b 0.0.0.0 -w 2 app.wsgi
working_dir = /home/app/app
uid = app
numprocesses = 1
autostart = true
stdout_stream.class = FileStream
stdout_stream.filename = /home/app/logs/gunicorn.stdout.log
stderr_stream.class = FileStream
stderr_stream.filename = /home/app/logs/gunicorn.stderr.log
copy_env = true
virtualenv = /home/app/.pyenv/versions/3.5.2
virtualenv_py_ver = 3.5
sudo -u app -i
mkdir /home/app/logs
sudo service circusd restart
sudo service circusd status
ps aux | grep gunicorn
sudo nano /etc/nginx/sites-available/app
server {
listen 80;
server_name app.domain.com;
access_log /home/app/logs/nginx.access.log;
error_log /home/app/logs/nginx.error.log;
location / {
include proxy_params;
proxy_pass http://127.0.0.1:8000/;
}
}
sudo ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled/app
sudo nginx -t
sudo service nginx reload
To avoid show the nginx version in 404 errors, modify file /etc/nginx/nginx.conf
uncommenting text server_tokens off;
tail -f errors.log
The server included in django is not for production servers, so we have to create the static files. As app user:
sudo -u app -i
In:
nano app/app/settings_production.py
Add:
import os
from app.settings import *
DEBUG = False
TEMPLATE_DEBUG = False
ALLOWED_HOSTS = ['*']
SECRET_KEY = 'n-3(f-lo&$%j0$8&_-5hkk8emsvlx840%_vb8ce$_98)a(x_74'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
cd app
mkdir static
mkdir media
export DJANGO_SETTINGS_MODULE=app.settings_production
python manage.py collectstatic
It is possible make the last two steps with:
python manage.py collectstatic --settings=app.settings_production
In general user
Modify /etc/nginx/sites-available/app adding, before "location /" :
location /static {
access_log off;
alias /home/app/app/static;
}
location /media {
access_log off;
alias /home/app/app/media;
}
Add to /etc/circus/conf.d/app.ini:
[env:app]
DJANGO_SETTINGS_MODULE = app.settings_production
Firstly, open new port in aws but dosn't close the actual port.
Then, modify the line Port 22
in /etc/ssh/sshd_config
with Port <numberport>
.
To check ir there is any error in the ssh file config:
sudo sshd -t
If there is any error, it will show on screen. Otherwise it wioll not diplay any message.
Reload the service
sudo service ssh reload
Try, in other session witouth exit the actual session, to connect with by :
ssh -p <numberport> -i <route/pem> [email protected]
If this work, close the old port in aws.