Production
Now that the project is up and running locally, the next step is to deploy it to Heroku. If you haven’t yet set up a Heroku account, you can sign up for one here.
After creating your Heroku account, proceed to install the Heroku Command Line Interface (CLI) by following the instructions provided here. The Heroku CLI is an essential tool for managing and deploying your applications on Heroku, offering a seamless integration right from your local environment.
Heroku setup
Once you have the Heroku CLI installed, you can log in to your Heroku account by running the following command in your terminal:
heroku login
Now that you are logged in to your Heroku account, you can create a new Heroku app by running the following command in your terminal:
heroku create interviewdb
And your terminal should display something like this:
Creating ⬢ interviewdb... done
https://interviewdb-abc123456efg.herokuapp.com/ | https://git.heroku.com/<yourproject>.git
And the heroku app will be created with the name interviewdb
and a random URL. You can change the name of the app by running the following command in your terminal:
heroku apps:rename yourPreferredName
At this point, deploying the project to Heroku is not yet feasible because there are several preliminary steps that need to be completed. These steps include setting up the database, configuring Redis for caching, and, as previously mentioned, establishing our environment variables on the Heroku server side (not locally).
PostgreSQL Database on Heroku
- First, we need to set up a PostgreSQL database on Heroku. You can do this by running the following command in your terminal:
heroku addons:create heroku-postgresql:essential-0
This command will initiate the creation of an essential-0 (smallest and cheapest) instance of the PostgreSQL database on Heroku. This instance is a cost-effective option for your project, currently priced at approximately $4 per month. You need to add your credit card information to your Heroku account to use this service.
Heroku discontinued the mini instance of PostgreSQL databases on May 29, 2024. You will need to switch to the essential-0
instance instead.
Setting up this database is essential for persistently storing your project's data, ensuring that your application can manage and access its data efficiently in a secure, scalable environment provided by Heroku. This step is crucial for applications that require a reliable database management system for their operations.
- Next, we can set up a daily backup for our database. You can do this by running the following command in your terminal:
heroku pg:backups schedule --at '02:00 US/Pacific' DATABASE_URL
It will configure a daily backup of your database to occur at 2:00 AM Pacific Time. If this timing doesn't suit your needs, you have the option to adjust it to your preferred time zone.
Redis
heroku addons:create heroku-redis:mini
AI SaaS Template uses Redis as our caching solution to enhance performance by storing frequently accessed data in memory. This command sets up a Redis instance specifically for your project, ensuring that it has a dedicated caching layer to efficiently handle data retrieval and storage. This step is crucial for optimizing the responsiveness and scalability of your application.
Set Other Environment Variables
heroku config:set WEB_CONCURRENCY=3
heroku config:set DJANGO_DEBUG=False
heroku config:set PYTHONHASHSEED=random
heroku config:set DJANGO_SETTINGS_MODULE=config.settings.production
heroku config:set DJANGO_SECRET_KEY="$(openssl rand -base64 64)"
Explanation:
heroku config:set WEB_CONCURRENCY=3
This command sets the WEB_CONCURRENCY environment variable to 3. It determines the number of worker processes running simultaneously to serve requests. Adjusting this value can optimize your application's concurrency, allowing it to handle more requests at the same time without running into performance bottlenecks. The optimal setting depends on your app's specific workload and the resources available on your Heroku dyno.
heroku config:set DJANGO_DEBUG=False
This sets the DJANGO_DEBUG variable to False, turning off Django's debug mode. While debug mode is useful during development for detailed error pages and diagnostics, it should be disabled in production to prevent the exposure of sensitive information and to improve performance.
heroku config:set PYTHONHASHSEED=random
This command ensures that the hash seed for Python's hash-based operations, such as dictionary key ordering, is set to a random value. This is a security measure to prevent certain types of attacks that rely on predictable hash operations. It also helps ensure that your application behaves more consistently with Python's expectations for hash randomness, which can vary between different environments and Python versions.
heroku config:set DJANGO_SETTINGS_MODULE=config.settings.production
Sets the DJANGO_SETTINGS_MODULE variable to point to your production settings module within your Django project. This tells Django to use a specific set of settings tailored for production environments, which might include configurations for database connections, allowed hosts, security settings, and more, distinct from your development settings.
heroku config:set DJANGO_SECRET_KEY="$(openssl rand -base64 64)"
Generates a new, random secret key for Django using OpenSSL and sets it as the DJANGO_SECRET_KEY environment variable. The Django secret key is used for cryptographic signing, and it's crucial that it remains secret and unique to each Django project. This command ensures your Django application on Heroku has a strong, unique secret key, which is essential for security.
Admin URL
You should use something unique for security purpose, DO NOT use the default /admin
path.
Let's say we want to access our admin at https://interviewdb.com/john_wick
heroku config:set DJANGO_ADMIN_URL="john_wick"
Allowed hosts
We will add our customized interviewdb.com domain later, for now, we will add the Heroku app URL to the allowed hosts.
heroku config:set DJANGO_ALLOWED_HOSTS="interviewdb-abc123456efg.herokuapp.com"
If you forget what your heroku app URL is, you can run the following command in your terminal:
heroku apps:info
At this step, if you run
heroku open
You will see an default web page saying Heroku | Welcome to your new app!
, this is because we haven't deployed our project to Heroku yet.
AWS S3
We will use AWS S3 to store our static files and media files for its scalability and reliability.
-
Sign Up for AWS: If you don’t already have an AWS account, sign up for a free account at the AWS website . The AWS Free Tier provides you with a limited amount of resources at no charge for 12 months, which is great for getting started.
-
Create an S3 Bucket: Once you have an AWS account and are signed in to the AWS Management Console, follow these steps to create an S3 bucket for your project:
- Navigate to the Amazon S3 dashboard within the AWS Management Console.
- Click the “Create bucket” button. Each bucket must have a unique name that is not currently in use by any other AWS user globally. Choose a name and a region that is closest to your users for the best performance.
- Follow the prompts to configure your bucket. For a typical web application, you might want to:
- Disable “Block all public access” settings if your static and media files need to be publicly accessible. Make sure to review and understand the implications of making your bucket publicly accessible.
- Configure any additional settings as needed, like logging and tags.
- Turn on “ACLs enabled” to allow Access Control Lists (ACLs) for your bucket and objects.
Once you have an S3 bucket, you can set up the environment variables for your project by running the following command in your terminal:
When configuring your S3 bucket, it's crucial to deselect the "Block all public access" setting and turn on the "ACLs enabled" option.
This adjustment is necessary because the project leverages Access Control Lists (ACLs) to regulate permissions for both the S3 bucket itself and its contained objects.
Additionally, your public static files such including Tailwind CSS styling will be hosted from the S3 bucket.
heroku config:set DJANGO_AWS_ACCESS_KEY_ID="your aws access key"
heroku config:set DJANGO_AWS_SECRET_ACCESS_KEY="your aws secret access key"
heroku config:set DJANGO_AWS_STORAGE_BUCKET_NAME="your aws storage bucket name"
MAILGUN
Mailgunis a third-party service and may require you to enter your credit card information to use their service.
This project uses Mailgun to send emails. If you don't have a Mailgun account, you can sign up for one here. You can find more information about their pricing here.
You'll also need to verify your domain to enable email sending from your domain. Personally, I've found the official documentation from Mailgun to be misleading, with instructions that were not accurate. If you encounter difficulties in setting up Mailgun, I recommend following my detailed guide on how to configure Mailgun properly. You can access the step-by-step instructions here.
Once you have a Mailgun account and verified your domain, you can set up the environment variables for your project by running the following command in your terminal:
heroku config:set MAILGUN_API_KEY="Your Mailgun API Key"
heroku config:set MAILGUN_DOMAIN="interviewdb.com"
Deploy to Heroku
git push heroku main
Following the steps accurately, your project should now be deploying to Heroku. If you encounter any errors, you can check the logs by visiting the Heroku dashboard and clicking on your app.
You can also check the logs by running the following command in your terminal:
After deployment, execute these commands in your terminal to create an admin superuser, so you can access the admin panel on your Heroku app:
heroku run python manage.py createsuperuser
Once completed, access the project's default landing page by visiting your Heroku app's URL, which can be opened in your browser through the terminal command provided.
heroku open