__memo_deploy application on ubuntu server

### Environment
Server: Utuntu 16.04
DB: PostgreSQL 9.5.18

### Postgresql install and setup with a new user.
```
$ apt-get update
$ apt-get install postgresql postgresql-contrib

$ sudo -i -u postgres
$ psql
> \conninfo
# You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432".
> \q
$ adduser july (Set password)
```

### Root permission and access control.

```
$ visudo
```

```vi:visudo
User privilege specification
july    ALL=(ALL:ALL) ALL
```

```
$ vi /etc/ssh/sshd_config
```
Add below line for new user.

```vi:sshd_config
AllowUsers july
```
```
$ service sshd reload
```

### Create user on postgresql

ssh july@*server ip address*

```
$ sudo su
$ sudo -i -u postgres
$ psql
> createuser july -P
> createdb july
> \q
$ exit
$ psql
july=> \conninfo
You are connected to database "july" as user "july" via socket in "/var/run/postgresql" at port "5432".
```

Modify db access control.

```
$ sudo vi /etc/postgresql/9.5/main/pg_hba.conf
```
change below line:

``` vi:pg_hba.conf(before)
# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
```
to:

```vi:pg_hba.conf(after)
# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
```

### Install ngix and configure

```
$ sudo apt-get update
$ sudo apt-get install nginx
$ sudo ufw status
$ sudo ufw enable
$ sudo ufw allow 'Nginx HTTP'
$ sudo ufw allow ssh
```

Create configuration file for our API.

```
$ vi /etc/nginx/sites-available/<app name>.conf
```

Config file:

```vi:xxx.config
server{
listen 80;
real_ip_header X-Forwarded-For;
set_real_ip_from 127.0.0.1;
server_name localhost;

location / {
include uwsgi_params;
uwsgi_pass unix:/var/www/html/<app name>/socket.sock;
uwsgi_modifier1 30;
}

error_page 404 /404.html;
location = /404.html{
root /usr/share/nginx/html;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
```


```
$ sudo ln -s /etc/nginx/sites-available/<app name>.conf /etc/nginx/sites-enabled
```

### Clone repository and install library

```
$ sudo mkdir /var/www/html/<app name>
$ sudo chown july:july /var/www/html/<app name>/
$ cd /var/www/html/<app name>/
$ git clone https://<app repository>
$ sudo apt-get install python-pip python3-dev libpq-dev
$ pip install virtualenv
$ virtualenv venv --python=python3.5
$ pip install -r requirements.txt
```

### Create service and define parameters

```
$ sudo vi /etc/systemd/system/<app name>.service
```

```vi:xxx.service
[Unit]
Description=uWSGI items rest

[Service]
Environment=DATABASE_URL=postgres://july:<password for postgresql user>@localhost:5432/july
ExecStart=/var/www/html/<app name>/venv/bin/uwsgi --master --emperor /var/www/html/<app name>/uwsgi.ini --die-on-term --uid july --gid july --logto /var/www/html/<app name>/log/emperor.log
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

[Install]
WantedBy=multi-user.target
```

### Procfile
```vi:Procfile
web: uwsgi uwsgi.ini
```

### Modify uwsgi.ini

```
vi uwsgi.ini
```

```vi:uwsgi.ini
[uwsgi]
base = /var/www/html/<app name>
app = run
module = %(app)

home = %(base)/venv
pythonpath = %(base)
socket = %(base)/socket.sock

chmod-socket = 777

processes = 8

threads = 8

harakiri = 15

callable = app

logto = /var/www/html/<app name>/log/%n.log
```

### Start services

```
$ sudo rm /etc/nginx/sites-enabled/default
$ sudo systemctl reload nginx
$ sudo systemctl restart nginx
$ sudo systemctl start <app name>
```