Taking a baseline installation of a Linux server and preparing it to host our web applications, securing our server from a number of attack vectors, installing and configuring a database server, and deploying one of our existing web applications onto it.
The server used is a Google Cloud Compute Engine instance.
Public IP Address: 35.247.82.229
SSH Port: 2200
Live version of the WebApp:Food Catalog
Note: To access the live WebApp with Google Authentication use http://35.247.82.229.xip.io
Things required for this project:
- A browser
- A stable internet connection
- A terminal for connecting to linux server, preferably git bash if on Windows
Create a linux VM instance, see this guide for details
Note: Make sure to select Allow HTTP traffic in the Firewall section.
To connect to the instance, click on the SSH button present under the Connect column in the row of the instance
Run the following commands:
$ sudo apt-get update $ sudo apt-get upgrade
Configuring the server instance
Run the following command:
$ sudo nano /etc/ssh/sshd_configChange the line
Port 22toPort 2200Change the line
PermitRootLogin prohibit-passwordtoPermitRootLogin noSave and exit the file
Run the following command:
$ sudo service sshd restart
Configuring the Google Compute Engine firewall
Open the Firewall rules page
Click on CREATE FIREWALL RULE option
Add the following details:
- Name -
new-allow-ssh - Network -
default - Priority -
65534 - Direction of traffic -
Ingress - Allow on match -
Allow - Targets -
All instances in the network - Source IP ranges -
0.0.0.0/0 - Protocols and ports -
tcp:2200
- Name -
Click on Create
Delete the
default-allow-sshruleGo to VM instances page and click on the dropdown button under the Connect column in the row of the instance and then select Open in browser window on custom port option and open on port 2200. If everything is configured correctly, then you are logged in and therefore the port is changed successfully. Now you can close the previously opened instance window
Configure the firewall to only allow incoming connections for SSH (port 2200), HTTP (port 80), and NTP (port 123)
Run the following commands:
$ sudo ufw default deny incoming $ sudo ufw default allow outgoing $ sudo ufw allow 2200/tcp $ sudo ufw allow www $ sudo ufw allow ntp $ sudo ufw enable
To add the user grader, run:
$ sudo adduser graderTo give
sudoaccess to grader, run:$ sudo nano /etc/sudoers.d/graderNow in this file, write and save:
grader ALL=(ALL) NOPASSWD: ALL
On your local machine, create an SSH key pair by running
$ ssh-keygen -C graderin the terminal. Enter the location where you would save the key. Enter nothing if you dont want a passphraseNow back into the linux server instance, login as the grader user by running:
$ sudo su - graderMake a new directory:
$ mkdir .sshCreate and open a new file:
sudo nano .ssh/authorized_keysCopy the contents of the public key file(.pub) which you already generated on the local machine with the help of
ssh-keygencommandPaste the contents into the
authorized_keysfile, save and exit the fileChange the permisions of the
.sshdirectory and theauthorized_keysfile:$ chmod 700 .ssh $ sudo chmod 644 .ssh/authorized_keysGo to VM instances and click on the name of your instance. In the VM instance details page, click on EDIT option. Scroll down the page until you find SSH Keys section. Click on Show and edit option, then click on + Add item button. Paste your public key file(.pub) contents in the box and then click on the Save button
Run the command:
$ ssh -i <full_path_of_pub_file> grader@35.247.82.229 -p 2200Enter the passphrase for the key
Now you have got access to the linux server instance from your local machine
Check whether your timezone is configured to UTC by running
$ datein your terminal.If the timezone is not set to UTC, run the following command:
$ sudo timedatectl set-timezone UTC
Run the following command:
$ sudo apt-get install apache2 libapache2-mod-wsgi gitEnable mod_wsgi:
$ sudo apache2ctl restartConfigure
git:$ git config --global user.name "<Your-Full-Name>" $ git config --global user.email "<your-email-address>"
Install Python dependencies for Postgresql:
$ sudo apt-get install libpq-dev python-devInstall Postgresql:
$ sudo apt-get install postgresql postgresql-contribCheck if no remote connections are allowed:
$ sudo cat /etc/postgresql/9.5/main/pg_hba.conf
See this link for more details
Login as postgres user (Default user), and get into
psqlshell:$ sudo su - postgres $ psqlCreate a new user named catalog:
# CREATE USER catalog WITH PASSWORD 'catalogpass';Create a new database named catalog:
# CREATE DATABASE catalog WITH OWNER catalog;Connect to the catalog database:
\c catalogRevoke all rights:
# REVOKE ALL ON SCHEMA public FROM public;Grant all permissions to catalog user:
# GRANT ALL ON SCHEMA public TO catalog;Get out of
psqlshell:\qSwitch back to grader user:
exitEdit the
pg_hba.conffile by running$ sudo nano /etc/postgresql/9.5/main/pg_hba.conf, scroll to almost end of the file and add the following line under # Database administrative login by Unix domain socket section:local all catalog passwordRestart PostgreSQL server:
$ sudo service postgresql restart
Run the following commands:
$ sudo apt-get install python-pip $ sudo pip install flask $ sudo pip install httplib2 oauth2client sqlalchemy psycopg2 requests
Make a directory named catalog in /var/www:
$ sudo mkdir /var/www/catalogClone the project Food-Catalog to the catalog directory:
$ sudo git clone https://github.com/jebinphilipose/Food-Catalog.git /var/www/catalogMake a catalog.wsgi file to handle requests with mod_wsgi:
$ cd /var/www/catalog $ sudo nano catalog.wsgiEnter the following contents, save and exit the file:
import sys import logging logging.basicConfig(stream=sys.stderr) sys.path.insert(0, "/var/www/catalog/") from catalog import app as application application.secret_key = 'super_secret_key'Change the engine inside the .py files:
engine = create_engine('postgresql://catalog:catalogpass@localhost/catalog')In
catalog.pyfile:Add the following line in the beginning after the line
app = Flask(__name__):app_path = '/var/www/catalog/'Change all occurences of line
'client_secrets.json'toapp_path + 'client_secrets.json'
Setup the initial data:
$ python database_setup.py $ python populate_database.py
To edit the file run:
$ sudo nano /etc/apache2/sites-available/000-default.confEdit it to match the following contents:
<VirtualHost *:80> ServerName 35.247.82.229 ServerAdmin jebinphilip24@gmail.com WSGIScriptAlias / /var/www/catalog/catalog.wsgi <Directory /var/www/catalog/> Order allow,deny Allow from all </Directory> Alias /static /var/www/catalog/static <Directory /var/www/catalog/static/> Order allow,deny Allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Run the following command:
$ sudo apache2ctl restartAccess the WebApp in your browser: Food Catalog
Install and configure
unattended-upgradesto automatically install updated packages:$ sudo apt-get install unattended-upgrades $ sudo dpkg-reconfigure unattended-upgrades
- Stack Overflow
- GCP Documentation
- DigitalOcean Community