This guide will help you install Apache2, create a Virtual Host for your domain, install Letsencrypt and install PHP 8.2 on Debian 11:
APACHE2
Let’s begin by installing apache2 and creating a virtual host for your website.
Make sure to replace mydomain.com with the appropriate path and domain name for your virtual host.
First, update your package lists by running the command:
sudo apt-get update
Next, install Apache by running the command:
sudo apt-get install apache2
This will install the latest version of Apache available in the package repository.
After the installation is complete, start Apache by running the command:
sudo service apache2 start
You can check the status of Apache by running the command:
sudo service apache2 status
You may also want to enable Apache2 by default with the following command:
sudo systemctl enable apache2
To check if Apache is working correctly, open a web browser and navigate to:
http://localhost/
You should see the default Apache page. If you are in an online server, such as AWS, you can type in: http://your-ip-address
.
To create a virtual host (for your domain), you will need to create a new configuration file in the directory: /etc/apache2/sites-available/
For example:
sudo nano /etc/apache2/sites-available/mydomain.com.conf
In this file, you will need to specify the ServerName, DocumentRoot, and other settings for your virtual host. It should look like this:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/mydomain.com/html
<Directory /var/www/mydomain.com/html>
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
</IfModule>
Make sure your domain on your DNS service (ex. GoDaddy) is pointing to your server. There should be an ‘A’ record with the value @ (or mydomain.com) pointing to your server IP. Also, there should be another ‘A’ record with the value ‘www’ pointing to your ‘@’ (or mydomain.com).
Once the configuration file is created, enable the virtual host by running the command:
sudo a2ensite mydomain.com.conf
Now navigate to /var/www
and run the following command:
mkdir mydomain.com
Create another folder inside mydomain.com called ‘html.’
mkdir mydomain.com/html
Now create a file called ‘index.html’ like this:
nano mydomain.com/html/index.html
Inside the file type:
hello world!
Save the file by pressing CTRL+x and then y.
Now, if you navigate to http://mydomain.com
you should see ‘hello world.’
Also, you need to disable the default Apache2 landing page (default site).
Locate the default site configuration file. It is typically located in the directory /etc/apache2/sites-enabled/ and the file is usually named 000-default.conf or default-ssl.conf
Disable the default site by running the command:
sudo a2dissite 000-default.conf
or sudo a2dissite default-ssl.conf
or both.
Verify that the default site is now disabled by running the command:
sudo apache2ctl -S
It should not list the default site in the output.
Restart Apache to apply the changes by running the command:
sudo systemctl restart apache2
Please keep in mind that the process above is based on the default apache2 configuration. If you made any changes or you are using a different version of apache2 the process could be different.
LETSENCRYPT SSL CERTIFICATE
Here are the step-by-step instructions on how to install a Let’s Encrypt SSL certificate on a virtual host in Apache 2 on Debian 11:
Installing certbot:
Add the certbot repository to your system by running the command:
sudo add-apt-repository ppa:certbot/certbot
Update the package list by running the command:
sudo apt-get update
Install certbot by running the command:
sudo apt-get install certbot
Obtaining a certificate:
Use certbot to obtain a certificate by running the command:
sudo certbot certonly --webroot -w /var/www/yourdomain.com/html -d mydomain.com -d www.mydomain.com
This command will place the certificate files in the /etc/letsencrypt/live/mydomain.com directory
It will also create a new file called:
mydomain.com-le-ssl.conf inside of /etc/apache2/sites-available
Configure your virtual host to redirect from HTTP to HTTPS:
Open the virtual host configuration file for your domain:
sudo nano /etc/apache2/sites-available/mydomain.com.conf
Change the file so it looks like this
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName mydomain.com
ServerAlias www.mydomain.com
Redirect permanent / https://mydomain.com/
</VirtualHost>
Now all traffic going to HTTP should be redirected to HTTPS.
Enable the SSL module by running the command:
sudo a2enmod SSL
Enable the virtual host by running the command:
sudo a2ensite mydomain.com
Configure the automatic renewal of the certificate by running the command:
sudo crontab -e
Choose 1 (nano) and then add the following to the end of the file:
0 0 1 * * certbot renew --quiet --renew-hook "systemctl reload apache2"
Save by pressing CTRL+x and then Y.
This will run the certbot renew command every month on the first day of the month at 00:00. The –quiet flag will prevent certbot from sending an email every time it runs, and the –renew-hook flag will automatically reload the Apache2 service after renewing the certificate.
You can also use certbot renew --dry-run
to test the renewal process before setting up the cronjob to make sure that everything is working correctly.
You may want to restart apache so all changes are saved:
sudo systemctl restart apache2
INSTALLING PHP 8.2
Now let’s install the latest version of PHP 8.2.
Add the PHP 8.2 package repository by running the command
sudo apt-get install lsb-release apt-transport-https ca-certificates
and then
sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
Create the repository list file by running:
sudo echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
Update your package lists by running the command:
sudo apt-get update
Install PHP 8.2 and the necessary Apache PHP libraries by running the command
sudo apt-get install php8.2 libapache2-mod-php8.2
NOTE: Installing just the PHP 8.2 package and the libapache2-mod-php8.2 package may not enough. To install additional PHP libraries, you can use the apt-get install command
followed by the name of the library. Here are some of the most commonly used PHP libraries:
- php-json: JSON extension for PHP
- php-xml: XML extension for PHP
- php-mbstring: Multibyte string extension for PHP
- php-mysql: MySQL extension for PHP
- php-gd: GD extension for PHP
- php-curl: cURL extension for PHP
- php-opcache: OpCache extension for PHP
- php-intl: Internationalization extension for PHP
- php-soap: SOAP extension for PHP
- php-zip: Zip extension for PHP.
After the installation is complete, restart Apache by running the command:
sudo systemctl restart apache2
You can check the version of PHP installed by running the command:
php -v
To configure PHP to work with Apache, you need to edit the /etc/php/8.2/apache2/php.ini
file and adjust the settings to your liking.
Once you have finished configuring PHP, restart Apache again for the changes to take effect by running the command:
sudo systemctl restart apache2
You can install all of these library by running the command:
sudo apt-get install php8.2-json php8.2-xml php8.2-mbstring php8.2-mysql php8.2-gd php8.2-curl php8.2-opcache php8.2-intl php8.2-soap php8.2-zip
Please note that these are just some of the most commonly used libraries, and your specific project may require additional libraries. Also, make sure you restart Apache after installing these libraries so that Apache can pick up the newly installed libraries.
After installing PHP, You can use the command php -v
to check the version of PHP that is currently installed on your system. This will display the version of PHP, the build date, and other information. It should look like this:
$ php -v
PHP 8.2.0 (cli) (built: Jan 14 2022 21:16:03) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.0-dev, Copyright (c) Zend Technologies
You can also create a simple PHP script to display the PHP version and configuration information by creating a new file called phpinfo.php
in your web root directory. Run the following command:
sudo nano /var/www/mydomain.com/html/phpinfo.php
And add the following to the file:
You can access this file through your web browser (http://yourdomain.com/phpinfo.php) to see the PHP version and configuration information. Make sure you delete the file afterward.
Additionally, you can check the installation of specific PHP modules by running the command php -m
this will list all the modules that are currently installed on your system.
Tada! Cheers!
Dan D.