concrete5 with Apache and NGINX on Ubuntu 18.04

Introduction

NGINX is great at serving static files, it uses less resources than Apache.
On the other hand, Apache offers more features.

It is possible to have both running, so that NGINX serves static files (technically as a reverse proxy) and Apache execute PHP scripts.

NGINX will be the publicly visible webserver, whereas Apache will only be visible by NGINX at the port .

In this document I describe how to setup such environment on Ubuntu 18.04, including all the required steps to have a running concrete5 8.3+ installation available at /var/www//.

Preparation

First of all, let’s update all the Ubuntu packages:

sudo apt update
sudo apt upgrade -y

Installing NGINX

Installing NGINX is as easy as running

sudo apt install -y nginx

Next we remove the configuration of the default NGINX website:

sudo rm /etc/nginx/sites-enabled/default

Adding the site to NGINX

Then we create the NGINX configuration for the website typing this command:

sudo nano /etc/nginx/sites-available/

And here’s the content of this new file:

server {
    listen 80;
    root /var/www///;
    index index.php index.html index.htm;
    server_name
        www.
        
    ;
    location ~ /\.ht {
        deny all;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location ~ \.php$ {
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:$request_uri;
        proxy_read_timeout 600;
    }
}

Next we enable this NGINX site:

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

Installing Apache

Installing Apache is as easy as running

sudo apt install -y apache2

During the installation you may see an error stating that apache can’t be started: ignore it (it’s because Apache tries to start using port 80, which is already taken by NGINX).

Let’s disable the default Apache website:

sudo a2dissite 000-default

In order to have pretty URLs in concrete5, we need to enable the rewrite Apache module:

sudo a2enmod rewrite

Next we have to prevent Apache from listening on port 80:

sudo nano /etc/apache2/ports.conf

and comment all the lines.

Adding the site to Apache

To create the Apache configuration for the website, type this command:

sudo nano /etc/apache2/sites-available/.conf

And use this content for this new file:

Listen 127.0.0.1:
<VirtualHost 127.0.0.1:>
	ServerAdmin webmaster@localhost
	DocumentRoot /var/www//
	ErrorLog ${APACHE_LOG_DIR}/.error.log
	CustomLog ${APACHE_LOG_DIR}/.access.log combined
</VirtualHost>
<Directory "/var/www//">
        Options +FollowSymLinks -SymLinksIfOwnerMatch
        AllowOverride All
</Directory>

Finally we enable this new Apache site:

sudo a2ensite 

Installing PHP

concrete5 requires PHP and a few PHP extensions. To install them type this command:

sudo apt install -y php libapache2-mod-php php-bz2 php-curl php-gd php-intl php-json php-mbstring php-mysql php-opcache php-xml php-zip

Activating the configuration

To instruct NGINX and Apache to reload their configuration, simply type these two commands:

sudo service apache2 restart
sudo service nginx restart

Configuring concrete5

You have to tell concrete5 that it’s behind a proxy. This can be easily done by typing this command:

sudo nano /var/www///application/config/concrete.php

and setting the content if this new file to:

<?php

return [
    'security' => [
        'trusted_proxies' => [
            'ips' => [
                '127.0.0.1',
                '::1',
            ],
            'headers' => -1,
        ],
    ],
];

and finally be sure that Apache can handle this new file:

sudo chown www-data:www-data /var/www///application/config/concrete.php 

HTTPS

In order to have a website accessible via HTTPS, simply configure NGINX.