Introduction
Hosting multiple websites on a single machine with a single IP address can be a cost-effective and efficient solution for small businesses and individuals. This article will focus on how to host two Flask websites on a single Ubuntu machine running a homeserver using Gunicorn. By the end of this article, you will have a solid understanding of the key concepts and be able to set up your own Flask websites using Gunicorn on a single machine with a single IP address.
Prerequisites
Before we begin, it is assumed that you have a basic understanding of the following:
- Python programming language
- Flask web framework
- Ubuntu operating system
- Command line interface
Setting Up Flask Websites
To set up Flask websites on a single Ubuntu machine, you will need to create two separate Flask applications. For the purpose of this article, we will call these applications app1 and app2.
Creating Flask Applications
To create a Flask application, you will need to create a new Python file and import the Flask module. Here is an example of a basic Flask application:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Hello, World!"
You will need to create two separate Python files for your two Flask applications.
Running Flask Applications
To run your Flask applications, you will need to use the command line interface. Navigate to the directory where your Python files are located and run the following command:
flask run
This will start the Flask development server on the default port 5000.
Setting Up Gunicorn
Gunicorn is a Python WSGI HTTP Server for UNIX, and it is an alternative to the Flask development server. Gunicorn is faster and more robust than the Flask development server, making it a better choice for production environments.
Installing Gunicorn
To install Gunicorn, you will need to use the command line interface and run the following command:
pip install gunicorn
Running Gunicorn
To run Gunicorn, you will need to use the command line interface and run the following command:
gunicorn -b 0.0.0.0:9000 app:app
This will start Gunicorn on port 9000 and bind it to all available network interfaces. You will need to replace app with the name of your Flask application.
Hosting Multiple Websites on a Single IP Address
To host multiple websites on a single IP address, you will need to use a reverse proxy. A reverse proxy is a server that sits in front of one or more application servers and forwards client requests to the appropriate server. In this case, we will use Nginx as our reverse proxy.
Installing Nginx
To install Nginx, you will need to use the command line interface and run the following command:
sudo apt-get install nginx
Configuring Nginx
To configure Nginx, you will need to edit the default configuration file. The default configuration file is located at /etc/nginx/sites-available/default.
You will need to add the following configuration to the file:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:9000;
}
}
server {
listen 80;
server_name example2.com;
location / {
proxy_pass http://127.0.0.1:9001;
}
}
You will need to replace example.com and example2.com with the domain names of your websites. You will also need to replace 9000 and 9001 with the ports that your Flask applications are running on.
In this article, we have covered the key concepts of hosting multiple Flask websites on a single Ubuntu machine running a homeserver using Gunicorn. We have also covered how to use Nginx as a reverse proxy to host multiple websites on a single IP address. By following the steps outlined in this article, you should be able to set up your own Flask websites using Gunicorn on a single machine with a single IP address.
References
Types of references included:
- Online resources