The item catalog project as deployed to a provisioned Linux server is a
two-part project, the first of which involved the provision and setup of
a bare-bones Linux server. Detailed instructions for this part of the
project may be found here.
The instructions below pertain to the second-part of the project - namely,
the deployment of the item catalog web application to the newly provisioned
server. In order for the catalog item project to be deployed successfully, a
number of packages need to be installed on our freshly provisioned Linux
server.
Required installations can be divided up into system/site-wide packages
and project-specific packages. For project-specific packages, we will
install a Python Virtual Environment which will have all the necessary
modules installed for running our particular application.
A). INSTALL APACHE2 SERVER:
sudo apt-get install apache2
1). Installation generally automatically starts apache server.
To test this, type the following command:
ps -ef | grep www
Process names 'www-data' indicate apache daemons are running.
If nothing is returned, use the following command to start the service:
sudo service apache2 start
You can check to see if apache is serving the default web page by
accessing our server and the default http port 80:
http://12.345.67.890:80
If you see the apache welcome page with the words 'It works!' - it's up
and running.
B). INSTALL THE APACHE WSGI MODULE:
sudo apt-get install libapache2-mod-wsgi
a). Generally, the mod_wsgi is enabled after installation by default.
If not, the following command will enable it:
sudo a2enmod wsgi
C). INSTALL POSTGRESQL:
sudo apt-get install postgresql
a). Check that PostgreSQL is up and running:
ps -ef | grep post
this should return several lines with 'postgres'.
1). Configure PostgreSQL:
a). Secure usage of our database: start off by creating a
non-standard root user other than postgres:
Switch to psql root user:
sudo su - postgres
launch application:
psql
create a new user other than 'postgres':
CREATE USER catuser WITH PASSWORD 'dbpass';
grant database creation permission to this user:
ALTER USER catuser CREATEDB;
create our database:
CREATE DATABASE instrumentgarage WITH OWNER catuser;
change to database to test existence:
\c instrumentgarage;
grant rights and permissions to this user:
REVOKE ALL ON SCHEMA public FROM public;
GRANT ALL ON SCHEMA public TO catuser;
finally, logout:
\q
and exit postgres profile:
exit
b). Check the PostgreSQL configuration file to ensure no remote
connections are enabled. This is the default when installing
PostgreSQL in Ubuntu.
sudo vim /etc/postgresql/9.5/main/pg_hba.conf
towards the bottom you should see the actual configurations table.
It's header begins with the following:
TYPE-DATABASE-USER-ADDRESS-METHOD
The last two entries starting with the word 'host' indicate remote
connections. So long as these point to our localhost with the values
similar to the following for column ADDRESS:
127.0.0.1/32 & ::1/128
we have no remote connections enabled.
(If you see other values here, edit them to point to the localhost).
D). INSTALL GIT:
We will need to install git on our server as we will be cloning this repository to it:
sudo apt-get install git
A). INSTALL & CONFIGURE PYTHON VIRTUAL ENVIRONMENT:
1). Install the Python package installer pip:
sudo apt-get install python-pip
2). Install the Python virtual environment installer:
sudo -H pip install virtualenv
3). Make the project directory, navigate to it, and create the virtual
python environment in that location:
cd /var/www
sudo mkdir catalog
change permissions so that user 'grader' owns it:
sudo chown -R grader:grader catalog
now let's create our python virtual environment in the project
directory:
cd /var/www/catalog
sudo virtualenv venv
now launch/activate the virtual environment:
source venv/bin/activate
This will create the virtual environment folder: 'venv'
Allow read/write/execute permissions on this directory:
sudo chmod -R 777 venv
Since we have activated the virtual environment, at this point we should
be executing all commands within the environment. This should be obvious
as our command line should be prefaced with (venv), as in:
(venv) grader@localhost:/var/www/catalog
4). This means that we can now install any/all required packages and
dependencies for the catalog project at hand.
Let's start off installing flask:
pip install Flask
and now for the remaining dependencies which are as follows for this
particular web application:
pip install httplib2, oauth2client, psycopg2, requests, sqlalchemy, sqlalchemy_utils
B). GIT CLONE PROJECT
With our Python virtual environment setup and our dependencies/required
packages installed, we now need to import this project:
git clone https://github.com/builderLabs/psql_catalog.git
1). Modify project configurations to match our new environment.
a).Update the location of the client_secrets file to reflect our
current directory structure
cd /var/www/catalog/psql_catalog
sudo vim catalog.py
search and replace any lines with 'client_secrets' in it to read the
absolute path to our client_secrets.json file (under psql_category) like so:
/var/www/catalog/psql_catalog/client_secrets.json
2). Setup the project:
a). First, we'll need to create our project database:
cd psqlsql_catalog
python create_db.py
b). Now seed the database tables with data in the initData module:
python populate_db.py
We should now have our project database instrumentgarage setup and
populated in PostgreSQL on our server.
C). CONFIGURE A NEW VIRTUAL HOST FOR APACHE TO SERVE
Now we need to set up the requirements for hosting our web application
via Apache server with our newly setup database at the back-end.
1). First, create a *.wsgi file which governs execution of our project scripts.
Our git clone already includes such a script but we need to edit it to ensure
its contents match those of our current environment.
cd /var/www/catalog/psql_catalog
vim controller.wsgi
change the line:
sys.path.insert(0,"/var/www/html/")
to read:
sys.path.insert(0,"/var/www/catalog/psql_catalog/")
and save and quit
:wq
2). Ensure Apache activates virtual environment when running:
Since we are dealing with a virtual environment, these two lines are
required at the top of our wsgi file:
activate_this = '/path/to/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
otherwise apache will default to reading the non-virtual path.
as this is where our project script 'catalog.py' resides.
Note this directory (/var/www/catalog/psql_catalog/) is also where our
controller .wsgi script resides as we'll be needing that in the next step.
The .wsgi tends to be enabled by default but you can run:
sudo a2enmod wsgi
to ensure that it is.
3). Create a new virtual host configuration file.
sudo vim /etc/apache2/sites-available/catalog.conf
paste the following information inside this file:
<VirtualHost *:80>
ServerName 12.345.67.890
ServerAdmin admin@12.345.67.890
WSGIDaemonProcess catalog python-path=/var/www/catalog:/var/www/catalog/venv/lib/python2.7/site-packages
WSGIScriptAlias / /var/www/catalog/psql_catalog/controller.wsgi
<Directory /var/www/catalog/psql_catalog/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/catalog/psql_catalog/static
<Directory /var/www/catalog/psql_catalog/static/>
Order allow,
deny Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog
${APACHE_LOG_DIR}/access.log combined
LogLevel warn
</VirtualHost>
In order for this new virtual host to be effectuated, run:
sudo a2ensite catalog
and then restart the apache service:
sudo service apache2 reload
(or sudo apache2ctl restart
)
The site should now be up and running, we can check that in any browser:
Finally, in order to render the Google sign-in functionality of the
site, we need to add our new server as an authorized host.
To do this, go to the API credentials section for the current application
in the Google console dashboard.
Add our host to the list of authorized origins to avoid the 'origin_mismatch' error:
http://12.345.67.890
The site should now accept Oauth 2.0 via Google sign-in.
With the site now fully deployed and functionality enabled, please refer to the
instructions in the README.md for app-specific guidance.