Mustafa Can Yücel
blog-post-1

Setting Up Your Debian Server 2: Git, Apache

For some reason, git was not included in the available packages, so we need to add it manually. We install it by apt (do not forget to check apt updates beforehand):

apt install git
To access your git account securely, you need to set up ssh access. In this case, we are going to create a new ssh key pair. If you already have one, you can skip this step. To create a new ssh key pair, you can use the following command:
ssh-keygen -t ed25519 -C "your_github_email@example.com"
This creates a new SSH key, using the provided email as a label. When you are prompted to "Enter a file in which to save the key," you can press Enter. This accepts the default file location. Then you will be prompted to enter a secure passphrase. This is optional, but if you choose to use a passphrase, make sure to remember it or write it down somewhere. You will need it later. You can add your new SSH key to the ssh-agent by using the following command so that you do not need to enter your passphrase every time you use your SSH key:
eval "$(ssh-agent -s)"ssh-add ~/.ssh/id_ed25519
Then you need to add your public key to your GitHub account. You can copy your public key to your clipboard by using the following command:
clip < ~/.ssh/id_ed25519.pub
Then you can go to your GitHub account settings, click on SSH and GPG keys, and click on New SSH key. You can paste your public key into the "Key" field and click Add SSH key.

Note From the Future

I strongly advise you to use Caddy instead of Apache. It is much easier to set up and use. For setting up Caddy, you can skip to Switching to Caddy Server post. If you still want to use Apache, you can continue reading this post.

Apache

Now we can install apache2. We can install it by apt:

sudo apt update
sudo apt install apache2
We can check if it is installed correctly by the version or system service status:
sudo apache2 -version
sudo systemctl status apache2
If you haven't done already, you need to allow the HTTP and HTTPS ports:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
Now you can check if Apache is working by going to your server's IP address. You should see the default Apache page. If you want to change the default page, you can edit the index.html file in /var/www/html. You can also add your HTML files to this folder.

The common way to host more than one site in a server (with a single ip) is to use virtual hosts. We can create a new virtual host by creating a new configuration file in /etc/apache2/sites-available. To serve a new page "testdomain.info" in our server, we can copy the default configuration file and edit it:

sudo vi /etc/apache2/sites-available/testdomain.info.conf
For this example, we do not use HTTPS, our site files are in /var/www/testdomain.info/ directory, and we will use custom apache logs:
<VirtualHost *:80>
    ServerAdmin admin@testdomain.info
    ServerName testdomain.info
    ServerAlias www.testdomain.info
    DocumentRoot /var/www/testdomain.info/html
    ErrorLog ${APACHE_LOG_DIR}/custom_error.log
    CustomLog ${APACHE_LOG_DIR}/custom_access.log combined
</VirtualHost>
The configuration files should end with .conf. The ServerName and ServerAlias should be the domain name you want to use. The DocumentRoot should be the directory where your site files are located. The websites under sites-available are not active by default. To activate them, we need to create a symbolic link to sites-enabled. The a2ensite command does this for us:
sudo a2ensite testdomain.info.conf
We can also disable the default page by using a2dissite command:
sudo a2dissite 000-default.conf
Then we need to restart Apache:
sudo systemctl restart apache2
We can test the configuration by using apache2ctl command:
sudo apache2ctl configtest
If you get an error/warning as
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message :
Then we need to edit the apache2 configuration file:
sudo vi /etc/apache2/conf-available/servername.conf
and add the following line:
ServerName testdomain.info
Then we need to enable this configuration, restart Apache, and test the configuration again:
sudo a2enconf servername.conf
systemctl reload apache2
sudo apache2ctl configtest

In the next part, we will use Let's Encrypt to get a free SSL certificate for our site, update our site to use HTTPS, and redirect HTTP requests to HTTPS.