{"id":590,"date":"2018-03-10T10:50:14","date_gmt":"2018-03-10T10:50:14","guid":{"rendered":"http:\/\/www.45rpmsoftware.com\/wordpress\/?p=590"},"modified":"2018-03-10T10:51:58","modified_gmt":"2018-03-10T10:51:58","slug":"docker-lab-containerising-your-website-part-2","status":"publish","type":"post","link":"https:\/\/www.45rpmsoftware.com\/blog\/?p=590","title":{"rendered":"Docker Lab \u2013 Containerising your website, Part 2 (PHP)"},"content":{"rendered":"<p>In the previous instalment (<a href=\"http:\/\/www.45rpmsoftware.com\/wordpress\/?p=586\">Docker Lab \u2013 Containerising your website, Part 1<\/a>), we installed Docker and Docker Compose, and set up an Nginx container. If all you need is to serve a few static pages there&#8217;s no need to read any further. But if you need your website to be dynamic, if you need PHP and, perhaps, a database, read on\u2026<\/p>\n<p>These instructions assume that you have followed the steps in part 1.<!--more--><\/p>\n<p>It will come as no surprise that, in order to install PHP, we need to edit the docker-compose.yml file. But before we do that, it will be necessary to stop Docker (just in case you left it running from the last session). In fact, you can edit the docker-compose.yml file while Docker is still running &#8211; but the changes won&#8217;t take effect until Docker is restarted.<\/p>\n<p>Check to see if Docker is running as follows:<\/p>\n<pre>docker ps<\/pre>\n<p>If any docker containers are running then you&#8217;ll see them listed like this:<\/p>\n<pre>CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES\r\n 673b1e25c03a bitnami\/nginx:latest \"\/app-entrypoint.s...\" 3 hours ago Up 3 hours 0.0.0.0:80-&gt;8080\/tcp, 0.0.0.0:443-&gt;8443\/tcp lempcompose_nginx_1<\/pre>\n<p>If you haven&#8217;t started any Docker containers manually then you can stop the containers using Docker Compose.<\/p>\n<h3>Stopping using Docker Compose<\/h3>\n<p>When I stop jobs using Docker Compose, I always like to clean up as well. The containers will be rebuilt by Compose, so there isn&#8217;t a great deal of extra effort and it helps keep the cruft under control.<\/p>\n<pre>cd ~\/lemp-compose\r\ndocker-compose stop\r\ndocker-compose rm<\/pre>\n<h3>Stopping Manually<\/h3>\n<p>If you have started any containers manually then you&#8217;ll need to stop them by using the docker command and the container ID.<\/p>\n<pre>docker stop &lt;containerId&gt;<\/pre>\n<p>This step will need to be repeated for all containers which are running.<\/p>\n<h2>Configuring your Containers.<\/h2>\n<p>Now that your containers are stopped, the docker-compose.yml file will need to be edited to add a PHP container. Add a networks section to the top of your docker-compose.yml, beneath the version number and before services:<\/p>\n<pre>networks:\r\n app-tier:\r\n  driver: bridge<\/pre>\n<p>Now add a section for PHP. Note that you&#8217;ll need a different PHP section for each vhost that you want to use PHP. You can&#8217;t (easily) host multiple websites with a single PHP instance. Because each vhost will have its own PHP instance I like to add the website name to the name of the PHP container that it will use.<\/p>\n<pre>phpfpm-&lt;your website name&gt;:\r\n image: 'bitnami\/php-fpm:latest'\r\n networks:\r\n   - app-tier\r\n volumes:\r\n   - .\/public\/&lt;your website name&gt;\/htdocs:\/app\r\n   - .\/logs\/&lt;your website name&gt;-php:\/opt\/bitnami\/php\/log<\/pre>\n<p>Finally, add the network and dependency to your nginx container configuration, under the image name and before the ports.<\/p>\n<pre>depends_on:\r\n  - phpfpm-&lt;your website name&gt;\r\nnetworks:\r\n  - app-tier<\/pre>\n<p>Having made these changes, your docker-compose.yml should look like this:<\/p>\n<pre>version: '2'\r\n\r\nnetworks:\r\n app-tier:\r\n  driver: bridge\r\n\r\nservices:\r\n nginx:\r\n  image: 'bitnami\/nginx:latest'\r\n  depends_on:\r\n   - phpfpm-&lt;your website name&gt;\r\n  networks:\r\n   - app-tier\r\n  ports:\r\n   - '80:8080'\r\n   - '443:8443'\r\n  volumes:\r\n   - .\/nginx\/nginx.conf:\/bitnami\/nginx\/conf\/nginx.conf\r\n   - .\/nginx\/fastcgi.conf:\/bitnami\/nginx\/conf\/fastcgi.conf\r\n   - .\/logs:\/opt\/bitnami\/nginx\/logs\r\n   - .\/nginx\/vhosts:\/bitnami\/nginx\/conf\/vhosts\r\n   - .\/public:\/opt\/bitnami\/nginx\/html\r\n phpfpm-&lt;your website name&gt;:\r\n  image: 'bitnami\/php-fpm:latest'\r\n  networks:\r\n   - app-tier\r\n  volumes:\r\n   - .\/public\/&lt;your website name&gt;\/htdocs:\/app\r\n   - .\/logs\/&lt;your website name&gt;-php:\/opt\/bitnami\/php\/log<\/pre>\n<h2>Website configuration<\/h2>\n<p>So far so good &#8211; it still won&#8217;t work though &#8211; we need to make changes to the vhost configuration. The reason that it won&#8217;t work is that we haven&#8217;t set up PHP in the vhost configuration, and it will also be necessary to modify the root information so that all resources (php, html, image files etc) can be accessed correctly.<\/p>\n<pre>nano ~\/nginx\/vhosts\/sites-available\/&lt;your website name&gt;.conf<\/pre>\n<p>Edit it as follows, replacing the server_name domain name with the domain name(s) of your website.<\/p>\n<pre>server {\r\n listen 0.0.0.0:8080;\r\n server_name &lt;your domain name&gt;;\r\n server_tokens off;\r\n \r\n error_log \"\/opt\/bitnami\/nginx\/logs\/&lt;your website name&gt;-error.log\";\r\n access_log \"\/opt\/bitnami\/nginx\/logs\/&lt;your website name&gt;-access.log\";\r\n\r\n location \/ {\r\n  try_files $uri $uri\/index.php;\r\n }\r\n\r\n location ~ \\.php$ {\r\n  # fastcgi_pass [PHP_FPM_LINK_NAME]:9000;\r\n  fastcgi_pass phpfpm-&lt;your website name&gt;:9000;\r\n  fastcgi_index index.php;\r\n  include fastcgi.conf;\r\n  root \/app;\r\n }\r\n\r\n location ~ \\.htm$ {\r\n  root \/opt\/bitnami\/nginx\/html\/&lt;your website name&gt;\/htdocs;\r\n }\r\n \r\n location ~* \\.(js|css|png|jpg|jpeg|gif|ico)$ {\r\n  expires max;\r\n  log_not_found off;\r\n  root \/opt\/bitnami\/nginx\/html\/&lt;your website name&gt;\/htdocs;\r\n }\r\n}<\/pre>\n<p>If you\u2019ve pulled all of that off correctly, you should be able to put info.php into ~\/lemp-compose\/public\/&lt;your website name&gt;\/htdocs and start Docker:<\/p>\n<pre>cd ~\/lemp-compose \r\ndocker-compose up --remove-orphans -d<\/pre>\n<p>Enter your website URL into your browser and (fingers crossed) access your info.php which will, hopefully, be displayed correctly, served up by your containerised instance of Nginx and PHP. Go and have a well earned cup of tea before moving onto the next stage \u2013 setting up your database.<\/p>\n<h2>info.php<\/h2>\n<p>If you&#8217;ve never created an info.php file before it&#8217;s quite simple. It contains the following and nothing else:<\/p>\n<pre>&lt;?php\r\nphpinfo();\r\n?&gt;<\/pre>\n<p>If it displays information about your PHP instance then PHP is installed correctly. If nothing is displayed, or if you are greeted with the dreaded &#8216;File Not Found&#8217; then go back and check your steps carefully.<\/p>\n<p>Once you have confirmed that PHP is installed correctly be sure to delete your info.php file &#8211; it&#8217;s a hackers goldmine and may reveal passwords and other information that make your website trivially easy to compromise.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous instalment (Docker Lab \u2013 Containerising your website, Part 1), we installed Docker and Docker Compose, and set up an Nginx container. If all you need is to serve a few static pages there&#8217;s no need to read any further. But if you need your website to be dynamic, if you need PHP &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.45rpmsoftware.com\/blog\/?p=590\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Docker Lab \u2013 Containerising your website, Part 2 (PHP)&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15,18,28,5],"tags":[],"_links":{"self":[{"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/590"}],"collection":[{"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=590"}],"version-history":[{"count":0,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/590\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}