
Le développement PHP sous Windows présente certains inconvénients. Mais, Microsoft propose désormais une excellente option pour les développeurs PHP qui travaillent sous Windows: le sous-système Windows pour Linux (WSL). WSL est une couche de compatibilité pour exécuter des exécutables binaires Linux (au format ELF) en mode natif sur Windows 10. Microsoft dit:
«C'est avant tout un outil pour les développeurs - en particulier les développeurs Web et ceux qui travaillent sur ou avec des projets open source».Nous pouvons exécuter un environnement Linux directement sur Windows sans la surcharge d'une machine virtuelle.
Remarque: cet article ne s'adresse pas uniquement aux Windows Insiders. Ces méthodes fonctionneront également sur les dernières versions stables de Windows 10.
Dans ce tutoriel, nous allons mettre en place une pile LAMP (Ubuntu 16.04, Apache, PHP 7.1, MariaDB) sur WSL pour le développement. Vous pouvez configurer d'autres piles (par exemple, une pile LEMP) avec des méthodes similaires.
Conditions préalables
Avant de commencer ce guide, vous aurez besoin des éléments suivants:
- Une version 64 bits de Windows 10 avec la mise à jour Creators ou une version ultérieure.
- familiarité avec Linux / bash (si vous souhaitez vous familiariser avec la ligne de commande, vous pouvez lire ce didacticiel DigitalOcean).
Étape 1: installation de bash sur Windows
Tout d'abord, vous aurez besoin de WSL installé sur votre ordinateur.
Vous pouvez installer plus de distributions Linux à partir du Microsoft Store (Ubuntu, openSUSE, SUSE Linux Enterprise Server 12). Mais, dans ce didacticiel, nous allons configurer la pile LAMP sur Ubuntu, vous devez donc sélectionner Ubuntu .
Microsoft a un excellent tutoriel sur la façon d'installer WSL, veuillez suivre les instructions de l'article.
Si vous avez installé avec succès Bash sur Ubuntu sous Windows, installons et configurons une simple pile LAMP pour le développement.
Étape 2: installation d'un serveur HTTP Apache
Nous voulons installer la dernière version stable d'Apache, mais les référentiels officiels Ubuntu ne contiennent pas la dernière version.
Nous devons ajouter un PPA pour les packages Apache. Une archive de packages personnels (PPA) est un référentiel qui permet aux développeurs tiers de créer et de distribuer des packages pour Ubuntu. Le PPA d'Ondřej Surý propose les derniers packages Apache / PHP pour Ubuntu.
Pour ajouter le PPA, exécutez la commande suivante dans le WSL bash:
sudo add-apt-repository ppa:ondrej/apache2
Une fois le PPA configuré, mettez à jour l'index du package local:
sudo apt-get update
Installez Apache:
sudo apt-get install apache2
Créez un dossier de projet pour vos applications Web. Ce dossier doit être en dehors du système de fichiers WSL. Je vous recommande d'utiliser votre dossier Documents.
La commande suivante créera un dossier serveur dans votre répertoire Documents. Veuillez remplacer VOTRE NOM D'UTILISATEUR WINDOWS par votre nom d'utilisateur Windows.
sudo mkdir /mnt/c/Users/YOUR WINDOWS USERNAME/Documents/server
Créez un lien symbolique vers le dossier sélectionné.
sudo ln -s /mnt/c/Users/YOUR WINDOWS USERNAME/Documents/server /var/www/devroot
Ouvrez le fichier de configuration d'hôte virtuel par défaut d'Apache:
sudo nano /etc/apache2/sites-enabled/000-default.conf
Modifiez la racine du document en «/ var / www / devroot», qui pointe vers le dossier de votre projet en dehors du système de fichiers de WSL. Réglez ServerName
sur localhost
(si le port 80 est réservé par une application Windows, remplacez 80 par un port inutilisé):
ServerName localhost ServerAdmin [email protected] DocumentRoot /var/www/devroot Options Indexes FollowSymLinks AllowOverride All Require all granted ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined
Lorsque vous avez terminé, enregistrez le fichier en appuyant sur Ctrl-O et appuyez sur Entrée pour confirmer. Quittez avec Ctrl-X.
Ouvrez votre éditeur / IDE Windows préféré et créez un fichier «index.html» dans le dossier de votre projet (C: \ Users \ VOTRE NOM D'UTILISATEUR WINDOWS \ Documents \ serveur) avec le contenu suivant:
It works!It works!
Démarrez le serveur HTTP Apache:
sudo service apache2 start
Ouvrez // localhost / dans votre navigateur et vous devriez voir le titre «Ça marche».
N'oubliez pas d'activer les modules Apache qui vous sont nécessaires. Par exemple, vous pouvez activer mod_rewrite:
sudo a2enmod rewritesudo service apache2 restart
Étape 3: installation du serveur MariaDB
Ajoutez un dépôt contenant les derniers packages MariaDB:
sudo apt-get install software-properties-common
sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
sudo add-apt-repository 'deb [arch=amd64,i386,ppc64el] //ams2.mirrors.digitalocean.com/mariadb/repo/10.2/ubuntu xenial main'
Installez MariaDB:
sudo apt-get updatesudo apt-get install mariadb-server
Vous serez invité à créer un mot de passe root lors de l'installation. Choisissez un mot de passe sécurisé et souvenez-vous-en, car vous en aurez besoin plus tard.
Démarrez MariaDB:
sudo service mysql start
Exécutez le script suivant (cela modifie certaines des options par défaut les moins sécurisées):
mysql_secure_installation
Étape 4: installation de PHP
Ajoutez PPA pour le dernier PHP:
sudo add-apt-repository ppa:ondrej/phpsudo apt-get update
Installez les packages PHP 7.1:
sudo apt-get install php7.1 libapache2-mod-php7.1 php7.1-mcrypt php7.1-mysql php7.1-mbstring php7.1-gettext php7.1-xml php7.1-json php7.1-curl php7.1-zip
Nous devons redémarrer Apache:
sudo service apache2 restart
Create an info.php file in your project folder with the following content:
Open //localhost/info.php in your browser. If PHP works correctly, you should see the following:

Original text

Step 5: installing phpMyAdmin
phpMyAdmin is a free and open source administration tool for MySQL and MariaDB.
With phpMyAdmin, you can easily create/manage your databases using a web interface.
sudo apt-get install phpmyadmin
- When the first prompt appears, press Space, Tab, and then Enter to select Apache.
- Select yes when asked to use dbconfig-common to set up the database.
- Provide your MariaDB root password
- Choose a password for the phpMyAdmin application itself
Enable the necessary PHP extensions:
sudo phpenmod mcryptsudo phpenmod mbstring
Restart Apache:
sudo service apache2 restart
Now you can access phpMyAdmin on the following URL: //localhost/phpmyadmin/
You can login using the root username and the root password you set up during the MariaDB installation.
Step 6: installing Composer
Composer is a package manager for PHP. It allows you to install/update the libraries your project depends on. If you are a PHP developer you probably use composer.
Visit Composer’s download page and follow the instructions in the Command-line installation section. After Composer has installed successfully, you can install it globally:
sudo mv composer.phar /usr/local/bin/composer
Now it can be run from any location by typing:
composer

Step 7: installing Git:
Git is a version control system which is primarily used for source code management. Learn more about Git here.
You can install it by running the following command:
sudo apt-get install git
Before you use Git (and if you aren’t familiar with it), please read the “How To Set Up Git” section from the How To Install Git on Ubuntu 16.04 tutorial.
Step 8: automatically start LAMP on WSL (optional)
Background tasks are currently not supported on WSL. When you close Bash your services (Apache and MariaDB) will stop.
Note for Windows Insiders: Background tasks are now supported on WSL starting with Windows Insider Build 17046 (for more details, you can read the following blog post: Background Task Support in WSL), but the auto start of services is still not available.
Unfortunately, automatically starting your services is a bit difficult.
Let’s configure autostarting!
We need to start the services without typing your password.
Before you get started with this, please take a look at the following tutorial How To Edit the Sudoers File on Ubuntu and CentOS.
Run the following command:
sudo visudo -f /etc/sudoers.d/services
Copy and paste the following to the editor and then save:
%sudo ALL=(root) NOPASSWD: /usr/sbin/service *%wheel ALL=(root) NOPASSWD: /usr/sbin/service *
This enables us to start the services (like Apache and MariaDB) without using our password.
Start Command Prompt (not the bash) as administrator and run:
SchTasks /Create /SC ONLOGON /TN "Start WSL LAMP" /TR "c:\Windows\System32\bash.exe -c 'sudo service apache2 start; sudo service mysql start; cd ~; bash'"
The above command creates a task that runs automatically when you login to Windows. It does the following:
- Starts Apache
- Starts MariaDB
- Changes the directory to your home directory
Don’t forget: when you close the terminal window, services will stop and you should restart them manually!
Step 9: add test domains (optional)
When you work on more web applications, multiple test domains will be helpful. For example, if you are working on myapp.com, you can access the local development version on //myapp.test/ instead of //localhost/myapp.
In the following, you can replace “myapp” with your web application’s name.
Create a folder in your projects directory for your web application:
sudo mkdir /mnt/c/Users/YOUR WINDOWS USERNAME/Documents/server/myapp
Add the virtual host file to Apache:
sudo nano /etc/apache2/sites-available/myapp.test.conf
Save the following configuration to the new file (don’t forget to replace myapp with your application’s name).
ServerName myapp.test
ServerAdmin [email protected] DocumentRoot /var/www/devroot/myapp
Options Indexes FollowSymLinks AllowOverride All Require all granted
ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined
Enable the new site:
sudo a2ensite myapp.test
Restart Apache:
sudo service apache2 restart
Finally, start Notepad or your favorite editor/IDE on Windows with admin privileges (Run as administrator) and open the hosts file. It is located in the c:\windows\system32\drivers\etc folder.
Add the following line to the end of the file and save it:
127.0.0.1 myapp.test
Now you can access your web application on the //myapp.test/ domain.
You can also add more test domains with the same method.
Conclusion
WSL does not replace Vagrant or Docker, and it is experimental. Automatically starting services is currently not supported on WSL, and this is one of the biggest problems with it at this moment. However, the Windows Subsystem for Linux is a great option for developers to use a native Linux shell on Windows. I think you should give it a try!