A baseline Linux server is configured to serve a web app built with Flask. The server was updated and secured and also configured to serve data from a Postgresql database.
The web server was configured with these steps;
-
Set up a new remote Linux instance on Amazon Lightsail
-
Create a Static IP address 18.219.29.211
-
Downloaded the private key file from Amazon as a
*pem
file. -
Generated the Private key in Puttygen and saved it as a
*ppk
file. -
Loaded the
*ppk
private keys to Putty and configured it to allow remote connection to the Amazon Linux instance. -
Saved my session on putty to auto login
-
Run
sudo apt-get update
to list the required updates. -
Run
sudo apt-get upgrade
to upgrade the software. -
Create new user
grader
default passwordgrader
usingsudo adduser grader
-
Create a
sudoers.d
for grader usingsudo touch etc/sudoers.d/grader
-
Edit with nano
sudo nano etc/sudoers.d/grader
-
Add
grader ALL = (ALL) NOPASSWD:ALL
into the file, this gives the grader user sudo access. -
Make a
.ssh
folder inside the home directory of user grader, ``mkdir .ssh``` -
cd into
.ssh
and create aauthorized_keys
file usingsudo touch authorized_keys
-
Copy the content of
authorized_keys
in theubuntu
user and paste intoauthorized_keys
ingrader
user, also make sure.ssh
andauthorized_keys
owner and group is grader. This allows the grader to login with ssh. -
Change file permissions on
.ssh
and.ssh/authorized_keys
in grader user using chmod 700 and 644 respectively. -
cd to
etc/ssh
, then usesudo nano sshd_config
to change ssh port from 20 to 2200. -
Deny incoming request with
sudo ufw default deny incoming
-
Allow outgoing connection with
sudo ufw default allow outgoing
-
Change ssh from 22 to 2200 with
sudo ufw allow 2200/tcp
-
Use
sudo ufw allow www
to set HTTP to port 80 -
Use
sudo ufw allow 123/tcp
to set NTP to port 123 -
sudo ufw enable
to enable the firewall -
Add custom TCP in the Amazon Lightsail account with TCP 2200
-
Restart linux instance and change port on putty to 2200, then log in again.
-
Install Apache with
sudo apt-get install apache2
-
Go to 18.219.29.211 to check if apache is working
-
Run
sudo apt-get install libapache2-mod-wsgi
-
Run
sudo apt-get install postgresql
-
Enabled automatic update with
sudo dpkg-reconfigure --priority=low unattended-upgrades
-
run
sudo su postgres
to switch to the postgres user. -
type
psql
to change to the Postgresql terminal. -
create a new database called catalog with
CREATE DATABASE catalog;
-
create user
CREATE USER catalog WITH PASSWORD 'catalog';
-
grant privileges with
GRANT ALL PRIVILEGES ON DATABASE catalog TO catalog;
-
installed mod-wsgi with
sudo apt-get install libapache2-mod-wsgi
-
edited
/etc/apache2/sites-enabled/000-default.conf
file by addingWSGIScriptAlias / /var/www/FlaskApp2/flaskapp2.wsgi
before the ending<VirtualHost *:80>
block. -
created a FlaskApp2.conf file with
sudo nano /etc/apache2/sites-available/FlaskApp.conf
and added the following block of code to it;
<VirtualHost *:80>
ServerName 18.219.29.211
ServerAdmin ezeh.chike
WSGIScriptAlias / /var/www/FlaskApp2/flaskapp2.wsgi
<Directory /var/www/FlaskApp2/FlaskApp2/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/FlaskApp2/FlaskApp2/static
<Directory /var/www/FlaskApp2/FlaskApp2/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
- resarted apache2 with
sudo apache2ctl restart
-
installed git with
sudo apt-get install git
-
intalled python and all the app dependencies.
-
cloned the catalog app with
git clone https://github.com/chikeezeh/Project_Item_Catalog.git
-
checked out the linux branch from the repo with
sudo git checkout linux
-
Changed the ownership of all the files in the project with
chown grader:grader *
-
arranged my folder structure as shown in this article
-
run
python database_setup.py
to create the database -
run
python populate_database.py
to populate the database -
put the following code in the flaskapp2.wsgi file ;
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, "/var/www/FlaskApp2/")
from FlaskApp2 import app as application
application.secret_key = 'Add your secret key'
- restart apache and go to 18.219.29.211 to launch the app.
https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps