Skip to content

Instantly share code, notes, and snippets.

@harshanas
Last active September 5, 2017 08:24
Show Gist options
  • Save harshanas/3e003223e7dd5b8639b8b9ea172252f6 to your computer and use it in GitHub Desktop.
Save harshanas/3e003223e7dd5b8639b8b9ea172252f6 to your computer and use it in GitHub Desktop.
Deploy Flask App to apache in Ubuntu 16.04

Deploy Flask App to apache in Ubuntu 16.04

  1. Make New Directory in var/www/ mkdir bots

  2. Create FILENAME.wsgi file sudo nano bots.wsgi

  3. Add this content and Save File

    #! /usr/bin/python3
    import sys
    import logging
    logging.basicConfig(stream=sys.stderr)
    sys.path.insert(0,"/home/bots/") #APPS DIRECTORY
    
    from botone import app as application # FROM APPNAME IMPORT APP AS APPLICATION
  4. upload Your flask app files to the above defind apps directory cd /home/bots/

  5. Go to the below directory cd /etc/apache2/sites-available

  6. Create a new .conf file sudo nano bots.conf

  7. Add the below content

    <VirtualHost *:80>
                    ServerName harshanas.me
                    ServerAdmin [email protected]
                    WSGIScriptAlias /bots/bottwo /var/www/bots/bots.wsgi
                    <Directory /home/bots/bottwo>
                            Order allow,deny
                            Allow from all
                    </Directory>
                    
                    ErrorLog ${APACHE_LOG_DIR}/error.log
                    LogLevel warn
                    CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    

    add below lines for every app you deploy

    WSGIScriptAlias /bots/botone /var/www/bots/botone.wsgi
                     <Directory /home/bots/botone>
                             Order allow,deny
                             Allow from all
                     </Directory>
    
  8. Create a new .conf file sudo nano bots-ssl.conf

  9. Add the below content

    <VirtualHost *:443>
                ServerName harshanas.me
                ServerAdmin [email protected]
                WSGIScriptAlias /bots/bottwo /var/www/bots/bots.wsgi
                <Directory /home/bots/bottwo/>
                        Order allow,deny
                        Allow from all
                </Directory>
                
                ErrorLog ${APACHE_LOG_DIR}/error.log
                LogLevel warn
                CustomLog ${APACHE_LOG_DIR}/access.log combined
    
                SSLEngine on
    		    SSLCertificateFile /etc/letsencrypt/live/harshanas.me/fullchain.pem
    		    SSLCertificateKeyFile /etc/letsencrypt/live/harshanas.me/privkey.pem
                Include /etc/letsencrypt/options-ssl-apache.conf
    		    
                </VirtualHost>
    

    add below lines for every app you deploy

     WSGIScriptAlias /bots/botone /var/www/bots/botone.wsgi
                <Directory /home/bots/botone>
                        Order allow,deny
                        Allow from all
                </Directory>
    
  10. add the virtual host sudo a2ensite bots.conf sudo a2ensite bots-ssl.conf

    Disable the virtual host sudo a2dissite bots.conf sudo a2dissite bots-ssl.conf

  11. reload apache service apache2 reload

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment