As an amateur web developer, I needed a blog post that shows how to launch a web site in Amazon Web Service. I made one!
If the commands at the below are followed, a django-powered webpage gets created. After finishing these steps, this app can be a nice starting point for building your web app idea, by adding in your html files and back-end python codes.
-These commands require you have a completely new unused Ubuntu instance at AWS. Follow this https://aws.amazon.com/getting-started/tutorials/launch-a-virtual-machine/
- Choose 'Ubuntu Server 16.04 LTS (HVM), SSD Volume Type - ami-66506c1c' or something like that from Step 1.
- For Step 6: Configure Security Group, click add group and choose HTTP. The proceed to the next. For source field, it should be like 0.0.0.0/0, ::/0
- When you launch ubuntu, you must download and save the pem file for ssh. Also the pem file security should be secure. This means you type in: chmod 400 your.pem
-You have a linux terminal or anything that allows you to do ssh and sftp. My desktop is ubuntu-powered PC as well. You could use psft or putty for windows (https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html). Below are versions of the involved software/server but maybe not that important.
-This assumes you know how to do basic vim commands for editing codes.
--------Ubuntu Version-----------
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.3 LTS
Release: 14.04
Codename: trusty
--------Python Version---------------
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
--------Apache Version -------------
Server version: Apache/2.4.7 (Ubuntu)
Server built: Jan 14 2016 17:45:23
--------Django Version---------------
(1, 7, 0, 'final', 0)
----------------------------------------Start. -----------------------------------------------
Line by line, copy each command and paste into the ssh command prompt, and run it by pressing enter. After running, do the same for the next command. Do not copy and paste lines that start with -----------. These lines are for procedure to perform by you.
Line by line, copy each command and paste into the ssh command prompt, and run it by pressing enter. After running, do the same for the next command. Do not copy and paste lines that start with -----------. These lines are for procedure to perform by you.
------ssh into the server. How to do ssh? For windows, learn putty. For Linux or apple PC, type in SSH command. Example SSH command looks like: ssh -i "test.pem" ubuntu@ec2-34-228-36-21.compute-1.amazonaws.com
-----On the ssh window, run the following commands.
-----On the ssh window, run the following commands.
sudo apt-get update
sudo apt-get install apache2
sudo apt-get install libapache2-mod-wsgi
sudo apt-get install python-setuptools
sudo apt install unzip
sudo apt install unzip
sudo vim ~/.bashrc
------------ Using vim, put in the following three lines of commands by simple copy and pasting: press keyboard key s, copy+paste the following 3 lines of commands, press the 'ESC' key, and type in :wq! and press enter key. We are going to use vim multiple times ---------------
alias re="sudo /etc/init.d/apache2 restart"
alias ee="sudo vim /var/log/apache2/error.log"
alias dc="sudo vim /etc/apache2/sites-available/ django.conf"
-------------- now continue running commands below--------------------
sudo mkdir /var/src
wget https://media.djangoproject.com/releases/1.7/Django-1.7.tar.gz
sudo tar xzvf Django-1.7.tar.gz
cd Django-1.7
sudo python setup.py install
cd /home/ubuntu
django-admin.py startproject website
cd website/website
sudo rm wsgi.py
sudo vim wsgi.py
----------------- Using vim,put in the following lines into the file------------- -------------
import os,sys
apache_configuration= os.path.dirname(__file__)
project = os.path.dirname(apache_ configuration)
workspace = os.path.dirname(project)
sys.path.append(workspace)
sys.path.append(project)
os.environ.setdefault("DJANGO_ SETTINGS_MODULE", "website.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
------------------------exit vim . Run the following lines-----------------
cd /etc/apache2/sites-available/
sudo vim django.conf
-----------------Using vim put in the following----------------------
------------------ec2-54-191-40-188.us-west-2. compute.amazonaws.com has to be changed to your ubuntu server's web address ------------ ----
<VirtualHost *:80>
Alias /static/ /home/ubuntu/website/static/
<Directory /home/ubuntu/website/static/>
Require all granted
</Directory>
WSGIScriptAlias / /home/ubuntu/website/website/ wsgi.py
<Directory "/home/ubuntu/website/website" >
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
------------------exit vim. Run commands below ----------- -----
sudo a2ensite django.conf
sudo /etc/init.d/apache2 restart
cd ~/website/
python manage.py startapp go
sudo rm ~/website/website/urls.py
sudo vim ~/website/website/urls.py
----Using vim put in the following into your urls.py----------------
from django.conf.urls import patterns, include, url
from django.contrib import admin
from go import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$',views.index1),
)+static(settings.STATIC_URL, document_root=settings.STATIC_ ROOT)
----------------exit vim ----------------------
sudo rm ~/website/go/views.py
sudo vim ~/website/go/views.py
--------------------------using vim, PUT ----------------------------
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
import random
import re
from django.http import HttpResponse
def index1(request):
return render_to_response(
'bibindex.html',
)
-------------------exit vim after saving the code----------------------------
sudo vim ~/website/website/settings.py
-----------------Do not cut and paste the following. Just edit accordingly. The variable ALLOWED_HOSTS, and INSTALLED_APPS are already in ~/website/website/settings.py ------------------------------
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'go',
)
---------------Do not close vim yet. Now cut and paste the following two codes into the code-------------------
TEMPLATE_DIRS= ("/home/ubuntu/website/go/ template",'')
STATIC_ROOT = '/home/ubuntu/website/static/'
----------------Now exit vim after saving the code------------------------
mkdir ~/website/go/template/
cd ~/website/go/template/
vim bibindex.html
***********using vim, put ****************
<!DOCTYPE html>
<html>
<body>
<h2>I love yeasts</h2>
<img src="/static/pic_mountain.jpg" alt="Mountain View" style="width:304px;height: 228px;">
</body>
</html>
*************exit vim after saving **************
mkdir /home/ubuntu/website/static/
cd /home/ubuntu/website/static/
mv xl_2116_yeast-tp.jpg pic_mountain.jpg
re
--- Now, using your web browser, visit that address you put into that django.conf file (i.e. ec2-34-228-36-21.compute-1.amazonaws.com )
---------Now if you want to proceed to more complicated website,
cd ~
wget https://images.template.net/wp-content/uploads/2014/12/random-login-form.zip
unzip random-login-form.zip
mv index.html /home/ubuntu/website/go/template/bibindex.html
re
---------To work with your own index.html file:
exit
cd into your folder that contains index.html
sftp into your server
put index.html
exit
ssh into your server
mv index.html /home/ubuntu/website/go/template/bibindex.html
re
then go to your website
----------------------------------------------End------------------------------------------
Now go see that web site with the yeast picture. (Sorry I am a yeast biologist). From here you start developing your own django web app!
For Django tutorial, go to Django 1.7 tutorial on the django website to understand the codes above.
For html tutorial, go to W3 school website. You can totally do jQuary, Javascript and others.