Over years, PHP has consolidated its position as the preferred development language for legacy and real-time projects. With the recent release of PHP versions, the language is now in a great position to help developers launch RAD enabled apps quickly.
At Cloudways, we try to facilitate PHP developers in launching their projects without worrying about the hassles of server management. Our about-to-be-released Content Delivery System (CDN) is a continuation of this commitment. In this article, I will go over the process of enabling and configuring CloudwaysCDN for your PHP applications.
- Cloudways server with PHP application
- Symfony installed on the server (or Symfony Flex enabled application)
- Your domain should be mapped
You can follow the following GIF to launch a server and a PHP application on Cloudways. By default, you will get PHP 7.0; however, you could downgrade or upgrade to PHP 5.6 or 7.1.
First of all, let’s talk about CDN, and why you need to integrate it into PHP projects.
A CDN is a system of multiple servers which delivers static content (such as text, images) to the users. Instead of serving the static content from the original server, a CDN ensures that the user is served the data from the nearest location (a.k.a., points of presence, or PoPs). Each PoP contains a number of caching servers responsible for content delivery to visitors within its proximity.
CDN ensures speedy delivery of content, thus improving the speed of websites and web apps. When a CDN is active, the actual location of the website does not matter. The CDN ensures that the visitor receives the data from the nearest location. Since Symfony is well known for powering fintech and ecommerce applications, CDn plays a central role in speeding up the user experience.
When you login to your server, move to the Application tab and open the PHP application. You will see the tab for configuring CDN at the end of the options. When you click the tab, you will enter the CDN configuration screen. Here you will find the URL of the application to be inserted in the WEBSITE URL field and click the Create button.
Next you will get the newly generated URL for you Symfony application:
Few things to note here:
- You can view the bandwidth usage on this screen.
- You can always purge old content from CDN by using the Purge button.
- You can always remove your subscription anytime.
In core PHP applications developers love to create their own usecases to integrate CDN. one use case will be to define the CDN url in php like this:
<?php define('CDN', 'https://3971-7889-2-raikfcquaxqncofqfm.stackpathdns.com/'); ?>
Now, add it to every asset URL here asset means all the JS,Css and image files:
The URL generated for the assets will look like this now:
This is just one example although developers tries different usecase with the websites.
Symfony provides easy configuration options for CDN services. If you know about
base_url, you are good to go. In the standard Symfony installation, move to app >> config and open
You need to find the asset settings in framework configs. Since our CDN is asset based, I will concatenate on the CDN Url as
base_url to every asset file in Symfony.
framework: assets: base_urls: - 'https://3971-7889-2-raikfcquaxqncofqfm.stackpathdns.com/'
Now clear your cache by running
rm -fr var/cache/* and check your Symfony application asset URLs:
If you are using Symfony Flex for application management, then in your project folder, move to config >> packages folder and open
framework.yaml file. Add the same asset config as shown above.
Cloudways have partnered with StackPath (formerly known as MaxCDN) and provide one-click activation for the Cloudways CDN. Note that, for a single website, the Monthly Subscription costs $1 per 25GB of bandwidth. You can avail this for any PHP website and framework based application.