How to set-up environment in CentOS to deploy your django app

Step-by-step guide:

  • Install CentOS (minimal version will work) in your virtual box
  • Install httpd :
    yum install httpd
  • Install httpd-devel
    yum install httpd-devel
    The above command will build and compile several modules into the http server. The one that will be used for this exercise is called apsx (Apache Extension Tool). To see which modules are currently compiled into the http server, you can use the -l command line option. You can also see what modules are loaded dynamically using the -M command line option.
  • Install gcc
    yum install gcc
  • Install mod_wsgi module for Apache httpd server
    pip install mod_wsgi>
  • To verify that the installation was successful, use the following command:
    mod_wsgi-express start-server
    This will start a foreground process running Apache/mod_wsgi server
  • To start the Apache/mod_wsgi server in daemon mode:
    • change the user to root: su root or sudo sh
    • make sure the PATH environment variable of the root is prepended with the correct python dependencies.
      e.g., export PATH=/opt/anaconda/bin:$PATH
    • from the base directory of your django app type in the following command
      python manage.py runmodwsgi --setup-only --port=8001 --user apache --group apache --server-root=/etc/name-of-the-directory-where-you-want-your-web-app-to-be-served
      The above command will start httpd/wsgi server at the localhost, but if you want other people can have access to your website, simply add –host argument to the above command with the ip address of the host server where the application is being deployed:
      python manage.py runmodwsgi --setup-only --host=99.123.234.456 --port=8001 --user apache --group apache --server-root=/etc/name-of-the-directory-where-you-want-your-web-app-to-be-served.
      Before you run the above command, make sure you have either ‘*’ or the host’s ip address listed as one of the allowed hosts in the settings file of your Django app.
    • The above command will create apachectl file in the above server root folder. Run this file with “start” argument to start the Apache/mod_wsgi server. The server can be stopped and restarted with stop and start arguments respectively.
      For example: /etc/mod_wsgi-8001/apachectl start

 

References:

How To Set-Up Elastic Beanstalk Environment In Amazon Web Services To Deploy Django Application

Elastic Beanstalk is one of the various cloud computing services provided by Amazon Web Services (AWS). It provides a platform for fast deployment of most web applications. Each beanstalk deployment consists of an app version and an environment pertaining to the type of application being deployed. Django is a web framework that helps in the development of python applications. A complete django application can be deployed in AWS by following the steps below:

Step-by-step guide

  1. Set-up a virtual environment for your python
    This is an optional step, and is mostly helpful when you are maintaining multiple versions of python with various versions of modules
  2. Install elastic beanstalk command line interface (cli)
    Install elastic beanstalk cli and from within the django project base directory (this is the directory where manage.py is) run:
    en init
    This will create two hidden directories: .elasticbeanstalk and .ebextensions
    Under the directory .ebextensions, create a configuration file with an extension “config”, e.g, 01-test0.config
    Follow the AWS documentation to edit this file. Depending on what your needs are, the content of this file might look different. A typical config file may look like the following:

    packages:
      yum:
        libmemcached-devel: '0.31'
    
    container_commands:
      collectstatic:
        command: "django-admin.py collectstatic --noinput"
      01syncdb:
        command: "django-admin.py syncdb --noinput"
        leader_only: true
      02migrate:
        command: "django-admin.py migrate"
        leader_only: true
      03wsgipass:
        command: 'echo "WSGIPassAuthorization On" >> ../wsgi.conf'
      99customize:
        command: "scripts/customize.sh"
    option_settings: "aws:elasticbeanstalk:application:environment": 
        DJANGO_SETTINGS_MODULE: "djproject.settings" 
        "application_stage": "staging" 
    "aws:elasticbeanstalk:container:python": 
        WSGIPath: djproject/wsgi.py 
        NumProcesses: 3 
        NumThreads: 20 
    "aws:elasticbeanstalk:container:python:staticfiles":
       "/static/": "static/"
    

You can specify any key-value pairs in the aws:elasticbeanstalk:application:environment namespace and it will be passed in as environment variables on your EC2 instances

  1. Create an environment
    From within the base directory of the django project, run:
    eb create
    This will create a beanstalk environment named <your django project name>-dev. For example: if your project name is “test0”, then your beanstalk environment will be named “test0-dev”. The above command creates the default instance type, ‘t2.micro’. In case, you would like to create a specific instance type, use the -i option. For example, eb create -i 'm4.4xlarge' which creates the instance type ‘m4.4xlarge’
  2. Deploy
    From within the base directory of the django project, run:
    eb deploy
    The above command will deploy your django app
  3. Known Issues
    If you get an error that is related to dependencies, ssh into your ec2 instance and manually install all the dependencies that is needed to successfully run your app.
    Accordingly, edit the config file in the directory .ebextensions directory of your django project folder
    After the edit is complete, re-deploy your app by running:
    eb deploy
    In case the static files are not rendering in the browser, take a look at the permissions of the folder from where your app is served in your EC2 instance. You app is served from the folder /opt/python/current/app and your python dependencies are served from the folder /opt/python/run/venv