Installation serveur LAMP sur Ubuntu 18.04.03

Introduction

Cet article décrit l’installation de LAMP (Linux – Apache – MySQL – PHP) sur un serveur Ubuntu 18.04.03. En effet, par défaut , l’installation Ubuntu se fait sans Apache – MySQL – PHP

Installation LAMP

Installation Linux Ubuntu 18.04.03

Pour l’installation d’un serveur Ubuntu, voir article suivant :  Installer serveur Ubuntu 18.04.03

Une fois votre serveur linux installé, connectez vous avec un compte sudo

Installation de MySQL

L’installation de MysSQL se fait en deux étapes : l’installation du serveur puis la configuration des accès root. En premier lieu, il convient de mettre à jour les paquets de votre serveur.

sudo apt-get update

Lancez ensuite l’installation du programme serveur de mysql

sudo apt install mysql-server

Par défaut l’installation n’est pas sécurisée. Il faut exécuter un script qui est installé avec le serveur MySQL et qui va permettre de définir notamment le compte root et de configurer l’accès en répondant à un certain nombre de questions

sudo mysql_secure_installation

La première question concerne l’activation du plugin pour gérer les mots de passe, qu’il faut activer (valeur : y)

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y

Mettre un niveau medium (valeur : 1 )

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1

Entrez ensuite le mot de passe pour root et confirmez le

Please set the password for root here.
New password:

Supprimez les accès anonymes sur la base (valeur : y)

By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y

Supprimez les accès réseau de root (valeur : y)

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y

Supprimez la base de test (valeur : y)

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) :

Rechargez les droits (valeur : y) pour valider l’ensemble des choix

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) :

Il faut ensuite changer le mode d’accès de root pour passer de ‘auth_socket’ à ‘mysql_native_password’. Connectez-vous à mysql

sudo mysql

Vérifiez le mode de connexion de root

mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;
+------------------+-------------------------------------------+-----------------------+-----------+
| user             | authentication_string                     | plugin                | host      |
+------------------+-------------------------------------------+-----------------------+-----------+
| root             |                                           | auth_socket           | localhost |
| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *2F899ABF3C30971834C9A6C2EFCEFFAB851A64E1 | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)

On voit que root est en mode ‘auth_socket’. Pour changer ce mode tapez la commande sql suivante en précisant le mot de passe dans le champ 'monmotdepasse'

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'monmotdepasse';

Validez les modifications apportées

FLUSH PRIVILEGES;

Vérifiez que le compte root se connecte avec le plugin ‘mysql_native_password

mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;
+------------------+-------------------------------------------+-----------------------+-----------+
| user             | authentication_string                     | plugin                | host      |
+------------------+-------------------------------------------+-----------------------+-----------+
| root             | *17DD10419701818E6AA8C5989C8587FF7C8A8C43 | mysql_native_password | localhost |
| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *2F899ABF3C30971834C9A6C2EFCEFFAB851A64E1 | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)

Quittez Mysql

exit

Installation de Apache

Lancez l’installation

sudo apt install apache2

Activer le module userdir et le module rewrite

cd /etc/apache2/mods-available
sudo a2enmod userdir
sudo a2enmod rewrite
sudo systemctl restart apache2

 

Installation PHP

lancer

sudo apt install php libapache2-mod-php php-mysql

Activer

sudo vi /etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c>
    DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>

Mettre

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

Activez PHP pour  le mode userdir (par défaut, le moteur php n’est pas actif sur les comptes utilisateurs, il faut mettre en commentaire dans le fichier php7.2.conf, le engine Off )

cd /etc/apache2/mods-available
sudo vi php7.2.conf
sudo

le fichier

 

<FilesMatch ".+\.ph(ar|p|tml)$">
    SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch ".+\.phps$">
    SetHandler application/x-httpd-php-source
    # Deny access to raw php sources by default
    # To re-enable it's recommended to enable access to the files
    # only in specific virtual host or directory
    Require all denied
</FilesMatch>
# Deny access to files without filename (e.g. '.php')
<FilesMatch "^\.ph(ar|p|ps|tml)$">
    Require all denied
</FilesMatch>

# Running PHP scripts in user directories is disabled by default
#
# To re-enable PHP in user directories comment the following lines
# (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.
# <IfModule mod_userdir.c>
#     <Directory /home/*/public_html>
#         php_admin_flag engine Off
#     </Directory>
# </IfModule>

Pour connaitre la configuration utiliser pour php (apache2 ou cli)

php --ini

Modifiez ensuite la configuration

$ cd /etc/php/7.2/cli/ ou cd /etc/php/7.2/apache2/ 
$ sudo vi php.ini

 

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = africa/tunis

 

restart

sudo systemctl restart apache2