A blog platform built with Python and the Django framework, designed to share technical blog posts on various topics.
- Create and manage blog posts.
- User-friendly admin interface for content management.
- Easy setup with Django.
- Support Markdown language for blog content
- Category management for posts.
- Create a new database in PostgreSQL.
sudo -u postgres psql
Adapt the config to match your settings
CREATE DATABASE mydb;
CREATE USER myuser WITH PASSWORD 'mypassword';
ALTER ROLE myuser SET client_encoding TO 'utf8';
ALTER ROLE myuser SET default_transaction_isolation TO 'read committed';
ALTER ROLE myuser SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
Expected configuration in the .env file :
# Looking to send emails in production? Check out our Email API/SMTP product!
EMAIL_HOST = '<host>' # e.g 'sandbox.smtp.mailtrap.io'
EMAIL_HOST_USER = '<host_user>>'
EMAIL_HOST_PASSWORD = '<host_password>'
EMAIL_PORT = '2525'
# Setting PostgreSQL connection
DB_NAME = '<db_name>'
DB_USER = '<user>'
DB_PASSWORD = '<password>'
DB_HOST = '<ip>'
DB_PORT = '5432' # default postgres port
- Clone the repository:
git clone git@github.com:maishuji/tech_blog_heho.git
- Activate python environment (optional)
source <path-to-env>/bin/activate
- Install requirements
pip install -r requirements.txt
- Run migrations
Be sure to configure your database settings in settings.py before running migrations
You can check that the connection is working by running:
python3 blog_heho/manage.py migrate
It should open a PostgreSQL shell if everything is configured correctly.python3 blog_heho/manage.py dbshell
- Create superuser
python3 blog_heho/manage.py createsuperuser
- Run server
python3 blog_heho/manage.py runserver
Instead of running the application locally, you can deploy it using Docker and Docker-compose. To do so, follow these steps:
- Install Docker and Docker Compose
- Build the Docker image
docker build -t django_app .
- Run Docker Compose
docker-compose up -d
sudo apt update
sudo apt install docker.io docker-compose
docker-compose --version
sudo usermod -aG docker ubuntu
- Create or import the key pair to the server (public key in ~/.ssh/authorized_keys) from EC2 instance management console.
- Set up the environment variables inside the instance in ~/.profile
: E.g.
www.mywebsite.com
export DJANGO_ALLOWED_HOSTS='<your-public-ip>,<your-domain-name>'
export DJANGO_SECRET_KEY='<your-secret-key>'
export DJANGO_TRUSTED_HOSTS='<your-full-domain-name>' # Should start with https or http
# EMAIL CONFIGURATION
EMAIL_HOST = '<host>' # e.g 'sandbox.smtp.mailtrap.io'
EMAIL_HOST_USER = '<host_user>>'
EMAIL_HOST_PASSWORD = '<host_password>'
EMAIL_PORT = '2525' # default mailtrap port
sudo apt install nginx
sudo apt install gunicorn
sudo nano /etc/nginx/sites-available/tech_blog
sudo apt update
sudo apt install certbot python3-certbot-nginx
sudo certbot certonly --standalone -d www.qcartier.dev
server {
listen 80;
server_name <domain-name>; # E.g www.mynamewebsite.com
# Redirect all HTTP traffic to HTTPS
return 301 https://#host$request_uri;
}
server {
listen 443 ssl;
server_name <domain-name>; # E.g www.mynamewebsite.com
# SSL Configuration
ssl_certificate /etc/letsencrypt/live/<domain-name>/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/<domain-name>/privkey.pem;
# Serve statics in production (through https)
location / {
alias /var/www/static/
}
# Additional SSL settings
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
# HSTS Header for additional security
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";
# Other server settings (e.g., Django app configuration)
location / {
proxy_pass http://localhost:8000; # Adjust this if your app runs on a different port
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
You may need to create a superuser to access the Django admin interface.
For that, check the container docker container ls
, go into the one running the Django app docker exec -it <container_id> bash
and run the command to create the superuser:
python3 blog_heho/manage.py createsuperuser
If for some reasons the deployment failed, remember to check the disk usage
on the target machine. It is possible that the disk is full, which can cause the deployment to fail.
ssh ubuntu@<my_ip>
df -h
A /dev/root
filesystem used at 100% would indicate a full disk. In this case, you need to free up space:
# Clean up unused docker images or containers
docker system prune -a
# Remove old logs or temporary files
sudo rm -rf /var/log/*
sudo rm -rf /tmp/*
If you encounter a 500 error, check the logs for more information. You can find the logs in the Nginx error log file:
sudo tail -f /var/log/nginx/error.log
If this issue occurs only on some pages, it may be related to the database connection or a specific view in your Django application. For example, some pages use a model of field that is not present in the database.
This issue can occur when a change in a model has been recently made, but the database schema has not been updated accordingly. This can happen if you have added a new field to a model or changed the type of an existing field, but you have not run the necessary migrations to apply those changes to the database. In this case, you may need to run migrations again:
python3 blog_heho/manage.py makemigrations
python3 blog_heho/manage.py migrate
If for some reason the migrations do not resolve the issue, and you see that the database is out of sync (e.g a field has not been added or removed), you can to try to manually update the schema.
- It involves going into the db container, and run sql commands.
# Example of adding a column to a table
ALTER TABLE <table_name> ADD COLUMN <column_name> <data_type>;
If you need to check the IP address of the container running the Django app, you can use the following command:
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_name_or_id>
In the nginx configuration (likely etc/nginx/sites-available/<name-of-the-site>
), you can use this IP address to set up the proxy_pass directive:
proxy_pass http://<container_ip>:8000;
Restart Nginx to apply the changes:
sudo systemctl restart nginx
Verify the configuration
sudo systemctl status nginx