So far, you've been accessing your site locally. Now, it's time to deploy it.
Deployment makes your site publicly accessible by moving it to a production server. Upon deployment, your site becomes accessible worldwide.
In this section of the tutorial, you'll use two platforms to deploy your site. You'll host your site on [fly.io](https://fly.io) and serve your site's images on [Backblaze](https://www.backblaze.com).
You can use fly.io to host your site and serve your images. However, storing your images on a platform other than the one hosting your site provides better performance, security, and reliability.
Now add the following environment variables to your `.env.production` file:
```text
AWS_STORAGE_BUCKET_NAME=
AWS_S3_ENDPOINT_URL=https://
AWS_S3_REGION_NAME=
AWS_S3_ACCESS_KEY_ID=
AWS_S3_SECRET_ACCESS_KEY=
DJANGO_ALLOWED_HOSTS=
DJANGO_CSRF_TRUSTED_ORIGINS=https://
DJANGO_SETTINGS_MODULE=mysite.settings.production
```
### Fill in your Backblaze B2 bucket information
The next step is to provide values for your environment variables. In your `.env.production` file, use your Backblaze B2 bucket information as values for your environment variables as follows:
| AWS_STORAGE_BUCKET_NAME | Use your Backblaze B2 bucket name |
| AWS_S3_ENDPOINT_URL | Use the Backblaze B2 endpoint URL. For example, _https://s3.us-east-005.backblazeb2.com_ |
| AWS_S3_REGION_NAME | Determine your bucket's region from the endpoint URL. For example, if your endpoint URL is _s3.us-east-005.backblazeb2.com_, then your bucket's region is _us-east-005_ |
| AWS_S3_ACCESS_KEY_ID | Leave this empty for now |
| AWS_S3_SECRET_ACCESS_KEY | Leave this empty for now |
| DJANGO_ALLOWED_HOSTS | Leave this empty for now |
| DJANGO_CSRF_TRUSTED_ORIGINS | Use _https://_ |
| DJANGO_SETTINGS_MODULE | Use _mysite.settings.production_ |
The Backblaze B2 storage uses _AWS_ and _S3_ because it works like Amazon Web Services’ S3.
Do not commit or share your `.env.production `file. Anyone with the variables can access your site.
If you lost your secret application key, create a new key following the preceding instructions.
```
For more information on how to set up your Backblaze B2 Cloud Storage, read the [Backblaze B2 Cloud Storage Documentation](https://www.backblaze.com/docs/cloud-storage/).
## Set up Fly.io
Now that you've linked your site to your Blackblaze storage, it's time to set up Fly.io to host your site.
To set up your Fly.io account, follow these steps:
1. Visit [Fly.io](https://fly.io/) in your browser.
2. Click **Sign Up**.
3. Sign up using your GitHub account, Google account, or the email option.
4. Check your email inbox for the verification link to verify your email.
Adding your credit card allows you to create a project in Fly.io. Fly.io won't charge you after adding your credit card.
```
6. [Install flyctl](https://fly.io/docs/hands-on/install-flyctl/) by navigating to your project directory and then running the following command in your terminal:
On macOS:
```sh
# If you have the Homebrew package manager installed, run the following command:
brew install flyctl
# If you don't have the Homebrew package manager installed, run the following command:
curl -L https://fly.io/install.sh | sh
```
On Linux:
```sh
curl -L https://fly.io/install.sh | sh
```
On Windows, navigate to your project directory on **PowerShell**, activate your environment and run the following command:
If you get an error on Windows saying the term `pwsh` is not recognized, install [PowerShell MSI](https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.3#installing-the-msi-package) and then rerun the preceding Windows command.
If you successfully install flyctl but get an error saying "`fly` is not recognized" or "flyctl: command not found error", then you must add flyctl to your PATH. For more information, read [Getting flyctl: command not found error post install](https://community.fly.io/t/getting-flyctl-command-not-found-error-post-install/4954/1).
The `fly launch` command creates two new files, `Dockerfile` and `fly.toml`, in your project directory.
If you use a third-party app terminal like the Visual Studio Code terminal, you may get an error creating your Postgres database. To rectify this error, follow these steps:
1. Delete `fly.toml` file from your project directory.
2. Go to your Fly.io account in your browser and click **Dashboard**.
Add the following to your `.gitignore` file to make Git ignore your environment files:
```
.env*
```
Also, add the following to your `.dockerignore` file to make Docker ignore your environment and media files:
```
.env*
media
```
Configure your Fly.io to use `1` worker. This allows your site to work better with Fly.io's low memory allowance. To do this, modify the last line of your `Dockerfile` as follows:
The preceding dependencies ensure that the necessary tools and libraries are in place to run your site successfully on the production server. The following are the explanations for the dependencies you may be unaware of:
1.`gunicorn` is a web server that runs your site in Docker.
2.`psycopg` is a PostgreSQL adapter that connects your site to a PostgreSQL database.
3.`dj-database-url` is a package that simplifies your database configurations and connects to your site to a PostgreSQL database.
4.`whitenoise` is a Django package that serves static files.
5.`django-storages` is a Django library that handles your file storage and connects to your Backblaze B2 storage.
Replace the content of your `mysite/settings/production.py` file with the following:
1.`DEBUG = False` turns off debugging for the production environment. It's important for security and performance.
2.`SECRET_KEY = os.environ["SECRET_KEY"]` retrieves the project's secret key from your environment variable.
3.`SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")` ensures that Django can detect a secure HTTPS connection if you deploy your site behind a reverse proxy like Heroku.
4.`SECURE_SSL_REDIRECT = True` enforces HTTPS redirect. This ensures that all connections to the site are secure.
5.`ALLOWED_HOSTS = os.getenv("DJANGO_ALLOWED_HOSTS", "*").split(",")` defines the hostnames that can access your site. It retrieves its values from the `DJANGO_ALLOWED_HOSTS` environment variable. If no specific hosts are defined, it defaults to allowing all hosts.
6.`EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"` configures your site to use the console email backend. You can configure this to use a proper email backend for sending emails.
7.`WAGTAIL_REDIRECTS_FILE_STORAGE = "cache"` configures the file storage for Wagtail's redirects. Here, you set it to use cache.
Now, complete the configuration of your environment variables by modifying your `.env.production` file as follows:
Congratulations! Your site is now live. However, you must add content to it. Start by creating an admin user for your live site. Run the following command:
Ensure you replace _username_, _mail@example.com_, and _password_ with a username, email address, and password of your choice.
```
For more information on how to set up your Django project on Fly.io, read [Django on Fly.io](https://fly.io/docs/django/).
## Add content to your live site
All this while, you've been adding content to your site in the local environment. Now that your site is live on a server, you must add content to the live site. To add content to your live site, go to ` https://yourname-wagtail-portfolio.fly.dev/admin/` in your browser and follow the steps in the following sub-sections of the tutorial:
If you encounter errors while trying to access your live site in your browser, check your application logs in your Fly.io Dashboard. To check your application logs, click **Dashboard > Apps > yourname-wagtail-portfolio > Monitoring**