<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Laravel Archives - Chief of Stack</title>
	<atom:link href="https://www.chiefofstack.com/category/laravel/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.chiefofstack.com/category/laravel/</link>
	<description>Full Stack Creative Works by Jay Acab</description>
	<lastBuildDate>Thu, 15 Sep 2022 08:41:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.2</generator>

<image>
	<url>https://www.chiefofstack.com/wp-content/uploads/2020/05/cropped-logo-32x32.jpg</url>
	<title>Laravel Archives - Chief of Stack</title>
	<link>https://www.chiefofstack.com/category/laravel/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Easily create a local Laravel development environment with Docker in Windows</title>
		<link>https://www.chiefofstack.com/2021/06/11/easily-create-a-local-laravel-development-environment-with-docker-in-windows/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=easily-create-a-local-laravel-development-environment-with-docker-in-windows</link>
					<comments>https://www.chiefofstack.com/2021/06/11/easily-create-a-local-laravel-development-environment-with-docker-in-windows/#respond</comments>
		
		<dc:creator><![CDATA[Jay Acab]]></dc:creator>
		<pubDate>Fri, 11 Jun 2021 22:00:42 +0000</pubDate>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Laravel]]></category>
		<category><![CDATA[PHP Development]]></category>
		<category><![CDATA[Tutorials]]></category>
		<guid isPermaLink="false">https://www.chiefofstack.com/?p=2824</guid>

					<description><![CDATA[<p>This article is meant to be a short walkthrough on how to quickly setup a local development environment using Docker, a better alternative to installing a full LAMP stack such as XAMPP on your machine.... </p>
<p class="more"><a class="more-link" href="https://www.chiefofstack.com/2021/06/11/easily-create-a-local-laravel-development-environment-with-docker-in-windows/">Read More</a></p>
<p>The post <a rel="nofollow" href="https://www.chiefofstack.com/2021/06/11/easily-create-a-local-laravel-development-environment-with-docker-in-windows/">Easily create a local Laravel development environment with Docker in Windows</a> appeared first on <a rel="nofollow" href="https://www.chiefofstack.com">Chief of Stack</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>This article is meant to be a short walkthrough on how to quickly setup a local development environment using Docker, a better alternative to installing a full LAMP stack such as XAMPP on your machine. This is not a full tutorial on Docker though, I just find this method works the best for me when developing Laravel apps.</p>



<p>Here&#8217;s the GitHub repo for all the code below, you can clone it for easier deployment. <a href="https://github.com/chiefofstack/docker-boilerplate">https://github.com/chiefofstack/docker-boilerplate</a></p>



<h3>What is Docker and what are we going to create anyway?</h3>



<p><em>&#8220;Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.&#8221;</em><br>&#8211; opensource.com</p>



<figure class="wp-block-image size-large"><img width="1024" height="818" src="https://www.chiefofstack.com/wp-content/uploads/2021/06/what-is-docker.jpg" alt="What is Docker" class="wp-image-2878" srcset="https://www.chiefofstack.com/wp-content/uploads/2021/06/what-is-docker.jpg 1024w, https://www.chiefofstack.com/wp-content/uploads/2021/06/what-is-docker-300x240.jpg 300w, https://www.chiefofstack.com/wp-content/uploads/2021/06/what-is-docker-768x614.jpg 768w, https://www.chiefofstack.com/wp-content/uploads/2021/06/what-is-docker-550x439.jpg 550w, https://www.chiefofstack.com/wp-content/uploads/2021/06/what-is-docker-626x500.jpg 626w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption>Apps and services like PHP can be packaged and deployed easily.<br>Photo from: <a rel="noreferrer noopener" href="https://www.docker.com/resources/what-container" target="_blank">docker.com</a></figcaption></figure>



<p>In a nutshell, we can think of Docker as a lightweight virtual machine that can be easily deployed across any environment together with all the configurations. This guarantees that your application will run precisely how it&#8217;s intended whether it is in production, staging or in a development environment.</p>



<p>That&#8217;s it, we will create service containers for our entire stack. Let&#8217;s get started!</p>



<h3>Step 1. Install Docker Desktop</h3>



<p>If you are a Windows user, download and install Docker for Windows using this <a href="https://docs.docker.com/docker-for-windows/install/" target="_blank" rel="noreferrer noopener">link</a>. Docker Desktop has a built in tool called <strong>docker-compose</strong> which we will utilize to link our services/containers together.</p>



<p><em>&#8220;Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.&#8221; </em><em><br></em>&#8211; docker.com</p>



<p>Note that you need WSL 2 configured on your Windows machine before you can install this app.</p>



<h3>Step 2:&nbsp; Create individual containers for each of our services</h3>



<p>Cd into your project directory and run the following commands in your terminal (for this example I used /home/myuser/Sites/awesomeproject.com). these are basically all the files/folders that we need:</p>



<pre class="wp-block-code">$ touch docker-compose.yml
$ touch Dockerfile
$ mkdir src
$ mkdir mysql
$ mkdir nginx
$ touch nginx/default.conf</pre>



<p>Your file structure should look like this.</p>



<figure class="wp-container-2 wp-block-gallery-1 wp-block-gallery has-nested-images columns-default is-cropped">
<figure class="wp-block-image size-large"><img data-id="2842"  src="https://www.chiefofstack.com/public_html/wp-content/uploads/2021/06/step-2-file-our-structure.jpg" alt="" class="wp-image-2842"/></figure>
</figure>



<p>Edit docker-compose.yml and define our networks</p>



<pre class="wp-block-code">version: '3'
networks:
    laravel: </pre>



<p>Next we define the services we will use for our stack. For NGINX add the following code, this will make the server accessible through port 1010. We also mapped the configuration file to default.conf file found in our nginx folder.:</p>



<pre class="wp-block-code">services:
    nginx:
        image: nginx:stable-alpine
        container_name: nginx_awesomeproject.com
        ports:
            - "1010:80"
        volumes:
            - ./src:/var/www/html
            - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
        depends_on:
            - php
            - mysql
            - phpmyadmin
        networks:
            - laravel</pre>



<p>Then edit nginx/default.conf and add the following code:</p>



<pre class="wp-block-code">server {
    listen 80;
    index index.php index.html;
    server_name localhost;
    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html/public;
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location ~ \.php$ {
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}</pre>



<p>For PHP edit docker-composer.yml again and add the following code. Like the NGINX service above, we prepend the service name php_ for our container_name, used a unique port 1000 so it doesn’t interfere with other local ports in our system and linked it to our container using the ‘laravel’ network. We also added a custom docker file ‘Dockerfile’:</p>



<pre class="wp-block-code">   php:
        build:
            context: .
            dockerfile: Dockerfile
        container_name: php_awesomeproject.com
        volumes:
            - ./src:/var/www/html
        ports:
            - "1000:9000"
        networks:
            - laravel</pre>



<p>Edit Dockerfile and add the following code, this will install php: 7.4-fpm-alpine and also install php extensions.</p>



<pre class="wp-block-code">FROM php:7.4-fpm-alpine
RUN docker-php-ext-install pdo pdo_mysql</pre>



<p>For MySQL add the following to docker-composer.yml.  Here we defined the MySQL credentials that we will use in our Laravel installation. And just like with the previous two service, we linked them up using the “laravel” network</p>



<pre class="wp-block-code">    mysql:
        image: mysql:5.7.22
        container_name: mysql_awesomeproject.com
        restart: unless-stopped
        tty: true
        ports: 
            - "1306:3306"
        volumes:
            - ./mysql_awesomeproject.com:/var/lib/mysql
        environment:
            MYSQL_DATABASE: homestead
            MYSQL_USER: homestead
            MYSQL_PASSWORD: secret
            MYSQL_ROOT_PASSWORD: secret
            SERVICE_TAGS: dev
            SERVICE_NAME: mysql
        networks:
            - laravel</pre>



<p>There you go, if all you want is a basic LEMP stack you can skip to the next step. But in order to make our dev environment even more complete I recommend you go ahead and install the following tools.</p>



<p>We will configure Artisan, the command-line interface included with Laravel. It provides a number of helpful commands for your use while developing your application.:</p>



<pre class="wp-block-code">    artisan:
        build:
            context: .
            dockerfile: Dockerfile
        container_name: artisan_awesomeproject.com
        volumes:
            - ./src:/var/www/html
        depends_on:
            - mysql
        working_dir: /var/www/html
        entrypoint: &#91;'/var/www/html/artisan']
        networks:
            - laravel</pre>



<p></p>



<p>We will add Composer, a dependency manager for PHP:</p>



<pre class="wp-block-code">    composer:
        image: composer:latest
        container_name: composer_awesomeproject.com
        volumes:
            - ./src:/var/www/html
        working_dir: /var/www/html
        depends_on:
            - php
        networks:
            - laravel</pre>



<p>Next we add Node Package Manager. NPM is a package manager for JavaScript.</p>



<pre class="wp-block-code">    npm:
        image: node:13.7
        container_name: npm_awesomeproject.com
        volumes:
            - ./src:/var/www/html
        working_dir: /var/www/html
        entrypoint: &#91;'npm']
        networks:
            - laravel</pre>



<p>I also love to use PHPMyAdmin, a free and open source administration tool for MySQL and MariaDB.</p>



<pre class="wp-block-code">    phpmyadmin:
        image: phpmyadmin/phpmyadmin
        container_name: phpmyadmin_awesomeproject.com
        restart: always
        depends_on:
            - mysql
        ports:
            - 1011:80
        environment:
            PMA_HOST: mysql
            PMA_USER: root
            PMA_PASSWORD: secret            
            PMA_ARBITRARY: 1
        networks:
            - laravel</pre>



<p>Your docker-compose.yml file should look like this.&nbsp;</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="796" src="https://www.chiefofstack.com/wp-content/uploads/2021/06/docker-compose.yml_-1024x796.jpg" alt="docker-compose.yml" class="wp-image-2863" srcset="https://www.chiefofstack.com/wp-content/uploads/2021/06/docker-compose.yml_-1024x796.jpg 1024w, https://www.chiefofstack.com/wp-content/uploads/2021/06/docker-compose.yml_-300x233.jpg 300w, https://www.chiefofstack.com/wp-content/uploads/2021/06/docker-compose.yml_-768x597.jpg 768w, https://www.chiefofstack.com/wp-content/uploads/2021/06/docker-compose.yml_-1536x1194.jpg 1536w, https://www.chiefofstack.com/wp-content/uploads/2021/06/docker-compose.yml_-1060x824.jpg 1060w, https://www.chiefofstack.com/wp-content/uploads/2021/06/docker-compose.yml_-550x427.jpg 550w, https://www.chiefofstack.com/wp-content/uploads/2021/06/docker-compose.yml_-643x500.jpg 643w, https://www.chiefofstack.com/wp-content/uploads/2021/06/docker-compose.yml_-1390x1080.jpg 1390w, https://www.chiefofstack.com/wp-content/uploads/2021/06/docker-compose.yml_.jpg 1920w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>With this file we were able to link our containers using the network &#8220;laravel&#8221;, added volumes to each of our containers and defined the images to be used when we run it later. Those images are available from the Docker library. We were also able to install tools necessary for a better Laravel development experience.</p>



<h3>Step 3: Run the containers and access it locally&nbsp;</h3>



<p>In your terminal run the following in your project folder, this will build and run the containers all in one go. Note that Artisan will fail to run but we will solve that on the next step:</p>



<pre class="wp-block-code">~ docker-compose up -d --build</pre>



<figure class="wp-block-image size-large"><img loading="lazy" width="806" height="453" src="https://www.chiefofstack.com/wp-content/uploads/2021/06/docker-compose-up-d-build.jpg" alt="" class="wp-image-2870" srcset="https://www.chiefofstack.com/wp-content/uploads/2021/06/docker-compose-up-d-build.jpg 806w, https://www.chiefofstack.com/wp-content/uploads/2021/06/docker-compose-up-d-build-300x169.jpg 300w, https://www.chiefofstack.com/wp-content/uploads/2021/06/docker-compose-up-d-build-768x432.jpg 768w, https://www.chiefofstack.com/wp-content/uploads/2021/06/docker-compose-up-d-build-550x309.jpg 550w" sizes="(max-width: 806px) 100vw, 806px" /></figure>



<p>To test, run this command to list all running containers:</p>



<pre class="wp-block-code">~ docker ps/</pre>



<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="103" src="https://www.chiefofstack.com/wp-content/uploads/2021/06/docker-ps-1024x103.jpg" alt="" class="wp-image-2871" srcset="https://www.chiefofstack.com/wp-content/uploads/2021/06/docker-ps-1024x103.jpg 1024w, https://www.chiefofstack.com/wp-content/uploads/2021/06/docker-ps-300x30.jpg 300w, https://www.chiefofstack.com/wp-content/uploads/2021/06/docker-ps-768x77.jpg 768w, https://www.chiefofstack.com/wp-content/uploads/2021/06/docker-ps-1060x107.jpg 1060w, https://www.chiefofstack.com/wp-content/uploads/2021/06/docker-ps-550x55.jpg 550w, https://www.chiefofstack.com/wp-content/uploads/2021/06/docker-ps.jpg 1164w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>You can then check http://localhost:1010 to see if the NGINX server is properly running, right now we don’t have a placeholder index.html there so it will naturally return a 404 error</p>



<figure class="wp-block-image size-large"><img src="https://www.chiefofstack.com/public_html/wp-content/uploads/2021/06/nginx-localhost-1010.jpg" alt="" class="wp-image-2840"/></figure>



<p>To be able to see the database go to http://localhost:1011, where you will see PHPMyAdmin</p>



<figure class="wp-block-image size-large"><img src="https://www.chiefofstack.com/public_html/wp-content/uploads/2021/06/phpmyadmin-localhost-1010.jpg" alt="" class="wp-image-2841"/></figure>



<p>You can connect to mysql through the terminal as well with this code:</p>



<pre class="wp-block-code">~ docker-compose exec mysql mysql -u root -p</pre>



<figure class="wp-block-image size-large"><img src="https://www.chiefofstack.com/public_html/wp-content/uploads/2021/06/docker-compose-exec-mysql.jpg" alt="" class="wp-image-2832"/></figure>



<p>Lastly, you can take the containers down using the following commands:</p>



<pre class="wp-block-code">~ docker-compose down</pre>



<figure class="wp-block-image size-large"><img src="https://www.chiefofstack.com/public_html/wp-content/uploads/2021/06/docker-compose-down.jpg" alt="" class="wp-image-2831"/></figure>



<h3>Final Step: Install Laravel and test the tools</h3>



<p>If you’ve gone this far you would’ve encountered an error when we ran the artisan container earlier. It’s because we do not have Laravel installed in the /src folder yet. We will do that now,&nbsp; cd into your /src folder and run the following composer command:</p>



<pre class="wp-block-code">~ composer create-project laravel/laravel .</pre>



<figure class="wp-block-image size-large"><img src="https://www.chiefofstack.com/public_html/wp-content/uploads/2021/06/docker-compose-create-project-laravel.jpg" alt="" class="wp-image-2830"/></figure>



<p>Configure the .env file to match the MySQL credentials we defined earlier in docker-compose.yml. Your .env file should look like this.<br></p>



<figure class="wp-block-image size-large"><img src="https://www.chiefofstack.com/public_html/wp-content/uploads/2021/06/laravel-env-file.jpg" alt="" class="wp-image-2838"/></figure>



<p>To install a Laravel package using Composer run the following command (we’ll install Socialite for this example):</p>



<pre class="wp-block-code">~ docker-compose run --rm composer require laravel/socialite</pre>



<figure class="wp-block-image size-large"><img src="https://www.chiefofstack.com/public_html/wp-content/uploads/2021/06/docker-compose-composer-require-laravel-socialite.jpg" alt="" class="wp-image-2829"/></figure>



<p>Let’s now test how to compile assets:</p>



<pre class="wp-block-code">~ docker-compose run -rm npm run dev</pre>



<figure class="wp-block-image size-large"><img src="https://www.chiefofstack.com/public_html/wp-content/uploads/2021/06/docker-compose-run-npm-run-dev.jpg" alt="" class="wp-image-2834"/></figure>



<p>Let’s test Artisan by issuing a migrate command:</p>



<pre class="wp-block-code">~ docker-compose run -rm artisan migrate</pre>



<figure class="wp-block-image size-large"><img src="https://www.chiefofstack.com/public_html/wp-content/uploads/2021/06/docker-compose-run-artisan-migrate.jpg" alt="" class="wp-image-2833"/></figure>



<p>Go back to your browser to test the Laravel installation:</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="679" src="https://www.chiefofstack.com/wp-content/uploads/2021/06/laravel-installation-complete-1024x679.jpg" alt="" class="wp-image-2874" srcset="https://www.chiefofstack.com/wp-content/uploads/2021/06/laravel-installation-complete-1024x679.jpg 1024w, https://www.chiefofstack.com/wp-content/uploads/2021/06/laravel-installation-complete-300x199.jpg 300w, https://www.chiefofstack.com/wp-content/uploads/2021/06/laravel-installation-complete-768x509.jpg 768w, https://www.chiefofstack.com/wp-content/uploads/2021/06/laravel-installation-complete-1060x703.jpg 1060w, https://www.chiefofstack.com/wp-content/uploads/2021/06/laravel-installation-complete-550x365.jpg 550w, https://www.chiefofstack.com/wp-content/uploads/2021/06/laravel-installation-complete-754x500.jpg 754w, https://www.chiefofstack.com/wp-content/uploads/2021/06/laravel-installation-complete.jpg 1235w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<h3>Bonus Step:</h3>



<p>I edited my Linux .bashrc file and added &#8220;dcr&#8221; as an alias for &#8220;docker-compose run -rm&#8221; command.</p>



<figure class="wp-block-image size-large"><img src="https://www.chiefofstack.com/public_html/wp-content/uploads/2021/06/edit-bashrc-file-add-docker-compose-alias.jpg" alt="" class="wp-image-2837"/></figure>



<p>This will make it easier to run the tools using the following shorthand &#8220;dcr npm run dev&#8221;, &#8220;dcr artisan migrate&#8221;, “dcr composer install”.&nbsp;</p>



<p>SOURCES:<br><a href="https://opensource.com/resources/what-docker" target="_blank" rel="noreferrer noopener">https://opensource.com/resources/what-docker</a><br><a href="https://docs.docker.com/compose/" target="_blank" rel="noreferrer noopener">https://docs.docker.com/compose/</a></p>
<p>The post <a rel="nofollow" href="https://www.chiefofstack.com/2021/06/11/easily-create-a-local-laravel-development-environment-with-docker-in-windows/">Easily create a local Laravel development environment with Docker in Windows</a> appeared first on <a rel="nofollow" href="https://www.chiefofstack.com">Chief of Stack</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.chiefofstack.com/2021/06/11/easily-create-a-local-laravel-development-environment-with-docker-in-windows/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Improving site speed: Time to First Byte (TTFB)</title>
		<link>https://www.chiefofstack.com/2020/09/22/improving-site-speed-time-to-first-byte-ttfb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=improving-site-speed-time-to-first-byte-ttfb</link>
					<comments>https://www.chiefofstack.com/2020/09/22/improving-site-speed-time-to-first-byte-ttfb/#respond</comments>
		
		<dc:creator><![CDATA[Jay Acab]]></dc:creator>
		<pubDate>Tue, 22 Sep 2020 03:58:31 +0000</pubDate>
				<category><![CDATA[Laravel]]></category>
		<category><![CDATA[Tutorials]]></category>
		<guid isPermaLink="false">https://www.chiefofstack.com/?p=2654</guid>

					<description><![CDATA[<p>More than half of visits are abandoned if a page doesn&#8217;t load within 3 seconds, this translates to a huge loss in potential sales and leaves a bad impression to users. Speed is now also... </p>
<p class="more"><a class="more-link" href="https://www.chiefofstack.com/2020/09/22/improving-site-speed-time-to-first-byte-ttfb/">Read More</a></p>
<p>The post <a rel="nofollow" href="https://www.chiefofstack.com/2020/09/22/improving-site-speed-time-to-first-byte-ttfb/">Improving site speed: Time to First Byte (TTFB)</a> appeared first on <a rel="nofollow" href="https://www.chiefofstack.com">Chief of Stack</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>More than half of visits are abandoned if a page doesn&#8217;t load within 3 seconds, this translates to a huge loss in potential sales and leaves a bad impression to users. Speed is now also a landing page factor for Google Search and though it cannot be proven that an improved Time to First Byte (TTFB) increases Page Rank, there is enough correlation to believe so. Take AMP pages, Google Search now favors AMP pages in their results. Accelerated Mobile Pages (AMP) are stripped down versions of HTML that makes loading on mobile devices faster. </p>



<p>So what really is TTFB? and what affects it?  TTFB  is the metric to focus on if you want to drastically improve your page load time, technically it is affected by these 3 things:</p>



<ol><li>The time it takes for your request to propagate through the network to the web server</li><li>The time it takes for the web server to process the request and generate the response</li><li>The time it takes for the response to propagate back through the network to your browser.</li></ol>



<figure class="wp-block-image size-large"><img loading="lazy" width="971" height="264" src="https://www.chiefofstack.com/wp-content/uploads/2020/09/ttfb-no-caching-no-cdn.jpg" alt="" class="wp-image-2675" srcset="https://www.chiefofstack.com/wp-content/uploads/2020/09/ttfb-no-caching-no-cdn.jpg 971w, https://www.chiefofstack.com/wp-content/uploads/2020/09/ttfb-no-caching-no-cdn-300x82.jpg 300w, https://www.chiefofstack.com/wp-content/uploads/2020/09/ttfb-no-caching-no-cdn-768x209.jpg 768w, https://www.chiefofstack.com/wp-content/uploads/2020/09/ttfb-no-caching-no-cdn-550x150.jpg 550w" sizes="(max-width: 971px) 100vw, 971px" /><figcaption><em>DNS time, socket time, SSL time, redirect time, and backend processing took around 6 seconds</em></figcaption></figure>



<p>In layman&#8217;s term, it&#8217;s just the time when the first byte of the page arrive (after any redirects). You don&#8217;t want to look at a waterfall chart like the one above where the first 3 items took around 6 seconds. </p>



<p>As an engineer, you need to take care of 3 things to be able to improve TTFB: </p>



<ol><li>Proper use of a Content Delivery Network (CDN) to reduce latency</li><li>Use of dynamic cache or page cache to reduce server processing.</li><li>Compression and minification of web assets to decrease loading</li></ol>



<h4>Proper use of a CDN</h4>



<p>With a CDN you reduce the physical distance and the number of hops an asset take to reach its destination. For example, the user is from Manila and the server is in New York, a CDN server in between (say Singapore) can deliver a jpeg file faster than the actual server in New York. Cloudflare CDN saves static content to it&#8217;s network of servers around the world then serve those cached content from edge servers nearest the next visitor.</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="1081" height="766" src="https://i0.wp.com/www.chiefofstack.com/wp-content/uploads/2020/09/cloudflare-page-rules.png?fit=710%2C503&amp;ssl=1" alt="" class="wp-image-2665" srcset="https://www.chiefofstack.com/wp-content/uploads/2020/09/cloudflare-page-rules.png 1081w, https://www.chiefofstack.com/wp-content/uploads/2020/09/cloudflare-page-rules-300x213.png 300w, https://www.chiefofstack.com/wp-content/uploads/2020/09/cloudflare-page-rules-1024x726.png 1024w, https://www.chiefofstack.com/wp-content/uploads/2020/09/cloudflare-page-rules-768x544.png 768w, https://www.chiefofstack.com/wp-content/uploads/2020/09/cloudflare-page-rules-1060x751.png 1060w, https://www.chiefofstack.com/wp-content/uploads/2020/09/cloudflare-page-rules-550x390.png 550w, https://www.chiefofstack.com/wp-content/uploads/2020/09/cloudflare-page-rules-706x500.png 706w" sizes="(max-width: 1081px) 100vw, 1081px" /><figcaption><em>&#8220;Cache everything&#8221; page rule in Cloudflare</em></figcaption></figure>



<p>TIP: If you have a static site like a blog or a basic company website,  just use the &#8220;Cache everything&#8221; page rule in Cloudflare. Using that will greatly improve your TTFB even if you only use an economy web host because most requests will be served from the edge cache and won&#8217;t actually touch your server. Just make sure to add another page rule to bypass caching of admin and dynamic pages.</p>



<h4>Caching and Compression</h4>



<figure class="wp-block-image size-large"><img loading="lazy" width="830" height="380" src="https://www.chiefofstack.com/wp-content/uploads/2020/09/WP-fastest-cache-1.png" alt="" class="wp-image-2663" srcset="https://www.chiefofstack.com/wp-content/uploads/2020/09/WP-fastest-cache-1.png 830w, https://www.chiefofstack.com/wp-content/uploads/2020/09/WP-fastest-cache-1-300x137.png 300w, https://www.chiefofstack.com/wp-content/uploads/2020/09/WP-fastest-cache-1-768x352.png 768w, https://www.chiefofstack.com/wp-content/uploads/2020/09/WP-fastest-cache-1-550x252.png 550w" sizes="(max-width: 830px) 100vw, 830px" /><figcaption><em>The WordPress Fastest Cache plugin saves pages in cache to reduce server processing time</em></figcaption></figure>



<p>Content Management Systems (CMS) generate pages on demand through the use of PHP and MySQL server requests. By using dynamic or file cache, commonly accessed pages can be stored in a file (or in memory) to reduce server processing time. You can think of it as the recent calls in your phone&#8217;s history, if you want to call someone again you don&#8217;t have to go through the trouble of finding that person in your phone book. </p>



<p>In Laravel, I simply run <code>npm run production </code> in my terminal and let Webpack take care of bundling the assets (minifying and compression) and then use my favorite package to cache pages. In WordPress, there&#8217;s a lot of plugins to cache pages (I regularly use WP Fastest Cache and WP Super cache plugins). I like WP Fastest Cache because it has options to minimize and compress CSS and JS files automatically. Whatever the framework is, the goal is always to reduce the time it takes for requests to reach users.</p>



<h4>Measuring TTFB</h4>



<figure class="wp-block-image size-large"><img loading="lazy" width="1126" height="871" src="https://i0.wp.com/www.chiefofstack.com/wp-content/uploads/2020/09/Webpagetest.org-first-byte-test.jpg?fit=710%2C549&amp;ssl=1" alt="" class="wp-image-2667" srcset="https://www.chiefofstack.com/wp-content/uploads/2020/09/Webpagetest.org-first-byte-test.jpg 1126w, https://www.chiefofstack.com/wp-content/uploads/2020/09/Webpagetest.org-first-byte-test-300x232.jpg 300w, https://www.chiefofstack.com/wp-content/uploads/2020/09/Webpagetest.org-first-byte-test-1024x792.jpg 1024w, https://www.chiefofstack.com/wp-content/uploads/2020/09/Webpagetest.org-first-byte-test-768x594.jpg 768w, https://www.chiefofstack.com/wp-content/uploads/2020/09/Webpagetest.org-first-byte-test-1060x820.jpg 1060w, https://www.chiefofstack.com/wp-content/uploads/2020/09/Webpagetest.org-first-byte-test-550x425.jpg 550w, https://www.chiefofstack.com/wp-content/uploads/2020/09/Webpagetest.org-first-byte-test-646x500.jpg 646w" sizes="(max-width: 1126px) 100vw, 1126px" /><figcaption><em>This site&#8217;s results in webpagetest.org</em> <em>where the First Byte time is only 300ms.</em></figcaption></figure>



<p>To be able to improve your TTFB you have to be able to measure it properly. There are a number of tools to measure site speed and TTFB, one tool that I always use is WebPageTest.org. There you can place your site&#8217;s URL and test location (from where you want to simulate the speed) and in less than a minute you will get a detailed waterfall of all resources being loaded on a page.</p>



<p></p>



<p></p>
<p>The post <a rel="nofollow" href="https://www.chiefofstack.com/2020/09/22/improving-site-speed-time-to-first-byte-ttfb/">Improving site speed: Time to First Byte (TTFB)</a> appeared first on <a rel="nofollow" href="https://www.chiefofstack.com">Chief of Stack</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.chiefofstack.com/2020/09/22/improving-site-speed-time-to-first-byte-ttfb/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
