Header

Deploying a Django Project with DeployHQ

This guide assumes you already have a hosted server in place with Django installed. You can find a getting started guide in the official Django documentation https://docs.djangoproject.com/en/5.0/.

DeployHQ for Django

DeployHQ is a service that allows you to automate application deployments on your servers. You can define configurations, custom commands, and trigger deployments automatically on pushes to your version control system.

The free plan allows you to deploy 1 project up to 10 times a day!

Create a Project on DeployHQ

After signing up and logging in, create a new project by navigating to Projects > New Project.

Follow the wizard and choose your version control repository. If you encounter any issues connecting your repository, refer to the DeployHQ support page https://www.deployhq.com/support/projects/creating-a-project.

Create a Deploy User

For enhanced security, it's recommended to create a dedicated user for deployment purposes.

On your server, run the following commands:

sudo adduser deployer
sudo usermod -a -G www-data deployer

Setting Up a Server

Now, we need to inform DeployHQ about the deployment location for your application. Navigate to Servers > New Server.

  • Provide a descriptive name for your server and select SSH as the protocol.
  • Enter your server's IP address in the Hostname field.
  • The Username should be set to deployer.
  • Enable the option Use SSH key rather than password for authentication?.

Next, log in to your server with the deployer user and execute the following commands:

su - deployer
mkdir ~/.ssh
chmod 700 ~/.ssh
nano ~/.ssh/authorized_keys

Paste the public key provided by DeployHQ into the opened file. Save the changes by pressing Ctrl + X, followed by y and Enter.

Set the correct permissions on the authorized_keys file using the following command:

chmod 600 ~/.ssh/authorized_keys
  • In DeployHQ, define the Deployment Path where your application resides (e.g., /var/www/example.com).
  • Enable the option for Perform zero-downtime deployments on this server.

The zero-downtime deployments ensure your application's previous version remains live until the new deployment is complete. You can learn more about this process in the DeployHQ documentation https://www.deployhq.com/features/zero-downtime-deployments.

Enabling this feature means your application files will reside in a path like /var/www/example.com/current. You'll need to update your web server configuration accordingly.

  • Enter production in the Environment field and enable automatic deploy.

Once finished, click Save.

Note: If you encounter any issues connecting to your server, refer to the DeployHQ support page https://www.deployhq.com/support/common-server-errors/connection-errors.

Config Files (Optional)

DeployHQ allows you to upload configuration files during deployment that you don't want stored in your version control system. This is particularly useful for sensitive files like .env.

Navigate to Config Files and click New Config File.

In your project, open the .env.example file and copy its contents into the config file content field on DeployHQ.

Setting Up SSH Commands

We'll configure a few commands to be executed on the server after each deployment.

Go to SSH Commands and click New SSH Command.

Create the following commands:

  • Install Dependencies: cd %path% && pip install -r requirements.txt
  • Collect Static Files: cd %path% && python manage.py collectstatic --noinput
  • Migrate Database: cd %path% && python manage.py migrate

Note: The %path% variable gets replaced with the actual deployment path during execution.

The commands are executed sequentially, so ensure they are listed in the correct order.

Deploying Your Project

With everything configured, it's time to deploy!

Click Deploy Project in the header.

The server you created should be preselected, along with the start and end revisions. Once everything looks good, click Deploy to initiate the deployment process.

Now you can relax and enjoy the convenience of automated deployments!

If you’ve got any other projects you’d like to automate deployments for, we’ve written lots of guides for deploying the most popular application frameworks and content management systems.

Tree

Proudly powered by Katapult. Running on 100% renewable energy.