How to automate a deploy in Apache?

5

I have some Django-based systems that often need to be deployed on different servers. This involves, among other things, installing all dependencies, downloading project files, placing them in a standardized folder, etc., all of which can be done through a simple script.

However, a part of this process I've always done manually, since I do not know a good way to automate / semi-automate:

  • Do the basic configuration of Apache (if it is the first installation of it too);
  • Create a virtual host for my application; in general it is just an archive with more or less this format:

    <VirtualHost X.X.X.X:443>
        SSLEngine On
        ServerName <<nome do servidor>>
        ServerAlias <<nome alternativo>>
        ServerAdmin <<e-mail do administrador>>
    
        DocumentRoot /opt/<<pasta da aplicação>>
        Alias /media/ /opt/<<pasta da aplicação>>/media/
        Alias /static/ /opt/<<pasta da aplicação>>/static/
    
        WSGIDaemonProcess <<processo wsgi>> user=<<usuario>> group=<<grupo>> threads=1
        WSGIProcessGroup <<grupo wsgi>>
        WSGIScriptAlias / /opt/<<pasta da aplicação>>/aplicacao.wsgi
    </VirtualHost>
    

    to be placed in sites-available with link in sites-enabled . This is the easiest: in the last case, I can generate the file given the desired parameters. But if you have a simpler way, the better.

  • Are there tools - from Apache itself or from third parties (preferably open-source ) - to simplify this configuration, without requiring you to edit httpd.conf and its various referenced files? Or, if the answer is no, what is the best way to programmatically edit these files? Unlike popular formats like XML, JSON and YAML, I do not know any reader / writer for their format.

    P.S. I'm assuming a Linux / Unix environment, and the programming language does not matter (but if there is a solution in Python, so much the better).

        
    asked by anonymous 12.11.2014 / 21:34

    1 answer

    1

    You can configure the entire server using Fabric .

      

    Fabric is a Python (2.5-2.7) library and command-line tool for   streamlining the use of SSH for application deployment or systems   administration tasks.

    With it you can create a file (fabfile.py) within your Django project, and in this file put the commands to connect to the server via SSH and perform all the required settings.

    In this case of Apache, it would look something like:

    # -*- encoding:utf-8 -*-
    from fabric.api import *
    from fabric.colors import green, red, yellow
    
    env.host_string = 'ip do seu servidor'
    env.user = 'ubuntu'
    env.key_filename = ''
    
    def configurar_servidor():
        ...
        print(green(u'Instalando apache...'))
        ...
        print(green(u'Configurando...'))
        run('sudo cp /home/ubuntu/seuprojeto/config/apache/arquivo diretoriodoapache/sites-available/arquivo')
        ...
    

    I just put an idea, after installing Fabric in the virtual environment of your project, locally you would enter with the command to execute the tasks of that file. With it you can configure everything from scratch, from creating instances to Amazon, to downloading your git project, installing virtualenv, database and etc.

        
    19.11.2014 / 02:07