<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sergey Lisovskiy</title>
    <description>The latest articles on DEV Community by Sergey Lisovskiy (@lsvs).</description>
    <link>https://dev.to/lsvs</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F970119%2F58a14ecc-b863-49f4-a561-844a65d919ed.jpeg</url>
      <title>DEV Community: Sergey Lisovskiy</title>
      <link>https://dev.to/lsvs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lsvs"/>
    <language>en</language>
    <item>
      <title>How to Launch a PHP Project in VS Code Dev Container</title>
      <dc:creator>Sergey Lisovskiy</dc:creator>
      <pubDate>Fri, 22 Dec 2023 15:57:29 +0000</pubDate>
      <link>https://dev.to/lsvs/how-to-launch-a-php-project-in-vs-code-dev-container-2mp0</link>
      <guid>https://dev.to/lsvs/how-to-launch-a-php-project-in-vs-code-dev-container-2mp0</guid>
      <description>&lt;p&gt;As a designer and frontend developer, I often find myself working on various projects. Recently, while working on one of legacy projects, I needed to make updates to a website that was built with PHP. After conducting some research, I discovered that the best option for creating a PHP development environment for my case would be to utilize the VS Code Dev Container feature.&lt;/p&gt;

&lt;p&gt;The Dev Container feature in VS Code allows developers to define and configure a development environment using containers. You can define the runtime, tools, and dependencies required for your project, ensuring consistency and reproducibility across different development machines.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisites:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://code.visualstudio.com/Download"&gt;Visual Studio Code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers"&gt;Dev Containers&lt;/a&gt; extension&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.docker.com/products/docker-desktop/"&gt;Docker&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To get started with the Dev Container feature in VS Code, you need to create a &lt;code&gt;.devcontainer&lt;/code&gt; folder in your project's root directory. Inside this folder, you can define a &lt;code&gt;devcontainer.json&lt;/code&gt; file that specifies the Docker image, runtime settings, and any additional configurations required for your PHP project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"PHP"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"image"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"mcr.microsoft.com/devcontainers/php"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"forwardPorts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, essentially, that’s all 🙃. We can now use &lt;code&gt;php -S&lt;/code&gt; inside the container to launch the dev server. But the container also already includes Apache, which is used in my project's production environment. Consequently, I've opted to utilize it and automate the configuration and launch of Apache as well.&lt;br&gt;
In my case, I extended the default configuration by adding some scripts to the &lt;code&gt;postCreateCommand&lt;/code&gt; field in the &lt;code&gt;devcontainer.json&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"postCreateCommand"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"sudo chmod a+x ./.devcontainer/postCreateCommand.sh &amp;amp;&amp;amp; ./.devcontainer/postCreateCommand.sh"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the shell script that I added:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo chmod &lt;/span&gt;a+x &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;pwd&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;/public_html"&lt;/span&gt;
&lt;span class="nb"&gt;sudo rm&lt;/span&gt; &lt;span class="nt"&gt;-rf&lt;/span&gt; /var/www/html
&lt;span class="nb"&gt;sudo ln&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;pwd&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;/public_html"&lt;/span&gt; /var/www/html
&lt;span class="nb"&gt;sudo &lt;/span&gt;a2enmod rewrite
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'error_reporting=0'&lt;/span&gt; | &lt;span class="nb"&gt;sudo tee&lt;/span&gt; /usr/local/etc/php/conf.d/no-warn.ini
&lt;span class="nb"&gt;sudo &lt;/span&gt;apache2ctl start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These scripts perform the following actions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Grant execute permission to the &lt;code&gt;public_html&lt;/code&gt; directory, where my PHP files are located.&lt;/li&gt;
&lt;li&gt;Remove the default files in the &lt;code&gt;/var/www/html&lt;/code&gt; directory.&lt;/li&gt;
&lt;li&gt;Create a symbolic link from the &lt;code&gt;public_html&lt;/code&gt; directory to the &lt;code&gt;/var/www/html directory&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Activate the rewrite module in Apache configuration.&lt;/li&gt;
&lt;li&gt;Turn off PHP error reporting by adding a configuration file &lt;code&gt;no-warn.ini&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Start the Apache server.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Great, everything is ready. Now, to seamlessly integrate our configured development environment, run Docker and navigate to the bottom left corner of VS Code, where you’ll find the “Remote Window” button. Click on it, and choose the “Reopen in Container” option.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnaqokvh9rydx5znl6zuf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnaqokvh9rydx5znl6zuf.png" alt="VS Code screenshot with highlighted Dev Container button" width="800" height="624"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Selecting “Reopen in Container” triggers VS Code to rebuild the container based on the configurations we’ve set in the &lt;code&gt;.devcontainer&lt;/code&gt; folder. Once the container rebuild is complete, you'll find yourself working inside the containerized environment, seamlessly incorporating the specified PHP runtime and the configured Apache server.&lt;/p&gt;

&lt;p&gt;Now you can open your web browser and navigate to &lt;a href="http://localhost:8080"&gt;localhost:8080&lt;/a&gt;. Here, you’ll see your PHP application running within the container.&lt;/p&gt;




&lt;p&gt;In conclusion, the VS Code Dev Container feature proves to be a convenient and efficient tool for setting up and managing PHP development environments. Its ability to streamline the development process ensures consistency across various machines, making it an excellent choice for PHP project work.&lt;/p&gt;




&lt;p&gt;Originally posted at &lt;a href="https://lisovskiy.medium.com/how-to-launch-a-php-project-in-vs-code-dev-container-4896fd55663c"&gt;Medium&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>devcontainer</category>
      <category>php</category>
      <category>docker</category>
    </item>
  </channel>
</rss>
