The objective of this project is to perform baseline installation of a Linux distribution on a virtual machine. Hosted a web applications, to include installing updates, securing it from a number of attack vectors and installing/configuring web and database servers.
- IP address: 54.187.202.55
- SSH Port : 2200
- Application URL : http://ec2-54-187-202-55.us-west-2.compute.amazonaws.com/
- Vagrant
- VirtualBox
- Amazon Lightsail instance to host our web application
1. Login as a root user on VM
- Visit Amazon lightsail account to obtain AWS IP address and key. Download Lightsailkey.pem and move it to
~/.sshfolder on your local machine - Open your terminal and type
chmod 400 ~/.ssh/Lightsail-key.pemfollowed byssh -i ~/.ssh/Lightsailkey.pem [email protected]
2. Add new user grader
sudo adduser gradersudo nano /etc/sudoers.d/grader- Add following lines
grader ALL=(ALL:ALL) ALL
3. Update existing installed packages
sudo apt-get updatesudo apt-get upgrade
4. Configuring the key based authentication for user grader
- On your local machine generate public and private key and save it in the
~/.sshfolder usingssh-keygen -f ~/.ssh/udacity-rsakey - On Virtual Machine:
su - gradermkdir .sshtouch .ssh/authorized_keysnano .ssh/authorized_keys - Copy the content of udacity-rsakey.pub previously generated to this file.
- Provide permissions to the user grader:
sudo chmod 700 .sshsudo chmod 644 .ssh/authorized_keyssudo chown -R grader:grader .ssh - Reload SSH using
sudo service ssh restart - At this point we should be able to log into the remote VM using
ssh -i ~/.ssh/udacity-rsakey [email protected]
5. Change SSH port from 22 to 2200
sudo nano /etc/ssh/sshd_config--> Modify port 22 to 2200 and save it.sudo service ssh restart- At this point we should be able to log into the remote VM using
ssh -i ~/.ssh/udacity-rsakey [email protected] -p 2200**Add a custom application and tcp protocol with corresponding port set to 2200 in the networking section in your instance at amazon lightsail.
6. Disabling ssh login for root use
sudo nano /etc/ssh/sshd_config--> Modify PermitRootLogin to no and save itsudo service ssh restart
7. Configuring Uncompatible Firewall (UFW)
- Allow incoming connections for SSH at port 2200, HTTP at port 80 and NTP at port 123.
sudo ufw allow 2200/tcpsudo ufw allow 80/tcpsudo ufw allow 123/tcpsudo ufw enablesudo ufw status
8. Setting the local timezone to UTC
sudo dpkg-reconfigure tzdataand then choose UTC.
9.Install Apache and mod_wsgi
sudo apt-get install apache2sudo apt-get install libapache2-mod-wsgi python-devsudo a2enmod wsgito enable mod_wsgisudo service apache2 start
10. Install git and cloning item_catalog from Github
sudo apt-get install gitcd /var/wwwsudo mkdir catalogsudo chown -R grader:grader catalogto provide createdcatalogownership to grader- cd /catalog
git clone https://github.com/kamireddym28/Item_Catalog_Project.git catalogsudo nano catalog.wsgiand then paste the following in catalog.wsgi: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 = 'supersecretkey'- Rename Catalog_project.py to
__init__.pyusingsudo mv application.py __init__.py.
11. Install Virtual Environment
sudo pip install virtualenvsudo virtualenv venvsource venv/bin/activatesudo chmod -R 777 venv
12. Install Flask and supporting dependencies
sudo apt-get install python-pippip install Flasksudo pip install httplib2 oauth2client sqlalchemy psycopg2 sqlalchemy_utils
13. Updating redirect-uris in client_secrets.json and path in __init__.py
sudo nano __init__.py- Change client_secrets.json path to
/var/www/catalog/catalog/client_secrets.json. - Change fb_client_secrets.json path to
/var/www/catalog/catalog/fb_client_secrets.json. - Update the redirect-uris and javascript-origins in client_secrets.json with this new URL
http://ec2-54-187-202-55.us-west-2.compute.amazonaws.com/.
14. Enable new Virtual host
sudo nano /etc/apache2/sites-available/catalog.conf- Paste the following:
<VirtualHost *:80> ServerName 54.187.202.55 ServerAlias ec2-54-187-202-55.us-west-2.compute.amazonaws.com ServerAdmin [email protected] WSGIDaemonProcess catalog python-path=/var/www/catalog:/var/www/catalog/venv/lib/python2.7/site-packages WSGIProcessGroup catalog WSGIScriptAlias / /var/www/catalog/catalog.wsgi <Directory /var/www/catalog/catalog/> Order allow,deny Allow from all </Directory> Alias /static /var/www/catalog/catalog/static <Directory /var/www/catalog/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> sudo a2ensite catalogto enable virtual host.
15. Install and Configure PostgreSQL*
sudo apt-get install libpq-dev python-devsudo apt-get install postgresql postgresql-contribsudo su - postgrespsqlCREATE USER catalog WITH PASSWORD 'mypassword';ALTER USER catalog CREATEDB;CREATE DATABASE catalog WITH OWNER catalog;\c catalogREVOKE ALL ON SCHEMA public FROM public;GRANT ALL ON SCHEMA public TO catalog;\qexit- Update existing engine path to
engine = create_engine('postgresql://catalog:mypassword@localhost/catalog')in database_setup.py , modelcatalog.py and init.py. sudo python database_setup.pysudo python modelcatalog.pysudo service apache2 restartsudo python __init__.pythen visit http://54.187.202.55
- Udacity course on Configure Web Servers.
- http://swaroopsm.github.io/12-02-2012-Deploying-Python-Flask-on-Apache-using-mod_wsgi.html
- https://www.digitalocean.com/community/tutorials/how-to-secure-postgresql-on-an-ubuntu-vps
- Udacity discussion forum.