Setting up django for production is hard! Using this template will give you a easy deployment that comes out of the box with:
- π³ Container service (Easily scale both horizontally and vertically in AWS lightsail)
- π SSL Certificate on connection
- π¦Ί Safety: if your build fails -> the old container will stay live so you site won't go down
- π Nginx reverse proxy integrated with uwsgi no set up required
- π S3 file storage configured out of the box ready to use in django
- π€ Environment secrets tucked away in your repository secrets (so easy to collaborate)
- π From code commit to deployed in less than 5 minutes
- π€ Serverless deployment for < $7 per month
- Ever time you commit code to branch
main
--> You trigger a deploy to a new lightsail container automatically - This process is fully automated if you provide the correct credentials in your Github Secrets
- You can build anything you like, without the hassle of setting up reverse proxies, docker containers, updating servers, ssl Sertificate
- Just go wild on your app without the hassle of hosting.
- Have Python3 installed
- run
brew install mysql
- in case you have don't have this - run
pip3 install -r requirements.txt
to get all requirements for template - fill in the
core/.env
file with your app's details. I provided a sample in thecore
folder - run
python3 manage.py runserver
to check if the app is working on your local machine
- I assume you have cloned the repo to your github account
- You have a AWS account and can log in to http://lightsail.aws.amazon.com
- Log in to your AWS on http://lightsail.aws.amazon.com
- Create a container service and name it
djangoapp
and pick a region you desire. - Note the region of your service
![Schermafbeelding 2023-10-12 om 23 58 58](https://private-user-images.githubusercontent.com/71013416/274742122-29b491f5-e837-4b93-8f2c-14b0e7e9be5b.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTgyNzc5ODYsIm5iZiI6MTcxODI3NzY4NiwicGF0aCI6Ii83MTAxMzQxNi8yNzQ3NDIxMjItMjliNDkxZjUtZTgzNy00YjkzLThmMmMtMTRiMGU3ZTliZTViLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA2MTMlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNjEzVDExMjEyNlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTgzM2RkNThjZWNhYmVhOWM1YTE0OTk5NWZhZWExMTRmOTgyMDUwZmYwYTYzMjdmMjg4NzA0ZjNmYTAwODVhZDImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.Rgip-V8ItxI-IImv9dZieKiugH883ySuBahdOmn7_Hg)
![Schermafbeelding 2023-10-12 om 23 59 38](https://private-user-images.githubusercontent.com/71013416/274742148-3b7e85df-04ea-49d1-83c5-8ede2810d8d9.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTgyNzc5ODYsIm5iZiI6MTcxODI3NzY4NiwicGF0aCI6Ii83MTAxMzQxNi8yNzQ3NDIxNDgtM2I3ZTg1ZGYtMDRlYS00OWQxLTgzYzUtOGVkZTI4MTBkOGQ5LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA2MTMlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNjEzVDExMjEyNlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWY1MzAxMmZmYTYyMTIzODY3MDk1MWY5OTcyMGFkZmI3OTI2M2M2MjAyZGRiMWZiMjZjYzFjZDhjODUxNmEzMWYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.sezACJwTGkPsskLVmzRre9wafMXp69KX9EJLhW_DVzE)
Generate AWS access credentials with the necessary permissions and store them securely. You can use AWS IAM for this purpose. Plenty of tuturials on how to obtain this. At the end of this step you should have:
AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
In AWS lightsail create a storage bucket and obtain the:
S3_SECRET_KEY
S3_ACCESS_KEY
S3_AWS_STORAGE_BUCKET_NAME
In your GitHub repository, go to Settings
-> Secrets
and add the following secrets:
-
AWS_ACCESS_KEY_ID
: Your AWS access key ID. from step 2 -
AWS_SECRET_ACCESS_KEY
: Your AWS secret access key. from step 2 -
AWS_REGION
: Your AWS region (e.g.,us-east-1
). this must match what you did in step 1 -
S3_AWS_STORAGE_BUCKET_NAME
- for file storage -
S3_ACCESS_KEY
-
S3_SECRET_KEY
-
DB_USER
-
DB_PASSWORD
-
DB_HOST
-
DB_NAME
-
DB_PORT
- for your SQL database. You can also use sqlite in which case you should change your settings.py file in django to do so. But I chose to use a SQL database since data will otherwise be lost if the container reboots.
When you're done it should look like this:
![Schermafbeelding 2023-10-12 om 23 29 52](https://private-user-images.githubusercontent.com/71013416/274736515-fd8cdc56-5516-4884-92db-dc9b1760b2cd.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTgyNzc5ODYsIm5iZiI6MTcxODI3NzY4NiwicGF0aCI6Ii83MTAxMzQxNi8yNzQ3MzY1MTUtZmQ4Y2RjNTYtNTUxNi00ODg0LTkyZGItZGM5YjE3NjBiMmNkLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA2MTMlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNjEzVDExMjEyNlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTgzYmUzMDFmZTlmMzUzMjM5ZWNiZDQ3YWE5NDA4ODRmYTBkZmNkOTMzMDhmMGRhMWZkYWZiZDI2MTYyNmJkZjAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.qJdKPonHw6mMBDBKceWbxP1aMMmAhI3a_2jWhXsEcL4)
- Now you can update your app locally and update it to your hearts desire
- Once you commit to main, github action will trigger
- That looks like this:
![Schermafbeelding 2023-10-12 om 23 30 24](https://private-user-images.githubusercontent.com/71013416/274736630-bf41300f-bc1e-4031-9d4a-28b434a673be.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTgyNzc5ODYsIm5iZiI6MTcxODI3NzY4NiwicGF0aCI6Ii83MTAxMzQxNi8yNzQ3MzY2MzAtYmY0MTMwMGYtYmMxZS00MDMxLTlkNGEtMjhiNDM0YTY3M2JlLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA2MTMlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNjEzVDExMjEyNlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTI0MTFlMWFjNGU0OTk4NTE0OTM2MWM5N2U2OGYyNjk1NzRhOTg2Y2RkM2NjNjE3ZTBkNDdjMzM0MGFlYTQyOGEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.eaDWHTtmniX2Jij-gLgtfDRtIGqB22SrjusyFwK5Jz4)
![Schermafbeelding 2023-10-12 om 23 31 10](https://private-user-images.githubusercontent.com/71013416/274736752-2153f467-e6d6-467c-ad00-21d654149a04.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTgyNzc5ODYsIm5iZiI6MTcxODI3NzY4NiwicGF0aCI6Ii83MTAxMzQxNi8yNzQ3MzY3NTItMjE1M2Y0NjctZTZkNi00NjdjLWFkMDAtMjFkNjU0MTQ5YTA0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA2MTMlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNjEzVDExMjEyNlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTBkNGNhMDNkMWI5MGRlNmQ5ODViMjk0MGQ2NGMzOThjMWUxMzg4MjRkZWEzMGRiNmI2MmE1NDMxYjc3ZTA1MGUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.G559U65mgoPSdIfaMbgL_Yb2gKLM2Uge1VX00t7ttDg)