PHP arenduskeskkonna seadistamine Windowsi allsüsteemis Linuxile (WSL)

PHP arendamisel Windowsis on mõned puudused. Kuid Microsoft pakub nüüd Windowsis töötavatele PHP-arendajatele suurepärast võimalust: Windowsi alamsüsteem Linuxile (WSL). WSL on ühilduvuskiht Linuxi binaarsete käivitatavate failide (ELF-vormingus) loomupäraseks käitamiseks Windows 10-s. Microsoft ütleb:

"See on peamiselt tööriist arendajatele - eriti veebiarendajatele ja neile, kes töötavad avatud lähtekoodiga projektide kallal".

Saame Linuxi keskkonda käivitada otse Windowsis ilma virtuaalse masina lisakuludeta.

Märkus. See artikkel pole mõeldud ainult Windowsi siseringi kasutajatele. Need meetodid töötavad ka Windows 10 uusimate stabiilsete väljalasete puhul.

Selles õpetuses seadistame WSL-i arendamiseks LAMP-i virna (Ubuntu 16.04, Apache, PHP 7.1, MariaDB). Sarnaste meetoditega saate seadistada ka teisi virnasid (näiteks LEMP-i virna).

Eeldused

Enne selle juhendi alustamist vajate järgmist:

  • Windows 10 64-bitine versioon koos loojate värskendusega või uuem.
  • Linuxi / bashi tundmine (kui soovite käsureaga tutvuda, võite lugeda seda DigitalOceani õpetust).

1. samm: bashi installimine Windowsi

Esiteks peate oma arvutisse installima WSL-i.

Microsoft Store'ist saate installida rohkem Linuxi jaotusi (Ubuntu, openSUSE, SUSE Linux Enterprise Server 12). Kuid selles õpetuses seadistame Ubuntu LAMP-i virna, nii et peate valima Ubuntu .

Microsoftil on suurepärane õpetus WSL-i installimiseks, järgige palun artikli juhiseid.

Kui olete Bashi Windowsi Ubuntu edukalt installinud, installime ja konfigureerime arendamiseks lihtsa LAMP-i virna.

2. samm: installige Apache HTTP server

Soovime installida Apache uusima stabiilse versiooni, kuid ametlikud Ubuntu hoidlad ei sisalda viimast versiooni.

Peame lisama PPA Apache'i pakettidele. Isikliku paketi arhiiv (PPA) on hoidla, mis võimaldab kolmanda osapoole arendajatel ehitada ja levitada pakette Ubuntu jaoks. Ondřej Surý PPA pakub Ubuntu jaoks uusimaid Apache / PHP pakette.

PPA lisamiseks käivitage WSL-i bash'is järgmine käsk:

sudo add-apt-repository ppa:ondrej/apache2

Kui PPA on konfigureeritud, värskendage kohaliku paketi indeksit:

sudo apt-get update

Apache installimine:

sudo apt-get install apache2

Looge oma veebirakenduste jaoks projektkaust. See kaust peaks olema väljaspool WSL-failisüsteemi. Soovitan teil kasutada kausta Dokumendid.

Järgmine käsk loob teie dokumendikausta serveri kausta. Palun vahetage oma Windows USERNAME oma Windows kasutajanimi.

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

Looge valitud kausta sümboolne link.

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

Avage Apache vaikimisi virtuaalse hosti konfiguratsioonifail:

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

Muutke dokumendi juur väärtuseks „/ var / www / devroot”, mis osutab teie projekti kaustale väljaspool WSL-i failisüsteemi. Määra ServerNameväärtuseks localhost(kui pordi 80 reserveerib Windowsi rakendus, asendage 80 kasutamata pordiga):

 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

Kui olete lõpetanud, salvestage fail, vajutades Ctrl-O, ja vajutage kinnitamiseks Enter. Väljuge klahvikombinatsiooniga Ctrl-X.

Avage oma lemmik Windowsi redaktor / IDE ja looge oma projekti kausta (C: \ Users \ YOUR WINDOWS USERNAME \ Documents \ server) fail index.html järgmise sisuga:

  It works!

It works!

Käivitage Apache HTTP server:

sudo service apache2 start

Avage brauseris // localhost / ja peaksite nägema pealkirja "See töötab".

Ärge unustage lubada Apache mooduleid, mis on teile vajalikud. Näiteks saate lubada mod_rewrite:

sudo a2enmod rewritesudo service apache2 restart

3. samm: MariaDB serveri installimine

Lisage repo, mis sisaldab uusimaid MariaDB pakette:

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'

Installige MariaDB:

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

Installimise ajal palutakse teil luua juurparool. Valige turvaline parool ja pidage seda meeles, sest vajate seda hiljem.

MariaDB käivitamine:

sudo service mysql start

Käivitage järgmine skript (see muudab mõnda vähem turvalist vaikevalikut):

mysql_secure_installation

4. samm: PHP installimine

Lisage uusima PHP jaoks PPA:

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

Installige PHP 7.1 paketid:

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

Peame Apache taaskäivitama:

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!