$1 CloudwaysCDN- Get Global Reach for your PHP Applications
Tags: php, cdn, symfony, dev
Published: 2017-10-13 14:49:24

---
title: $1 CloudwaysCDN- Get Global Reach for your PHP Applications
published: true
description: Symfony CDN serves static content of your website from the nearest geographical location. The result is a significant increase in user experience.
tags: php,cdn,symfony,dev
cover_image: http://www.cloudways.com/blog/wp-content/uploads/Leverage-the-Power-of-CDN-in-Symfony-3-Applications.png
---

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.

##Prerequisites

- [Cloudways server with PHP application](https://www.cloudways.com/blog/host-php-on-digitalocean/)
- [Symfony installed on the server](https://www.cloudways.com/blog/install-symfony-3-on-cloud/) (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.

![](http://www.cloudways.com/blog/wp-content/uploads/php-server-stack-1-2-2.gif)


First of all, let’s talk about CDN, and why you need to integrate it into PHP projects.

##What is CDN ?

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.

![](https://www.cloudways.com/blog/wp-content/uploads/cdn-structure-768x329.png)


##Why CDN is Necessary?

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.

##Enable CDN for PHP Apps on Cloudways

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.

![](https://www.cloudways.com/blog/wp-content/uploads/cdn-config-768x351.png)


Next you will get the newly generated URL for you Symfony application:


![](https://www.cloudways.com/blog/wp-content/uploads/cdn-app-url-768x350.png)


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.

##Integrate CDN URL in Application Assets

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:

```
<img src="<?php echo CDN ?>images/portfolio/big/css.png"/>
<script type="text/javascript" src="<?php echo CDN ?>js/modernizr.custom.26633.js"></script>

```
The URL generated for the assets will look like this now:

```
<script type="text/javascript" src="<?php echo CDN ?>https://3971-7889-2-raikfcquaxqncofqfm.stackpathdns.com/js/modernizr.custom.26633.js"></script>
```
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 `config.yml`.

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](https://www.cloudways.com/blog/using-symfony-flex/), then in your project folder, move to **config >> packages** folder and open `framework.yaml` file. Add the same asset config as shown above.


##Conclusion

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.