To deploy a Django application on Render using PostgreSQL as the database and hosting images over AWS S3, follow these steps:
1. Prepare Django Application for Deployment
-
Install required dependencies: Make sure your Django application includes the necessary dependencies in
requirements.txt
:
psycopg2-binary boto3 # for AWS S3 storage dj-database-url # for parsing the database URL gunicorn # for running the Django application in production django-storages # for S3 storage support
-
Configure
settings.py
:
Update your settings to connect to PostgreSQL and AWS S3:
import os import dj_database_url # Render database configuration DATABASES = { 'default': dj_database_url.config( default=os.getenv('DATABASE_URL') ) } # AWS S3 configuration for media storage AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID') AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY') AWS_STORAGE_BUCKET_NAME = os.getenv('AWS_STORAGE_BUCKET_NAME') AWS_S3_REGION_NAME = os.getenv('AWS_S3_REGION_NAME', 'us-east-1') AWS_DEFAULT_ACL = None AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com' AWS_S3_OBJECT_PARAMETERS = { 'CacheControl': 'max-age=86400', } # Static and Media files on S3 STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage' DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
2. Set Up PostgreSQL Database on Render
-
Create a PostgreSQL database:
- Log in to Render.
- Navigate to Databases and click on New PostgreSQL Database.
- Set a name for your database and choose a region.
- Once created, you will get a
DATABASE_URL
. Store this as an environment variable in your Render service under Environment with the nameDATABASE_URL
.
3. Set Up AWS S3 for Image Hosting
-
Create an S3 Bucket:
- Log in to the AWS Management Console.
- Go to S3 and create a new bucket for storing images.
- In Permissions, make sure to set public access appropriately for media files.
-
Set up IAM user for S3 access:
- Go to the IAM service in AWS.
- Create a new user with programmatic access and attach the AmazonS3FullAccess policy.
- Copy the
AWS_ACCESS_KEY_ID
andAWS_SECRET_ACCESS_KEY
.
-
Configure Django with S3 Credentials:
- In Render, navigate to your service settings and add the following environment variables:
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_STORAGE_BUCKET_NAME
AWS_S3_REGION_NAME
- In Render, navigate to your service settings and add the following environment variables:
4. Deploy to Render
-
Create a new Web Service:
- In Render, go to Dashboard > New > Web Service.
- Connect your GitHub repository containing your Django project.
- Select a name for your service and pick a region.
-
For the Build Command, use:
pip install -r requirements.txt python manage.py collectstatic --noinput python manage.py migrate
5. For the **Start Command**, use:
```bash
gunicorn your_project_name.wsgi:application
```
5. Run Migrations and Collect Static Files
Once the deployment is complete, Render will run the migration command and collect static files. If everything is set up correctly, your application should be live with PostgreSQL as the database and AWS S3 hosting your media files.
Let me know if you need any additional details or further clarification!
Top comments (0)