Skip to content

Installed and configured all required software to turn a baseline Ubuntu Amazon Web Services server into a fully functional web application server, including Apache Web Server and PostgreSQL database server.

Notifications You must be signed in to change notification settings

adityamehra/udacity-linux-server-configuration

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 

Repository files navigation

udacity-linux-server-configuration

This is the final project for "Full Stack Web Developer Nanodegree" on Udacity.

In this project, a Linux virtual machine needs to be configurated to support the Item Catalog website.

You can visit http://35.164.53.24/ for the website deployed.

You may refer this Udacity course

  1. https://www.udacity.com/course/configuring-linux-web-servers--ud299

Instructions for SSH access to the instance

  1. Download Private Key from the SSH keys section in the Account section on Amazon Lightsail.
  2. Move the private key file into the folder ~/.ssh (where ~ is your environment's home directory). So if you downloaded the file to the Downloads folder, just execute the following command in your terminal. mv ~/Downloads/Lightsail-key.pem ~/.ssh/
  3. Open your terminal and type in chmod 400 ~/.ssh/Lightsail-key.pem
  4. In your terminal, type in ssh -i ~/.ssh/Lightsail-key.pem ubunut@35.164.53.24

Create a new user named grader

  1. sudo adduser grader
  2. sudo vi /etc/sudoers
  3. sudo touch /etc/sudoers.d/grader
  4. sudo vi /etc/sudoers.d/grader, type in grader ALL=(ALL:ALL) NOPASSWD:ALL, save and quit

Set ssh login using keys

  1. Generate keys on local machine usingssh-keygen ; then save the private key in ~/.ssh on local machine

  2. Deploy public key on developement enviroment

    On you virtual machine:

    $ su - grader
    $ mkdir .ssh
    $ touch .ssh/authorized_keys
    $ vi .ssh/authorized_keys
    

    Copy the public key (one with the extension .pub) generated on your local machine to this file and save

    $ chmod 700 .ssh
    $ chmod 644 .ssh/authorized_keys
    
  3. reload SSH using service ssh restart

  4. now you can use ssh to login with the new user you created

    ssh -i ~/.ssh/[privateKeyFilename] grader@35.164.53.24

Update all currently installed packages

sudo apt-get update
sudo apt-get upgrade

Change the SSH port from 22 to 2200

  1. Use sudo vi /etc/ssh/sshd_config and then change Port 22 to Port 2200 , save & quit.
  2. Reload SSH using sudo service ssh restart

Note: Remember to add and save port 2200 with Application as Custom and Protocol as TCP in the Networking section of your instance on Amazon Lightsail.

Configure the Uncomplicated Firewall (UFW)

Configure the Uncomplicated Firewall (UFW) to only allow incoming connections for SSH (port 2200), HTTP (port 80), and NTP (port 123)

sudo ufw allow ssh
sudo ufw allow www
sudo ufw allow ntp
sudo ufw allow 2200/tcp
sudo ufw allow 80/tcp
sudo ufw allow 123/udp
sudo ufw enable 
sudo ufw status

Configure the local timezone to UTC

  1. Configure the time zone sudo dpkg-reconfigure tzdata
  2. It is already set to UTC.

Install and configure Apache to serve a Python mod_wsgi application

  1. Install Apache sudo apt-get install apache2
  2. Install mod_wsgi sudo apt-get install python-setuptools libapache2-mod-wsgi
  3. Restart Apache sudo service apache2 restart

Install and configure PostgreSQL

  1. Install PostgreSQL sudo apt-get install postgresql

  2. Check if no remote connections are allowed sudo vi /etc/postgresql/9.3/main/pg_hba.conf

  3. Login as user "postgres" sudo su - postgres

  4. Get into postgreSQL shell psql

  5. Create a new database named catalog and create a new user named catalog in postgreSQL shell

    postgres=# CREATE DATABASE catalog;
    postgres=# CREATE USER catalog;
    
  6. Set a password for user catalog

    postgres=# ALTER ROLE catalog WITH PASSWORD 'password';
    
  7. Give user "catalog" permission to "catalog" application database

    postgres=# GRANT ALL PRIVILEGES ON DATABASE catalog TO catalog;
    
  8. Quit postgreSQL postgres=# \q

  9. Exit from user "postgres"

    exit
    

Install git, clone and setup your Catalog App project.

  1. Install Git using sudo apt-get install git
  2. Use cd /var/www to move to the /var/www directory
  3. Create the application directory sudo mkdir FlaskApp
  4. Move inside this directory using cd FlaskApp
  5. Clone the Catalog App to the virtual machine git clone https://github.com/adityamehra/catalog2.git
  6. Rename the project's name sudo mv ./catalog2 ./FlaskApp
  7. Move to the inner FlaskApp directory using cd FlaskApp
  8. Rename server.py to __init__.py using sudo mv website.py __init__.py, if __init__.py not present.
  9. Edit database_setup.py and fill_catalog.py to change engine = create_engine('sqlite:///catalog.db') to engine = create_engine('postgresql://catalog:password@localhost/catalog'), if not already done.
  10. Install pip sudo apt-get install python-pip
  11. Use pip to install dependencies -
    • sudo pip install sqlalchemy flask-sqlalchemy psycopg2 bleach requests
    • sudo pip install flask packaging oauth2client redis passlib flask-httpauth
  12. Install psycopg2 sudo apt-get -qqy install postgresql python-psycopg2
  13. Create database schema sudo python database_setup.py
  14. Fill database sudo pip install fill_catalog.py

Configure and Enable a New Virtual Host

  1. Create FlaskApp.conf to edit: sudo vi /etc/apache2/sites-available/FlaskApp.conf

  2. Add the following lines of code to the file to configure the virtual host.

    <VirtualHost *:80>
    	ServerName fill_catalog.py
    	ServerAdmin mehraaditya713@gmail.com
    	WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
    	<Directory /var/www/FlaskApp/FlaskApp/>
    		Order allow,deny
    		Allow from all
    	</Directory>
    	Alias /static /var/www/FlaskApp/FlaskApp/static
    	<Directory /var/www/FlaskApp/FlaskApp/static/>
    		Order allow,deny
    		Allow from all
    	</Directory>
    	ErrorLog ${APACHE_LOG_DIR}/error.log
    	LogLevel warn
    	CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    
  3. Enable the virtual host with the following command: sudo a2ensite FlaskApp

Create the .wsgi File

  1. Create the .wsgi File under /var/www/FlaskApp:

    cd /var/www/FlaskApp
    sudo vi flaskapp.wsgi 
    
  2. Add the following lines of code to the flaskapp.wsgi file:

    #!/usr/bin/python
    import sys
    import logging
    logging.basicConfig(stream=sys.stderr)
    sys.path.insert(0,"/var/www/FlaskApp/")
    
    from FlaskApp import app as application
    application.secret_key = 'super_secret_key'
    

Restart Apache

  1. Restart Apache sudo service apache2 restart

References:

  1. Udacity's FSND Forum
  2. https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps
  3. https://www.digitalocean.com/community/tutorials/how-to-secure-postgresql-on-an-ubuntu-vps

About

Installed and configured all required software to turn a baseline Ubuntu Amazon Web Services server into a fully functional web application server, including Apache Web Server and PostgreSQL database server.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published