Python: Deploy to production

How to deploy your Python code to production in minutes

👋 Welcome to the Stackhero documentation!

Stackhero offers a ready-to-use Python cloud solution that provides a host of benefits, including:

  • Deploy your application in seconds with a simple git push.
  • Use your own domain name and benefit from the automatic configuration of HTTPS certificates for enhanced security.
  • Enjoy peace of mind with automatic backups, one-click updates, and straightforward, transparent, and predictable pricing.
  • Get optimal performance and robust security thanks to a private and dedicated VM.

Save time and simplify your life: it only takes 5 minutes to try Stackhero's Python cloud hosting solution!

Stackhero Python cloud hosting service offers a straightforward and reliable way to deploy your Python projects to production in just minutes. With its intuitive setup and robust features, you can focus on building your application while Stackhero takes care of the infrastructure details.

Some key features include:

  • Deployments with a simple git push
  • Custom domains with automatic TLS certificate management (HTTPS)
  • Dedicated, private VMs for enhanced security and confidentiality
  • Support for HTTP/2, TLS 1.3 (HTTPS), WebSockets, GZIP & Brotli compression, ETag, and TCP/UDP port access

Before you get started with Stackhero, please ensure that Git is installed on your computer.

If your development environment is not set up yet or you want to save time on configuration, you might want to try Stackhero's Code-Hero.

Code-Hero is an easy-to-use, ready-to-code development platform that runs right in your browser, on any computer, tablet, or phone. Based on a VSCode fork, Code-Hero comes pre-installed with essential development tools, so you can start coding right away with no extra setup.

Example of a Code-Hero instance running directly on the browserExample of a Code-Hero instance running directly on the browser

Deploying your Python application to Stackhero is designed to be simple and seamless. Here is how you can do it:

  1. Create a Python service on Stackhero.
  2. Add your SSH public key to your Python service configuration.
  3. Configure the git push command on your development platform.
  4. Add a Makefile to your project.
  5. Deploy your code to production.

If you encounter any issues during deployment, you will likely find answers in the "Troubleshooting" documentation. Checking there first can help you avoid unnecessary frustration.

If you have not already started a Stackhero for Python service, you can create one from your Stackhero dashboard. It usually takes just a couple of minutes.

Once your Python service is running, you will need to add your SSH public key to its configuration. You can display your public key by running:

cat ~/.ssh/id_*.pub

Do you not have SSH keys yet?

You can generate them with:

ssh-keygen -t ed25519

To add your public key to your Python service:

  1. Open the Stackhero dashboard and select your Python service.
  2. Click the "Configure" button.
  3. Paste your SSH public key into the "SSH public keys" section.

Example of a Python service configurationExample of a Python service configuration

Tip: You can also set your SSH public key globally so that any new services you create will automatically include it. To do this, go to your profile in the Stackhero dashboard, click your profile picture in the top right corner, select "Your profile", and add your SSH public key.

Stackhero makes deployments easy by using the Git CLI and the familiar git push command. There is no need to install extra software, just configure your Git remote once, and you are ready to go.

You will find your git remote command on the main page of your service in the Stackhero dashboard. It will look something like this:

git remote add stackhero ssh://stackhero@<XXXXXX>.stackhero-network.com:222/project.git

Python Git remote command on Stackhero's dashboardPython Git remote command on Stackhero's dashboard

To tell Stackhero how to run your application, you will use a Makefile in your project's root directory.

If you are new to Makefile files and the make command, that is okay. Just follow these instructions to get started.

Create a Makefile at the root of your project with the following content:

run:
	gunicorn app:app -b 0.0.0.0:8080 --error-logfile -

There are a few things to keep in mind with this Makefile:

  1. The run rule is required, as Stackhero executes it automatically. Please do not rename it.
  2. The command gunicorn app:app -b 0.0.0.0:8080 --error-logfile - is just an example. Be sure to adjust it to fit your application's requirements.
  3. Make sure to start the gunicorn command with a tab character. If you see an error like "*** missing separator", it means the tab is missing.

In this example, Gunicorn is used to serve your app (make sure it is listed in your requirements.txt). The argument -b 0.0.0.0:8080 means it listens on all interfaces at port 8080. If you choose a different port, you will also need to update your Python service configuration in the Stackhero dashboard. The app:app part refers to the app object inside your app.py file.

Once everything is set up, you are ready to deploy. From your project directory, you can use this command to push your code to Stackhero:

git push stackhero main

The first time you run this command, you will be prompted to confirm the SSH key fingerprint. Just answer "yes" when asked. In a few seconds, your application will be live.

Example of deploying Python code to Stackhero using the git push commandExample of deploying Python code to Stackhero using the git push command

To view your deployed app, go to the "website" URL listed in your Stackhero dashboard (it typically looks like https://<XXXXXX>.stackhero-network.com).

That is it, your application is now live in production.

Are you facing any issues during deployment?

The Troubleshooting section is packed with solutions to help you resolve problems quickly.

We hope this guide has made deploying your Python code to production with Stackhero clear and approachable. With this foundation, you can reliably bring your projects to life.

If you are interested in going further, the "Advanced Deployment Usages" documentation covers topics like deploying from different git branches, tags, or commits, managing multiple environments (such as staging and production), and working with environment variables.

And remember, if you encounter any issues, the "Troubleshooting" documentation is always available to help you get back on track quickly.