Jump to content

Recommended Posts

Posted (edited)

I'm having trouble debugging the code from a github repository that works on my machine, but I can't make it work on the server. So I would like to make a virtual machine with the same os and applications to be able to debug the code. I'm doing a flask website. What are the applications that Tommy has installed to deal with flask? Which os? How to install wsgi?

Edited by heirloom
Posted

CentOS 7, mod_wsgi-4.6.7. You install mod_wsgi by downloading the source, running ./configure, make, make install. Python 3.7.4. Flask 1.1.1. Let us know if you need more help.

Posted (edited)

I've had some trouble configuring the 'flask' folder. I don't know why I've had to include the location of the python packages in the wsgi file. Or else it said:

Exception occurred processing WSGI script '/var/www/flask/myapp.wsgi'.
Traceback (most recent call last):
    File "/var/www/flask/myapp.wsgi", line 6, in <module>
        from myapp import app as application
    File "/var/www/flask/myapp/__init__.py", line 1, in <module>
        from flask import Flask
ImportError: No module named flask

Also although I've installed python 3.7.4 I had to end up using the default Python 3, which is 3.6.8, because I didn't know how to use pip without installing the default. And I couldn't install mod_wsgi from source because it gave me an error, that was something like: 'apxs command not found'. Probably I forgot some dependency.

 

Here I write all the commands I used to deploy flask apps with apache in centos in a similar way as how the Tommy server does it:

# VirtualBox guest additions

sudo yum install -y epel-release centos-release-scl

sudo yum update -y

sudo yum install -y make gcc kernel-headers kernel-devel perl dkms bzip2

# Devices -> Insert Guest Additions CD image and run it

sudo reboot

sudo yum install -y dconf-editor

bash <(curl -s https://raw.githubusercontent.com/ajr-dev/post-install-script/master/install/settings/gnome.sh)

 

# Install apache

sudo yum install -y httpd httpd-devel python3 python3-pip python3-devel

sudo systemctl start httpd

sudo systemctl enable httpd

 

# Install mod_wsgi 4.6.7

wget -q "https://github.com/GrahamDumpleton/mod_wsgi/archive/4.6.7.tar.gz"

tar -xzf '4.6.7.tar.gz'

rm '4.6.7.tar.gz'

cd 'mod_wsgi-4.6.7'

./configure --with-python=/usr/local/bin/python3

make

sudo make install

 

# Add this to `/etc/httpd/conf/httpd.conf`

# LoadModule wsgi_module modules/mod_wsgi.so

apachectl restart

sudo cat /var/log/httpd/error_log

# If all is okay, you should see a line of the form:

# Apache/2.4.6 (CentOS) mod_wsgi/4.6.7 Python/3.6 configured

 

cd ..

sudo rm -rf 'mod_wsgi-4.6.7'

apachectl restart

# Flask Deploy with Apache on CentOS

sudo pip3 install flask==1.1.1
cd /var/www

sudo mkdir flask

cd flask

sudo mkdir website

 

sudo vi __init__.py

from flask import Flask
app = Flask(__name__)

sudo vi main.py

import sys
import platform

from website import app

def linux_distribution():
    try:
        return platform.linux_distribution()
    except:
        return "N/A"

@app.route("/")
def hello():
    return """
        Hello World!<br><br>
        <a href="/flask/info/">System Information</a><br>
    """

@app.route("/info/")
def info():
    return f"""
        Python version: {sys.version}<br>
        dist: {platform.dist()}<br>
        linux_distribution: {linux_distribution()}<br>
        system: {platform.system()}<br>
        machine: {platform.machine()}<br>
        platform: {platform.platform()}<br>
        uname: {platform.uname()}<br>
    """"

if __name__ == "__main__":
    app.run(debug=True)

sudo vi website.wsgi

import sys
sys.path.insert(0, '/var/www/flask')

from website import main, app as application

cd /etc/httpd/conf/

sudo cp httpd.conf httpd.conf.bak

sudo vi httpd.conf

Insert:

<VirtualHost *:80>
    ServerName localhost
    WSGIScriptAlias /flask /var/www/flask/website.wsgi
    <Directory /var/www/flask>
        Require all granted
    </Directory>
</VirtualHost>

apachectl restart

 

# Get IP

hostname -I

 

# When visiting 'http://my_host_ip/flask' you should see 'Hello World!'

# If you are getting an error you can see it by doing

sudo cat /var/log/httpd/error_log

Edited by heirloom
Posted (edited)

Actually the installed mod_wsgi was using Python 2.7. So I need a way to install the correct version of mod_wsgi.

 

After installing the correct version of mod_wsgi I no longer have to include the location of the packages. mod_wsgi is now using the default Python3 which is 3.6.8. I couldn't get it to run with 3.7.4, even when installing it with --enable-shared it gave 'Error while loading shared libraries' unless I specified the location of the packages in the code like:

import site

site.addsitedir('/usr/local/lib/python3.7/site-packages')
site.addsitedir('/usr/local/lib64/python3.7/site-packages')

Install mod_wsgi 4.6.7 with python 3.7.4 in CentOs 7

Edited by heirloom
Posted (edited)

In the WSGIScriptAlias the alias should be '/' or else all links inside the application will have to include '/flask/path' and changing all the links in the application is not easy unless you've made them relative to a specific url that you can easily change. This has be done for heliohost anyway.

 

This problem has an answer in Path configuration problem with mod_wsgi - WSGIScriptAlias

 

The way in which the Tommy server is configured is probably using the technique explained there.

Edited by heirloom
Posted

With my current configuration apache won't read the .htaccess file in the folder /home/username/public_html/flask. First I have to setup apache to read from that folder. How do I do that?

Posted

By default apache ignores .htaccess files. You have to edit the configuration, find the directory block, and change "AllowOverride None" to "AllowOverride All".

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...