How to set up a personal blog

Subscribe Send me a message home page


Introdcution

In order to set up a personal blog, we need the following items:

We can buy a domain at a domain provider such as bluehost. For exmaple, the domain name of this blog compassinbabel.org is bought at bluehost. There are many cloud service providers that provide web hosting service. For example, this blog is currently hosted in digitalocean. In digitalocean, a host is also called a droplet so in this post host and droplet are interchangealbe.

In general, there are two main steps when we set up a personal blog. They will be discussed in the below sections.

Step 1: Config DNS

The objective of configuring DNS is to connect the domain name to the host(droplet). As the below diagram illustrates, there are two parts of the configuration. First, we need to let the domain provider know the name server provided by the hosting service, which knows where to find the host. This part is configured in the domain provider account. Second, we need to let the hosting service know the host wants to handle requests associated with the domain name. This configuration is the ultimate domain-to-host mapping we want to establish (solid orange line in the diagram).

dns_config.png

It takes up to 72 hours for the DNS configuration update to propagate through the internet. This progress can be tracked in websites such as https://www.whatsmydns.net/.

To verify that the DNS configuration is updated, we can run whois <domain-name> on the local machine. For example, if we run whois compassinbabel.org, we get the folloing outputs

Registrant Country: US
Name Server: NS1.DIGITALOCEAN.COM
Name Server: NS2.DIGITALOCEAN.COM
Name Server: NS3.DIGITALOCEAN.COM
Troubleshooting

It can happen that nslookup works but if we ping the domain name we get the unknown host error. To solve this issue, we need to refresh the local DNS cache. For example, on Mac, we can run the following command to refresn the DNS cache

sudo killall -HUP mDNSResponder

Step 2: Deploy Flask Application

Here is a tutorial about the flask deployment. Most of the instruction are still valid.

Two components are important in the whole setup:

  1. Apache server
  2. Flask application

Both of them need appropriate access permision. This is important becuase when we log into the droplet account we are root usere and the files created require root permission by defaut. We need to grant permission to files and folders needed by apache server and Flask application.

The most important part in the deployment process is the apache server configuration. We need to connect Flask application to the Apache framework. By Flask application, we mean the main python script that is used to start the Flask application instance. This script usually contains the following line:

app = Flask(__name__)

On the Apache configuration side, we can registere a web application in /etc/apache2/sites-available/. It's a good practice to make the name consistent, therefore, for this blog, we have the following configuration file:

/etc/apache2/sites-available/compassinbabel.conf

Here is te content of the config file:

<VirtualHost *:80>
	ServerName compassinbabel.org
	ServerAdmin admin@compassinbabel.org
	WSGIScriptAlias / <path-of-the-main-flask-script>
	WSGIProcessGroup compassinbabel
	WSGIDaemonProcess compassinbabel processes=2 threads=12 python-path=<path-to-flask-project-root>:/usr/bin/python3

	<Directory /var/www/>
	    Options Indexes FollowSymLinks
	    AllowOverride All
	    Require all granted
	</Directory>

	<Directory <path-to-flask-project-root>>
		Require all granted
	</Directory>
	Alias /static <path-to-flask-project-root>/static
	<Directory <path-to-flask-project-root>/static/>
	Order allow,deny
	Allow from all
	</Directory>
	ErrorLog ${APACHE_LOG_DIR}/error.log
	LogLevel warn
	CustomLog ${APACHE_LOG_DIR}/access.log combined


	<Directory <path-to-flask-project-root>>
	    WSGIProcessGroup compassinbabel
	    WSGIApplicationGroup %{GLOBAL}
	    Require all granted
	</Directory>
</VirtualHost>

Note on /var/www/: If we want to host multiple websites on the same server, we could have the following configuration

<Directory /var/www/<your-website-name>/html>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

And we need to have an index.html file in the folder /var/www/<your-website-name>/html to make it work. Remember to execute command a2ensite /etc/apache2/sites-available/<config file> as well.

As we can see here, this configuration file makes the flask application known to the Apache server. We can also test if the syntax in the configuration is corret by using the following command:

sudo apache2ctl configtest

The final step is to restart the apache with the following command:

sudo service apache2 restart

By default, logs are saves in

/var/log/apache2/error.log
/var/log/apache2/access.log

The log folder can be configured in the web application configuration file (for example /etc/apache2/sites-available/compassinbabel.conf) and it depends on the environment variable APACHE_LOG_DIR, which is defined in:

/etc/apache2/envvars/APACHE_LOG_DIR

----- END -----

Send me a message Subscribe to blog updates

Want some fun stuff?

/static/shopping_demo.png


Comments