Skip to content

Simple doc explaining how to deploy a Pyramid application to AWS EC2

Notifications You must be signed in to change notification settings

sjschmidt44/deploying_pyramid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 

Repository files navigation

Deploy a Pyramid Application to AWS

In your Pyramid project

  1. Ensure your setup.py reflects any additional dependencies installed via pip for local development

On AWS

You can skip some of these steps if you already have the instances created.

  1. Create a new EC2 instance.

    • Choose the Ubuntu Server 16.04 from the free tier
    • Continue to the the security group settings
    • Use a security group with access to SSH and HTTP from anywhere (we will tighten these access points later)
    • When launching, create a new key pair and download it
    • Launch the instance
  2. Set up a ssh configuration for the EC2 instance.

    • Move the downloaded key pair .pem file to ~/.ssh

    • Change the permissions for the .pem file to remove public access

      $ chmod 400 ~/.ssh/(key name).pem
    • Create a config file in ~/.ssh and add the EC2 instance as a Host.

      # config
      Host (your name for host)
          Hostname (EC2 public DNS)
          User ubuntu
          IdentityFile ~/.ssh/(key name).pem

In your Pyramid project

  1. Connect to your EC2 instance.

    $ ssh (host name)
    • Accept the instance being added to known hosts

On the EC2 instance

  1. Update apt-get.

    $ sudo apt-get update && sudo apt-get upgrade -y
  2. Install necessary packages from apt-get.

    $ sudo apt-get install nginx build-essential python-dev python3-pip -y
  3. Clone Pyramid project repo onto the instance.

    $ cd ~
    $ git clone (link to repo).git src
  4. Install pip requirements for Pyramid project.

    $ cd src
    $ pip3 install -e .

    DO NOT USE SUDO TO PIP INSTALL.

  5. Replace the default nginx configuration file.

    $ sudo nano /etc/nginx/nginx.conf

    Delete the contents of this file and paste the below file:

    # nginx.conf
    user www-data;
    worker_processes 4;
    pid /var/run/nginx.pid;
    
    events {
        worker_connections 1024;
        # multi_accept on;
    }
    
    http {
    
        ##
        # Basic Settings
        ##
    
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;
    
        server_names_hash_bucket_size 128;
        # server_name_in_redirect off;
    
        include /etc/nginx/mime.types;
        default_type application/octet-stream;
    
        ##
        # Logging Settings
        ##
    
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
    
        ##
        # Gzip Settings
        ##
    
        gzip on;
        gzip_disable "msie6";
    
        ##
        # Virtual Host Configs
        ##
    
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
    }
  6. Add a new, project-specific, configuration for your nginx server

    $ sudo nano /etc/nginx/conf.d/(project_name).conf

    Paste the below contents into this file, update with your project details, save and exit:

    # (project_name).conf
    upstream (project_name) {
        server 127.0.0.1:8000;
    }
    
    server {
        listen 80;
    
        server_name (EC2 public DNS);
    
        access_log  /home/ubuntu/.local/nginx.access.log;
    
        location / {
            proxy_set_header        Host $http_host;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header        X-Forwarded-Proto $scheme;
    
            client_max_body_size    10m;
            client_body_buffer_size 128k;
            proxy_connect_timeout   60s;
            proxy_send_timeout      90s;
            proxy_read_timeout      90s;
            proxy_buffering         off;
            proxy_temp_file_write_size 64k;
            proxy_pass http://(project_name);
            proxy_redirect          off;
        }
    }
  7. Validate your updated nginx configs:

    $ sudo nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    $

    RESTART THE EC2 INSTANCE FROM YOUR AWS CONSOLE.

  8. Check that nginx is working properly.

    $ sudo service nginx status
    • If nginx is failing:

      $ sudo service nginx restart

      You can also check the error logs at /var/log/nginx/error.log.

  9. Install gunicorn with pip3.

    $ pip3 install gunicorn
  10. Create a gunicorn configuration file.

    $ sudo nano /etc/systemd/system/gunicorn.service
    # gunicorn.service
    [Unit]
    Description=(your description)
    After=network.target
    
    [Service]
    User=ubuntu
    Group=www-data
    WorkingDirectory=/home/ubuntu/(repo name)/(project name)
    ExecStart=/home/ubuntu/.local/bin/gunicorn --access-logfile - -w 3 --paste production.ini
    
    [Install]
    WantedBy=multi-user.target
  11. Start up gunicorn and check that it is working properly.

    $ sudo systemctl enable gunicorn
    $ sudo systemctl start gunicorn
    $ sudo systemctl status gunicorn

And you're done!

Go check out your site!

About

Simple doc explaining how to deploy a Pyramid application to AWS EC2

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published