Saturday, August 19, 2017

Use Visual studio code for PHP and Virtual Machine, Vagrant

This is for windows.

1. Download and install Visual Studio Code from the official page.
https://code.visualstudio.com/download

2. Start the Visual Studio Code. And click the square symbol to install extensions for PHP.
You can search extensions from here.

3. Install these extensions: php intelliSence, HTML CSS support, PHP debug

4. Close the Visual studio code and open it again.

5. Maybe you will see this error. Ignore it now.

6. Download PHP from here: http://windows.php.net/download#php-7.1
I chose PHP 7.1 because it's being used in my virtual server.

7. Create a folder for PHP in this directory: C:\. Save the downloaded PHP inside this folder.

 From the extracted folder, just copy and paste all of the files 
to the PHP folder which we created under C:\.

Then add the path (C:\PHP) to the environment variables.

Go to:
Control Panel  >  System and Security  >  System
Then open Advanced system settings.

Open environment variables.

Add "C:\PHP;" to the PATH variable. If end of the path doesn't have ";", you need to add ";" like ";C:\PHP" because each path must be separated by ";".



You can check if the path is working by running "php -version" command on cmd:
Please note that you need to reopen the cmd after you change the environment path.


8. Close the Visual Studio code and open it again.

9. You will see this error. Click the "Open settings".


10. You can see 2 windows. In the right window, add below codes:
"php.validate.executablePath": "C:/PHP/php.exe",
"php.executablePath": "C:/PHP/php.exe"


11. Close the Visual Studio Code and open it again. The PHP executable not found error should be gone now. Now click the bug symbol on the left menu.

12. Click the gear symbol and choose PHP. Then launch.json will be generated automatically.
 

13. Open your launch.json and write like this:



{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000,
            //Your project's path in the server
            "pathMappings": {
                "/vagrant": "${workspaceRoot}"
            }
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000
        }
    ]
}



14. Install x-debug in your server or virtual machine (For CentOS 7).
For example, if you use IUS repository:
$ sudo rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
$ sudo yum install https://centos7.iuscommunity.org/ius-release.rpm

If you already have repositories (like Remi, webtatic), you don't need to add these repositories.

If you are using IUS repository:
Please note this is a xdebug for PHP7.1.
$ sudo yum -y install php71u-pecl-xdebug

And open your xdebug.ini:
$ sudo vi /etc/php.d/xdebug.ini

Or probably:
$ sudo vi /etc/php.d/15-xdebug.ini

And add these lines in the file:
[xdebug]
xdebug.default_enable = 1
xdebug.idekey = "vscode"
xdebug.remote_enable = 1
xdebug.remote_port=9000
xdebug.remote_autostart=1
xdebug.remote_host=192.168.10.100 #Your host PC's IP here! Not guest machine's IP!

(Press a on your keyboard to go into insert mode, then you can edit the file. Press escape key to stop the insert mode. After stopping the insert mode, press shift key + g to go to the lowest row. Shift + zz or :wq to save and close the file. You can go to command mode by pressing : on your keyboard. To save and quit by the command mode, input :wq and enter. To exit the file without saving the file, inout :q! and enter. To search a word, press ? then write a word what you want to search. For example, if you write ?aaa and press enter, "aaa" is searched and highlighted in the file. Press n to jump to next match.)

Save and close it with a command ":wq".

15.  Restart Apache.
Run this command:
$ sudo service httpd restart

Now your x-debug should work.

16. Choose "Listen for Xdebug" from the debug menu. Then click the play button from the menu. If you are asked by windows if it is ok to allow access, answer yes.

Write as follows in php file and save it as phpinfo.php in the vagrant shared folder:
<?php
phpinfo();
?>
Check if it can be seen from browser. If it can, open it from Visual Studio Code.

Add some breakpoints:

Then reload the page from your browser:

You can see that xdebug is working.