PC Pro

Installing a PHP developmen­t environmen­t

-

When you install it for the first time, the Windows Subsystem for Linux is a bare-bones environmen­t, lacking any of the tools you will need to be productive. Below is a step-by-step guide to installing

a popular stack of components that are used for Linux-based web developmen­t – namely a web server, a database engine and the PHP scripting language.

1INSTALL Apache web server

We’ll start with the Apache web server. As you would expect, this is simple to install using apt: the command is “sudo apt-get install apache2”. Installati­on normally takes less than a minute, but before it starts you may be prompted to enter your password, and to confirm that you’re happy for the installati­on to continue.

2 turn off Windows web server

Once Apache is installed, it will listen for connection­s on port 80 by default. This will cause problems if Windows’ built-in web server is also using that port; one option is to change the port used by Apache, but we’ll fix it by simply turning off the Windows web server. To do this, go back to the “Turn Windows features on or off” panel, untick all the components listed under Internet Informatio­n Services, click OK, reboot, and then restart WSL.

3 launch apache

You can now launch Apache by typing “sudo service apache2 start” and pressing return. If this throws up a warning from the Windows firewall, leave the box ticked to allow access to private networks, but check that access over public networks is disabled. Click “Allow access”, then open a web browser in the Windows environmen­t and point it at “localhost/index.html”: if everything has worked, you should see the Apache default homepage.

4 install mysql

Now it’s time to install MySQL by entering “sudo apt-get install mysql-server”. Once again, confirm that you’re happy for the installati­on to proceed, and wait for the package to download and install. Once that’s complete, type “sudo service mysql start”. To access the MySQL commandlin­e configurat­ion interface, type “sudo mysql -uroot –p”; when prompted, enter the password you specified when setting up WSL.

5 add a new database

MySQL can host multiple databases and four are set up automatica­lly: you can confirm this by entering “show databases;” (not forgetting the semicolon). To add a new database of your own, enter “create database [name];” – replacing [name] with your preferred database name and again retaining the semicolon. Enter “show databases;” and you’ll see that your new database has been added to the list. Enter “quit” to return to the WSL command prompt.

6 install php

The final step is to install PHP. Needless to say, this too is done from the command line: type “sudo apt install php7.2” and press return. Confirm that you’re happy to continue with the installati­on, and when it completes type “sudo apt install hhvm”, press return and, once again, confirm that you’re happy for it to be installed. Finally, install the PHP extension for Apache with “sudo apt-get install libapache-mod-php7.2”.

7 restart apache

To make Apache recognise the new PHP extension, you’ll need to restart it – which is easily done by entering “sudo service apache2 restart”. To check that everything’s working, create a new text file with “sudo nano /var/www/html/ php.php”, and enter the contents shown in the grab above. Press Ctrl+O to save it, then Ctrl+X to quit. Switch to your browser in the Windows environmen­t and point it at localhost/php.php and you should see the PHP status page.

8 integrate mysql with php

MySQL also needs to integrate with PHP, and this is done in a similar manner to the Apache packages – this time “sudo apt-get install php7.2-mysql”. Once the extension is installed, restart Apache one last time with “sudo service apache2 restart”. You now have a fully functionin­g developmen­t environmen­t running natively within the Linux subsystem, which is in turn running natively within Windows itself.

9 use the change mode command

Your developmen­t system is now set up within Linux, but there’s just one more potential gotcha to be aware of. If you’re developing web applicatio­ns, you’ll need to save your work to “/var/www/html/” for it to be seen by Apache – but this may well throw up a security error. If that happens to you, you’ll need to change the permission­s on your /html/ folder using the “chmod” (change mode) command. Within WSL, type the following commands: cd /var/www/ sudo chmod 777 html

Provide your password if required and the problem should be fixed. Note that setting permission­s to 777 enables full read and write access for all users – don’t use this setting for folders on a public-facing server without good reason.

 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??

Newspapers in English

Newspapers from United Kingdom