Installer un serveur Apache en local

← Retour aux Labs

Exemple pour ajouter un site Web local "domaine.local" sous Linux

Étape Action
1. Créer le répertoire du site mkdir ~/site/mondomaine.local
2. Créer un fichier index.html vi ~/site/mondomaine.local/index.html
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <title>Mon site local</title>
</head>
<body>
    <h1>Ça fonctionne !</h1>
</body>
</html>
3. Ajouter le domaine au DNS local sudo vi /etc/hosts

Ajouter la ligne :
127.0.0.1 mondomaine.local
4. Créer le VirtualHost cd /etc/apache2/sites-available
sudo vi mondomaine.conf
<VirtualHost *:80>
    ServerName   mondomaine.local
    ServerAlias  www.mondomaine.local
    ServerAdmin  webmaster@localhost

    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>

    <Directory /var/www/mondomaine.local>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    DocumentRoot /var/www/mondomaine.local

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
5. Activer le VirtualHost sudo a2ensite mondomaine
sudo /etc/init.d/apache2 reload
ou
sudo systemctl reload apache2
6. Créer le lien symbolique sudo ln -s ~/site/mondomaine.local/ /var/www/mondomaine.local
7. Tester Ouvrir le navigateur et aller à :
http://mondomaine.local

Vous devriez voir : "Ça fonctionne !"

Configuration pour PHP

Si vous souhaitez activer PHP sur votre site local :

sudo apt-get install php libapache2-mod-php
sudo systemctl restart apache2

Tester avec un fichier info.php :

<?php
phpinfo();
?>

Permissions

Si vous rencontrez des problèmes de permissions :

sudo chown -R www-data:www-data /var/www/mondomaine.local
sudo chmod -R 755 /var/www/mondomaine.local

Désactiver un site

Pour désactiver un VirtualHost :

sudo a2dissite mondomaine
sudo systemctl reload apache2
← Retour aux Labs