This project is a part of the Full Stack Web Developer Nanodegree through Udacity.
Configuring a Linux server to host Catalog project
IP address: 35.160.113.98
SSH Port: 2200
Username: grader
URL site: http://35.160.113.98/
Name | Description |
---|---|
apache2 | Web Server |
libapache2-mod-wsgi | Tool that serves Python web apps from Apache server |
postgresql | Relational database system |
sqlalchemy | SQL toolkit and ORM for Python |
Flask | Python web framework |
oauth2client | Python library for accessing resources protected by OAuth 2.0 |
httplib2 | A comprehensive HTTP client library |
requests | Python HTTP Requests for Humans |
git | Version control tool |
- Create a new user with name 'grader':
- Create a new user with name 'grader':
$ adduser grader
- Grant user permission to perform sudo command:
$ visudo
- Add the new user: grader below
root ALL=(ALL:ALL) ALL
line and save it::
$ grader ALL=(ALL:ALL) ALL
- Check the new user is present as root
- You can list all the users present as root:
$ cut -d: -f1 /etc/passwd
-
Update all available packages:
$ sudo apt-get update
-
Upgrade packages to newer versions:
$ sudo apt-get upgrade
- Edit the file
/etc/ssh/sshd_config
and change the line ofPort 2
to:Port 2200
- Then restart the SSH service:
$ sudo service ssh restart
- Now you can connect as
grader
from local machine:
$ ssh grader@35.160.113.98 -p 2200
- Block all incoming connections on all ports by default:
$ sudo ufw default deny incoming
- Allow outgoing connection on all ports:
$ sudo ufw default allow outgoing
- Allow incoming connection for SSH on port 2200, HTTP on port 80 and NTP on port 123:
$ sudo ufw allow 2200/tcp
$ sudo ufw allow www
$ sudo ufw allow ntp
- Enable the firewall:
$ sudo ufw enable
- Configure the local timezone to UTC:
$ sudo dpkg-reconfigure tzdata
- Select UTC
- Install Apache2 web server:
$ sudo apt-get install apache2
- Install mod_wsgi and python-setuptools for serving Python apps from Apache:
$ sudo apt-get install python-setuptools libapache2-mod-wsgi
- Restart Apache:
$ sudo service apache2 restart
- Install Git:
$ sudo apt-get install git
- Clone the Catalog app on Github:
$ cd /var/www
$ mkdir FlaskApp
$ cd FlaskApp
$ sudo git clone https://github.com/eakmotion/Catalog.git catalog
- Install python, pip , virtualenv (in /var/www/FlaskApp/catalog):
$ sudo apt-get install libapache2-mod-wsgi python-dev
$ sudo apt-get install python-pip
$ sudo pip install virtualenv
- Enable virtualenv:
$ sudo virtualenv venv
$ source venv/bin/activate
- Install following nessary packages in virtual environment:
$ sudo apt-get install python-setuptools
$ sudo apt-get install python-psycopg2
$ sudo pip install Flask
$ sudo pip install oauth2client
$ sudo pip install requests
$ sudo pip install httplib2
$ sudo pip install sqlalchemy
- Restart apache:
$ sudo service apache2 restart
- Install PostgreSQL database:
$ sudo apt-get install postgresql postgresql-contrib
- Create linux user for psql:
$ sudo adduser catalog
- Change to user postgres:
$ sudo su - postgre
- Connect to Postgres:
$ psql
- Create user and password for your app:
$ CREATE USER catalog WITH PASSWORD 'yourpassword';
- Create a database:
$ CREATE DATABASE catalog;
- Grant only access to the catalog role:
$ \c catalog
$ REVOKE ALL ON SCHEMA public FROM public;
$ GRANT ALL ON SCHEMA public TO catalog;
- Navigate to Catalog directory:
$ cd /var/www/FlaskApp/catalog/
- Edit the catalog.py, database_setup.py and data_seeder.py files by change:
engine = create_engine('sqlite:///catalog.db')
to
engine = create_engine('postgresql://catalog:yourpassword@localhost/catalog')
- Run database script to setup data:
$ python database_setup.py
$ python data_seeder.py
Apache uses the .wsgi file to serve the Flask app
- Navigate to /var/www/FlaskApp directory and create a file named catalog.wsgi:
cd /var/www/FlaskApp
sudo nano catalog.wsgi
- Add the following codes to the catalog.wsgi file:
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/catalog/")
from catalog import app as application
application.secret_key = 'supersecret'
- Configure the virtual host to our domain:
$ sudo nano /etc/apache2/sites-available/FlaskApp.conf
- Add the following codes to the FlaskApp.conf:
<VirtualHost *:80>
ServerName 35.160.113.98
ServerAdmin grader@35.160.113.98
WSGIScriptAlias / /var/www/catalog/catalog.wsgi
<Directory /var/www/catalog/>
Require all granted
</Directory>
Alias /static /var/www/catalog/static
<Directory /var/www/catalog/static/>
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
- Restart Apache with the following command:
$ sudo service apache2 restart
- You may see a message similar to the following:
Could not reliably determine the VPS's fully qualified domain name, using 127.0.0.1 for ServerName
Ignore that message its just a warning - Go to this link: http://35.160.113.98