Installing and configuring Apache, PHP and Mysql in Debian 12


Install Apache using:

$ apt install apache2 apache2-utils

Restart service when you made any changes

$ systemctl reload apache2

enable Apache to start at boot 

$ systemctl enable apache2

start Apache

$ systemctl start apache2

To check apache build and version

$ apache2 -v

 


Install MariaDB

To Install MariaDB use:

$ apt install mariadb-server mariadb-client

Check the status of mariaDB database server

$ systemctl status mariadb

Secure your MariaDB installation by running:

$ mysql_secure_installation


Using the above command, follow the on-screen prompts to set a root password and configure security options.
    By default, MariaDB password is not set. So, leave it empty and press <Enter>.
    And set root password.

    Remove anonymous users.

    Disable remote login for root user.
    Remove test database and access to it.

Enter current password for root (enter for none):

Change the root password? [Y/n] y
<your_sql_pass>

Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y

To log in to the MariaDB shell as root use;

$ mariadb -u root -p

of 

$ mysql -u root -p

To start MariaDB use:

$ systemctl start mariadb 

Enable MariaDB:

$ systemctl enable mariadb

To see MariaDB status use: 

$ systemctl status mariadb 

To exit out of the MariaDB shell, just type in quit and press <Enter>.

 


To prevent the error 'Error in accept: Too many open files' set higher open file limits for MariaDB.

Open the file /etc/security/limits.conf with an editor:

$ pico /etc/security/limits.conf

and add these lines at the end of the file.

mysql soft nofile 65535
mysql hard nofile 65535

Next, create a new directory /etc/systemd/system/mysql.service.d/ with the mkdir command.

$ mkdir -p /etc/systemd/system/mysql.service.d/

and add a new file inside:

$ pico /etc/systemd/system/mysql.service.d/limits.conf

paste the following lines into that file:

[Service]
LimitNOFILE=infinity

Save the file and close the pico editor.

Then reload systemd and restart MariaDB:

$ systemctl daemon-reload
$ systemctl restart mariadb

 

Now check that networking is enabled. Run

$ apt-get install net-tools

then check

$ netstat -tap | grep mysql

 

 

Install PHP and perl and enable CGI module

To install PHP and perl on the server use;

$ apt install php libapache2-mod-php php-mysql
$ apt install perl libapache2-mod-perl2 libcgi-pm-perl

 

Enable CGI Module

$ a2enmod cgi
$ a2enmod cgid 

To check installed version of Perl use command;

$ perl -v




Install Python

To install Python in Debian use;

$ apt install python3 libapache2-mod-python 

To check installed version of Python use command;

$ python3 --version





Install extra PHP modules

$ apt install php-json php-pdo php-curl php-zip php-gd php-cli php-fpm php-mbstring php-pear php-bcmath
$ apt install php-imagick php-tidy php-xml php-xmlrpc


Install APC User Cache

$ apt-get install php8.2-dev
$ apt-get install php8.2-apcu



To get a list of all PHP modules using the combination of apt-cache command and grep command:
$ apt-cache search php | egrep 'module' | grep default


Apache configuration

Edit the following file 

$ pico /etc/apache2/mods-available/mpm_prefork.conf
<IfModule mpm_prefork_module>
 StartServers    4
 MinSpareServers    20
 MaxSpareServers    40
 MaxRequestWorkers   200
 MaxConnectionsPerChild    4500
</IfModule>


Above settings should be adjusted as per your deployment’s needs. And disable the Apache event module and enable httpd prefork, run:

$ a2dismod mpm_event
$ a2enmod mpm_prefork
$ a2enconf php8.2-fpm

 
If not already enabled, enable Apache php module then restart the Web Server

$ a2dismod php8.2
$ systemctl restart apache2

or

$ systemctl restart apache2.service

To check the PHP version use;

$ php -v



Apache2 mods 

a2enmod ssl
a2enmod rewrite
a2enmod suexec
a2enmod include
a2enmod alias
a2enmod authz_groupfile


To increase PageSpeed: Leverage browser caching, need to be fixed in your htaacess

$ a2enmod headers
$ a2enmod expires

 If you want to compress HTML, text, and XML

$ a2enmod deflate

Restart Apache to implement your changes:

$ systemctl restart apache2

 


Configure Apache2 PHP Settings

Now that Apache2 and PHP are installed, you may want to configure Apache2 to use PHP properly.
The default Apache2 PHP configuration file is located at /etc/php/8.2/apache2/php.ini
php 8.2 (depend of installed version, check php version with )

$ php -v

then

$ pico /etc/php/8.2/apache2/php.ini

Then edit the file to suit your environments. Some important lines to consider:

memory_limit = 256M
file_uploads = On
allow_url_fopen = On
post_max_size = 12M
max_execution_time = 60
upload_max_filesize = 120M


then check apache2 dir.conf file

$ pico /etc/apache2/mods-available/dir.conf
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm

 


To allow php script to run in htm and html files add this in the file

$ pico /etc/apache2/mods-available/php8.2.conf
<FilesMatch ".+\.ht(?:m|ml)$">
    SetHandler application/x-httpd-php
</FilesMatch>


Edit apache2.conf file

$ pico /etc/apache2/apache2.conf
<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>

<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>
#<Directory /var/www/>
#       Options Indexes FollowSymLinks
#       AllowOverride None
#       Require all granted
#</Directory>
<Directory /home/sites/*/HTML>
        AddType application/x-httpd-php .html .html
        Options -Indexes
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>
#<Directory /srv/>
#       Options Indexes FollowSymLinks
#       AllowOverride None
#       Require all granted
#</Directory>

 

Edit apache2 security conf file

$ pico /etc/apache2/conf-enabled/security.conf

# change

ServerTokens Prod

ServerSignature Off

ServerSignature Off

TraceEnable Off

Header set X-Content-Type-Options: "nosniff"

Header set Content-Security-Policy "frame-ancestors 'self';"

# close file and restart apache2

$ systemctl restart apache2.service

 

 

Extra apache2 mods

Install imagemagick, dpkg-dev and curl

$ apt install imagemagick
$ apt install dpkg-dev
$ apt install curl


Apache2 important commands;

To stop your web server

$ systemctl stop apache2

To start the web server when it is stopped, type:

$ systemctl restart apache2

If you making configuration changes, Apache can often reload without dropping connections. To do this, use this command:

$ systemctl reload apache2


By default, Apache is configured to start automatically when the server boots. If this is not what you want, disable this behavior by typing:

#$ systemctl disable apache2

To re-enable the service to start up at boot, type

$ systemctl enable apache2