Setup Steps
-
Install virtual environment
pip3 install virtualenv
-
Create virtual environment
virtualenv venv --python=python3
-
Activate virtual environment
source venv/bin/activate
-
(optional) Leave virtual environment
deactivate
-
Install Flask-RESTful library 'pip install Flask-Restful'
Deploy to AWS EC2 instance
If deploying on AWS, after installing Ubuntu 16.04, make sure to install libpcre3 and libpcre3-dev by running the following commands:
sudo apt-get install libpcre3 libpcre3-dev
pip install uwsgi -I --no-cache-dir
While logged in as root user:
apt-get update
apt-get install postgresql postgresql-contrib
After installation, change user to postgres user by running
sudo -i -u postgres
Connect to the database by running
psql
To exit postgres database console, run
\q
To exit postgres user
exit
To create a new user, run the following command and enter the user details as prompted (password, full name etc). Remember to replace "jose" with the name of your user.
adduser jose
Running the visudo command will open a file (normally located at /etc/sudoers).
visudo
Under "User privilege specification", add the following line below root line
jose ALL=(ALL:ALL) ALL
Save the file with CNTR+O and press enter to save it. Then CNTR+X to exit the file.
To enable logging in to the server as the newly created user, enable password login to the server by running
vi /etc/ssh/sshd_config
This opens another file. To disable root login with password, change the following line. Note you need to press "i" key to go to insert/edit mode before changing the contents of the file.
PermitRootLogin yes
to
PermitRootLogin no
Scroll down to the end of the file and add the following line
AllowUsers jose
To exit edit/insert mode, press escape key then ":" followed by wq and press enter. "wq" writes the file to disc and quits the file.
Finally, run
service sshd reload
This section assumes you created a new unix user in Ubuntu 16.04 (instructions in the previous lecture) and you are logged into the server as the new user.
To become the postgres user, run
sudo su
sudo -i -u postgres
To create a postgres user, run the following command. Note, the user must have the same name as the unix user logged into the server ("jose" in our case).
createuser jose -P
You will prompted twice to set the password for the new postgres user.
To create a database, run
createdb jose
To enforce password login to PostgreSQL with user jose, run the following commands. Note 9.5 is the PostgreSQL version installed in your server. Later this version may change so make sure to change your accordingly.
vi /etc/postgresql/9.5/main/pg_hba.conf
Scroll down to the bottom and change "peer" to "md5" in the following line under '# "local" is for unix domain socket connections only comment'. This is how the line should look after changing.
local all all md5
Finally, write and quit.
Quit postgres user
exit```
## Setting up Nginx
To install and cofigure Nginx on your server, follow the following instructions.
First, you need to update your server by running
```bash
sudo apt-get update
To install Nginx, run
sudo apt-get install nginx
Allow Nginx access through firewall (otherwise incoming requests will be blocked by the firewall)
sudo ufw enable
sudo ufw allow 'Nginx HTTP'
Also since we have enabled firewall, remember to allow ssh through the firewall, else you will get locked out of the server.
sudo ufw allow ssh
You can check firewall status by using
sudo ufw status
To check Nginx status, use
systemctl status nginx
The following commands stop, start and restart Nginx respectively.
systemctl stop nginx
systemctl start nginx
systemctl restart nginx
Create a new file for the items REST API configuration.
sudo vi /etc/nginx/sites-available/items-rest.conf
Press "i" key (insert mode), copy and paste the following to the file
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/items-rest/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;
}
}
After writing and quiting the file (escape key then :wq enter) enable the configuration by running
sudo ln -s /etc/nginx/sites-available/items-rest.conf /etc/nginx/sites-enabled/
Create a directory/folder for the app
sudo mkdir /var/www/html/items-rest
Since the director was created with root user, give access to the unix user ("jose" in our case) by making the user the owner of the directory.
sudo chown jose:jose /var/www/html/items-rest
Got to the directory, clone the app and install dependencies. Run the following commands one by one in that order.
cd /var/www/html/items-rest
git clone https://github.com/schoolofcode-me/stores-rest-api.git .
mkdir log
sudo apt-get install python3-pip python3-dev libpq-dev
sudo pip3 install virtualenv
virtualenv venv --python=python3
source venv/bin/activate
pip install -r requirements.txt
This guide will help you set up uWSGI on your server to run the items rest app. Go to the items-rest directory we created in the previous lecture.
cd /var/www/html/items-rest
Run the following command to create a service file.
sudo vi /etc/systemd/system/uwsgi_items_rest.service
Copy and paste the following to the file. Note "jose:1234" is the username:password combination of the Postgres user we created before. Change yours accordingly.
[Unit]
Description=uWSGI items rest
[Service]
Environment=DATABASE_URL=postgres://jose:1234@localhost:5432/jose
ExecStart=/var/www/html/items-rest/venv/bin/uwsgi --master --emperor /var/www/html/items-rest/uwsgi.ini --die-on-term --uid jose --gid jose --logto /var/www/html/items-rest/log/emperor.log
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all
[Install]
WantedBy=multi-user.target
Replace the uwsgi.ini file contents with the following
sudo vi uwsgi.ini
[uwsgi]
base = /var/www/html/items-rest
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/items-rest/log/%n.log
Finally start the app by running
sudo systemctl start uwsgi_items_rest