Kako postaviti PHP razvojno okruženje na Windows podsustav za Linux (WSL)

Razvoj PHP-a na Windowsima ima nekih nedostataka. No, Microsoft sada nudi izvrsnu opciju za PHP programere koji rade na sustavu Windows: Windows podsustav za Linux (WSL). WSL je sloj kompatibilnosti za pokretanje binarnih izvršnih datoteka Linuxa (u ELF formatu) izvorno u sustavu Windows 10. Microsoft kaže:

"Ovo je prvenstveno alat za programere - posebno za web programere i one koji rade na projektima otvorenog koda ili s njima".

Linux okruženje možemo pokretati izravno na sustavu Windows bez dodatnih troškova virtualnog stroja.

Napomena: Ovaj članak nije samo za Windows Insiders. Te će metode raditi i na najnovijim stabilnim izdanjima Windows 10.

U ovom uputstvu postavit ćemo LAMP stog (Ubuntu 16.04, Apache, PHP 7.1, MariaDB) na WSL za razvoj. Možete postaviti druge snopove (na primjer, LEMP snop) sličnim metodama.

Preduvjeti

Prije nego što započnete s ovim vodičem, trebat će vam sljedeće:

  • 64-bitna verzija sustava Windows 10 s Creators Update ili novijom verzijom.
  • poznavanje Linuxa / basha (ako se želite upoznati s naredbenim retkom, možete pročitati ovaj tutorial DigitalOcean).

Korak 1: instalacija basha na Windows

Prvo će vam trebati WSL instaliran na računalu.

Možete instalirati više distribucija Linuxa iz Microsoftove trgovine (Ubuntu, openSUSE, SUSE Linux Enterprise Server 12). Ali, u ovom uputstvu postavit ćemo LAMP stog na Ubuntu, pa morate odabrati Ubuntu .

Microsoft ima sjajnu lekciju o tome kako instalirati WSL, slijedite upute u članku.

Ako ste uspješno instalirali Bash na Ubuntu u sustavu Windows, instalirajmo i konfigurirajmo jednostavni LAMP stog za razvoj.

Korak 2: instaliranje Apache HTTP poslužitelja

Želimo instalirati najnoviju stabilnu verziju Apachea, ali službena Ubuntu spremišta ne sadrže najnovije izdanje.

Moramo dodati PPA za Apache pakete. Arhiva osobnih paketa (PPA) spremište je koje omogućuje nezavisnim programerima izgradnju i distribuciju paketa za Ubuntu. PPA Ondřeja Suryja nudi najnovije Apache / PHP pakete za Ubuntu.

Da biste dodali PPA, pokrenite sljedeću naredbu u WSL bashu:

sudo add-apt-repository ppa:ondrej/apache2

Nakon što je PPA konfiguriran, ažurirajte lokalni indeks paketa:

sudo apt-get update

Instalirajte Apache:

sudo apt-get install apache2

Stvorite mapu projekta za svoje web aplikacije. Ova mapa trebala bi biti izvan WSL datotečnog sustava. Preporučujem vam upotrebu mape Dokumenti.

Sljedeća naredba će stvoriti mapu poslužitelja unutar vašeg direktorija Dokumenti. Zamijenite VAŠE WINDOWS KORISNIČKO IME svojim Windows korisničkim imenom.

sudo mkdir /mnt/c/Users/YOUR WINDOWS USERNAME/Documents/server

Stvorite simboličku vezu do odabrane mape.

sudo ln -s /mnt/c/Users/YOUR WINDOWS USERNAME/Documents/server /var/www/devroot

Otvorite Apacheovu zadanu konfiguracijsku datoteku virtualnog hosta:

sudo nano /etc/apache2/sites-enabled/000-default.conf

Izmijenite korijen dokumenta u “/ var / www / devroot”, što upućuje na mapu vašeg projekta izvan WSL-ovog datotečnog sustava. Postavite ServerNamena localhost(ako je priključak 80 rezerviran za Windows aplikaciju, zamijenite 80 neiskorištenim vratom):

 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

Kad završite, spremite datoteku pritiskom na Ctrl-O i pritisnite Enter za potvrdu. Izlaz Ctrl-X.

Otvorite svoj omiljeni Windows uređivač / IDE i stvorite datoteku “index.html” u mapi projekta (C: \ Users \ YOUR WINDOWS USERNAME \ Documents \ server) sa sljedećim sadržajem:

  It works!

It works!

Pokrenite Apache HTTP poslužitelj:

sudo service apache2 start

Otvorite // localhost / u svom pregledniku i trebali biste vidjeti naslov "Radi".

Ne zaboravite omogućiti Apache module koji su vam potrebni. Na primjer, možete omogućiti mod_rewrite:

sudo a2enmod rewritesudo service apache2 restart

Korak 3: instaliranje MariaDB poslužitelja

Dodajte repo koji sadrži najnovije pakete 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'

Instalirajte MariaDB:

sudo apt-get updatesudo apt-get install mariadb-server

Od vas će se zatražiti da stvorite root lozinku tijekom instalacije. Odaberite sigurnu lozinku i zapamtite je jer će vam trebati kasnije.

Pokreni MariaDB:

sudo service mysql start

Pokrenite sljedeću skriptu (ovo mijenja neke manje sigurne zadane opcije):

mysql_secure_installation

Korak 4: instaliranje PHP-a

Dodajte PPA za najnoviji PHP:

sudo add-apt-repository ppa:ondrej/phpsudo apt-get update

Instalirajte PHP 7.1 pakete:

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

Moramo ponovno pokrenuti 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:

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!