Setting up the best performance server is tricky and requires knowledge in Linux and server setups. Digital Ocean is one of the best cloud hosting providers that you can find on the internet, it will let you create a droplet with root access on it, freedom to set up everything you think of, including a Pagespeed from Google and the latest Brotli compression, http2 protocol, as well as the latest PHP version.
Pagespeed is a powerful tool from Google that will allow you to rewrite, compress and cache webpage content automatically to make your website load faster. While the Brotli is a new compression technology that will allow you to compress non-binary files much smaller than the common gzip technology. Installing the latest PHP version also has additional benefit especially in WordPress and the http2 protocol will make fewer TCP connections for all your web assets resulting in a fast loading website.
Table of Contents
- Installing NGINX Source
- Compiling and Installing NGINX Source with PageSpeed and Brotli Modules
- Setup Pagespeed
- Installing PHP 7.4.26
- Install SSL and Certbot
- Setting up PHP, Brotli and Nginx Configurations
- Adding your New Domain
- Adding SSL and HTTP2 Protocol
- Conclusion
In this guide, we will help you how to setup PageSpeed, Brotli, PHP 7.4.26 and HTTP2 protocol from Nginx Source manually using the Ubuntu server from Digital Ocean.
Step 1: Installing NGINX Source
Nginx doesn’t have a way to install Pagespeed on the fly, you need to install it from the source. As in compiling everything from scratch.
Let’s assume you have a fresh server created from DigitalOcean, we use the Ubuntu 18.04.4 LTS for this tutorial. Please create one and connect your SSH server using your favorite Command Prompt.
SSH root@YOUR_SERVER_IP_ADDRESS
To make this tutorial in order, we need to be in the same directory. Let’s go to its home directory at ~/. Run the commands below to go to the home directory.
cd ~/
First, we need to update and upgrade your server repository.
sudo apt-get update
sudo apt-get upgrade
Next, we install the Debian package development tools.
sudo apt-get install dpkg-dev
sudo apt-get install uuid-dev
We will also download the NGINX source code so we can compile it, and add the mod version of Pagespeed and Brotli modules. Let’s get the apt-key so we can pull it.
sudo wget -qO - https://nginx.org/keys/nginx_signing.key | sudo apt-key add -
Edit the sources.list using vim to include the NGINX repository. (If you don’t know how to add lines on vim, just type i)
vim /etc/apt/sources.list
Add the following lines on the bottom of the file /etc/apt/sources.list.
deb https://nginx.org/packages/mainline/ubuntu focal nginx
deb-src https://nginx.org/packages/mainline/ubuntu focal nginx
For other version of ubuntu use the following instead of focal: (eg: deb https://nginx.org/packages/mainline/ubuntu groovy nginx)
- Ubuntu 20.10: groovy
- Ubuntu 20.04.1 LTS: focal
- Ubuntu 18.04.5 LTS: bionic
- Ubuntu 16.04.7 LTS: xenial
- Ubuntu 14.04.6 LTS: trusty
- Ubuntu 12.04.5 LTS: precise
or simply enter on CMD to check your release name: lsb_release -cs
Save and exit. (If you don’t know how to save and exit in vim, just press escape and type :wq
)
We will run the update again to get the repository.
sudo apt-get update
If you get an error like “Skipping acquire of configured file ‘nginx/binary-i386/Packages’ as repository ‘https://nginx.org/packages/mainline/ubuntu focal InRelease’ doesn’t support architecture ‘i386′”, this is because you’re using different architecture. However, you can use amd64 in it. Edit /etc/apt/sources.list
and replace it with this.
deb [arch=amd64] https://nginx.org/packages/mainline/ubuntu focal nginx
deb-src https://nginx.org/packages/mainline/ubuntu focal nginx
Now, we can install the Nginx build tool and download its source.
sudo apt-get build-dep nginx
sudo apt-get source nginx
This will create some files at the home directory and a folder for nginx-1.25.3.
Step 2: Compiling and Installing NGINX Source with PageSpeed and Brotli Modules
Since we already have the Nginx Source, that is placed under the home directory at ~/nginx-1.25.3, we can now proceed on adding the Pagespeed and Brotli module. (For us not to get confused, we also include the full path in every command below)
Now, let’s create a module directory under the Debian folder and access it.
mkdir ~/nginx-1.25.3/debian/modules
cd ~/nginx-1.25.3/debian/modules
Once inside, we have to download the Pagespeed Source from Github.
wget https://github.com/apache/incubator-pagespeed-ngx/archive/v1.13.35.2-stable.zip
Install unzip and unzip the Pagespeed source zip.
sudo apt-get install unzip
unzip v1.13.35.2-stable.zip
We need to rename the Pagespeed directory to ngx_pagespeed.
sudo mv incubator-pagespeed-ngx-1.13.35.2-stable ngx_pagespeed
There are error on newer version of nginx 1.23, regarding the “elts error” because of the cache-control are re-implemented. To fix this one, you need to replace the ngx_pagespeed.cc with the latest patch.
curl https://raw.githubusercontent.com/apache/incubator-pagespeed-ngx/dea38db4cb7370a9bbb06139798a4936be7553db/src/ngx_pagespeed.cc > ~/nginx-1.25.3/debian/modules/ngx_pagespeed/src/ngx_pagespeed.cc
Then next, we need to install the PSOL compiler for Pagespeed under the ngx_pagespeed folder that we just renamed.
cd ngx_pagespeed
sudo wget https://dl.google.com/dl/page-speed/psol/1.13.35.2-x64.tar.gz
Decompress the PSOL file.
sudo tar -xzvf 1.13.35.2-x64.tar.gz
Alternatively, you can also experiment and use the latest non-stable version of Pagespeed (Just changed the URL and directory above instruction):
- PageSpeed: https://github.com/apache/incubator-pagespeed-ngx/archive/v1.14.33.1-RC1.zip
- PSOL: https://dist.apache.org/repos/dist/release/incubator/pagespeed/1.14.36.1/x64/psol-1.14.36.1-apache-incubating-x64.tar.gz
We are done with Pagespeed, let’s download the source for Brotli. We have to go back to the modules directory we created earlier.
cd ~/nginx-1.25.3/debian/modules
Download the Brotli source from Github.
git clone --recursive https://github.com/google/ngx_brotli
We are done downloading the Pagespeed and Brotli source, we can now edit the configure file for our Nginx compiler. To do this, we have to edit the ~/nginx-1.25.3/debian/rules.
cd ~/nginx-1.25.3
vim ~/nginx-1.25.3/debian/rules
Find the line that says ./configure --prefix/etc/nginx
, somewhere in the line 41 and line 46, then add the following string for both config.env.nginx and config.env.nginx_debug.
--add-module="$(CURDIR)/debian/modules/ngx_pagespeed" --add-module="$(CURDIR)/debian/modules/ngx_brotli"
The result of the edit should look like:
config.status.nginx: config.env.nginx
cd $(BUILDDIR_nginx) && \
CFLAGS="" ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --add-module="$(CURDIR)/debian/modules/ngx_pagespeed" --add-module="$(CURDIR)/debian/modules/ngx_brotli" --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt="$(CFLAGS)" --with-ld-opt="$(LDFLAGS)"
touch $@
config.status.nginx_debug: config.env.nginx_debug
cd $(BUILDDIR_nginx_debug) && \
CFLAGS="" ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --add-module="$(CURDIR)/debian/modules/ngx_pagespeed" --add-module="$(CURDIR)/debian/modules/ngx_brotli" --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt="$(CFLAGS)" --with-ld-opt="$(LDFLAGS)" --with-debug
touch $@
Save the file.
The next step is to change the version of our modded Nginx, this will avoid the automatic update from NGINX that will deactivate the module we added. You can edit it at the ~/nginx-1.25.3/debian/changelog.
vim ~/nginx-1.25.3/debian/changelog
Then just replace the first line with the following info. (Note: the space between the email and date should be two spaces or else you’ll get an error message, eg. (@domain.com”> Tue, 17 Feb)”.
nginx (1.19.6-1~focal+pagespeed+brotli) focal; urgency=low
* 1.19.6-1
-- YOUR_NAME <your_name@domain.com> Tue, 24 Nov 2020 16:02:03 +0300
We also have to edit the NGINX name and version at ~/nginx-1.25.3/src/core/nginx.h.
vim ~/nginx-1.25.3/src/core/nginx.h
Then edit the NGINX_VER with your own mod version. example:
#define nginx_version 1019000
#define NGINX_VERSION "1.19.6"
#define NGINX_VER "YOUR_NAME nginx/" NGINX_VERSION
Save the file.
We can now compile the NGINX source. Go back to ~/nginx-1.25.3/ folder and compile it.
cd ~/nginx-1.25.3/
sudo dpkg-buildpackage -b
If you’re prompt for PSOL’s debug compilation, just type yes.
If you got an error saying “gpg: dpkg-sign.Jl4yp2Hj/nginx_1.19.6-1~focal+pagespeed+brotli_amd64.buildinfo: clear-sign failed: No secret key”, the cause of this error is when you change the email address in changelog file, to remove this error, use this command instead when compiling sudo dpkg-buildpackage -b -uc -us
Go back to the home directory to access the compiled deb file.
cd ~/
Finally, we can now install the compiled deb file for our server.
sudo dpkg -i nginx_1.21.4-1~focal+pagespeed+brotli_amd64.deb
To test that your Nginx is working, you can run this command.
nginx -t
If success, you should get:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
When you upgrade your system, if a new version is found, nginx will be included in the upgrade list and since we are using an custom nginx, this one will be replaced with the official nginx files which removes the pagespeed and brotli. For that not to happen, we need to hold the update for nginx. To do that, run this command:
sudo apt-mark hold nginx
If you want to see all of the hold programs in your system, just run this command:
sudo dpkg --get-selections | grep "hold"
Step 3: Setup Pagespeed
The next step we need to do is to run the Pagespeed we added in Nginx. Here are some helpful vim settings you might want to activate before eding the nginx configuration. Just create this file at home directory, vim ~/.vimrc
. Then add the following:
set tabstop=4
set shiftwidth=4
set expandtab
First, we have to create a cache folder for the Pagespeed. We have to assign the www-data as a user for that folder so our Nginx can access it.
sudo mkdir -p /var/ngx_pagespeed_cache
sudo chown -R www-data:www-data /var/ngx_pagespeed_cache
Let’s turn on pagespeed in the nginx configuration file.
sudo vim /etc/nginx/nginx.conf
Add the following at the http{.... }
blocks. (You can actually add the brotli config here, but since we don’t have SSL and a domain yet, it’s useless for the meantime, we can add it after we set up the SSL and domain)
pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;
Restart Nginx.
sudo service nginx restart
Now, open a browser and access the http://YOUR_SERVER_ IP_ ADDRESS. The response header should look like this in Google Chrome Devtool.
x-page-speed: 1.13.35.2-0
server: YOUR_NAME nginx/1.19.6
It’s too long to discuss how to see this in Devtool in Google Chrome, you can also check this by running this command:
curl -s -I -X GET http://localhost
The output in command should have the X-Page-Speed and the changes we made in the NGINX version:
HTTP/1.1 200 OK
Server: YOUR_NAME nginx/1.19.6
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
ETag: "5e4a58d7-10"
Date: Tue, 18 Feb 2020 18:36:44 GMT
X-Page-Speed: 1.13.35.2-0
Cache-Control: max-age=0, no-cache
Step 4: Installing PHP 7.4.26
To install the latest version of PHP, we have to add the repository maintained by Ondrej. Add the following repo to your server, then update and upgrade.
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt upgrade
Install the latest version php7.4, the repo will automatically install the apache server, in order for it to not install we added these parameters to ignore it apache2- apache2-bin-
.
sudo apt-get install php7.4 apache2- apache2-bin-
Install some necessary files that are needed for most web apps like WordPress.
sudo apt-get install php7.4-cli php7.4-fpm php7.4-pdo php7.4-mysql php7.4-zip php7.4-mbstring php7.4-curl php7.4-xml php7.4-bcmath php7.4-json
(Optional) If you’re using WordPress and want to enable the image editor. Install the Imagic module.
sudo apt-get install php7.4-imagick
To check if the php7.4-fpm that is needed for NGINX is properly working. Restart the fpm and check its version.
sudo service php7.4-fpm restart
php-fpm7.4 -v
(Optional) To save you some trouble in the future, you may edit these important php.ini configurations.
vim /etc/php/7.4/fpm/php.ini
Then find and edit the following configuration. (To search in vim, just type escape and type ?YOUR_KEYWORD
. Press enter to edit it.)
upload_max_filesize = 150M
post_max_size = 150M
max_execution_time = 300
max_input_vars = 8000
session.name = create_your_own_session_id (default: PHPSESSID)
It is best to change the default session name to avoid brute force attack.
Step 5: Install SSL and Certbot
For our step to run smoothly, let’s install the Certbot for our SSL for later use. This is a free SSL certificate for your website. The step for installing one for your website will be discussed in the later step.
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python3-certbot-nginx
If you’re using Ubuntu 20.04.2 LTS and above, you no longer need to add the repository sudo add-apt-repository ppa:certbot/certbot
because the repo is already included in the OS. Or else you’ll get this error :
E: The repository 'http://ppa.launchpad.net/certbot/certbot/ubuntu focal Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
Step 6: Setting up PHP, Brotli and Nginx Configurations
Since we have now a working Pagespeed, Certbot and PHP, the last couple of things we need to do is to clean up our Nginx conf folder and set our web directory to /var/www. (The PHP configuration that comes from the Ondrej repository does not include a configuration for the NGINX web server, we have to manually add it in order to work)
First, we create the /var/www
folder for our web directories.
mkdir /var/www
sudo chown -R www-data:www-data /var/www
Remove the file the comes from NGINX default configuration, we don’t need it. Then add a snippets folder for our PHP-fpm configs.
rm /etc/nginx/conf.d/default.conf
mkdir /etc/nginx/snippets
Now, let’s create the fastcgi-php config that is needed for PHP-fpm to work for our NGINX.
vim /etc/nginx/snippets/fastcgi-php.conf
Add the following lines. (PHPcache comments out, we don’t need it since we already have the Pagespeed module. In case you need it, just uncomment it)
#split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;
# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi_params;
##
# FastCGI PHP connection
##
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_read_timeout 300;
##
# FastCGI Cache
##
#fastcgi_cache phpcache;
#fastcgi_cache_valid 200 60m;
#fastcgi_cache_methods GET HEAD;
#add_header X-Fastcgi-Cache $upstream_cache_status;
Then we also need to edit the fastcgi_params and this common pitfall param that will work on most php applications.
vim /etc/nginx/fastcgi_params
Add fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
in the first line.
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
Then we need to re-edit the vim /etc/nginx/nginx.conf
we edited earlier, just replace it with the lines below. We don’t need to declare the gzip on the nginx.conf because the Pagespeed tends to compress the cache non-binary files into gzip. In order for Pagespeed to use Brotli compression, do not declare gzip at all. No worries, it will still compress content to gzip for the older browser.
user www-data;
worker_processes auto;
include /etc/nginx/modules-enabled/*.conf;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# PageSpeed Settings
##
pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;
##
# Access/Error Log Settings
##
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log;
##
# Http Core Module Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
##
# Gzip Settings
##
pagespeed FetchWithGzip off;
pagespeed HttpCacheCompressionLevel 0;
#gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/x-font-ttf application/x-web-app-manifest+json application/xml+rss text/javascript image/svg+xml image/x-icon;
##
# Brotli Settings
##
brotli on;
brotli_comp_level 6;
brotli_static on;
brotli_types application/octec-stream text/xml image/svg+xml application/x-font-ttf image/vnd.microsoft.icon application/x-font-opentype application/json font/eot application/vnd.ms-fontobject application/javascript font/otf application/xml application/xhtml+xml text/javascript application/x-javascript text/plain application/x-font-trutype application/xml+rss image/x-icon font/opentype text/css image/x-win-bitmap application/x-web-app-manifest+json;
##
# SSL Configuration
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# FastCGI Cache Settings
##
fastcgi_cache_path /etc/nginx-cache levels=1:2 keys_zone=phpcache:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_ignore_headers Cache-Control Expires;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Next, we need to set up a sites-available and sites-enabled directory. This is ideal for SSL Certbot to automatically add their own rules to the conf files.
mkdir /etc/nginx/sites-available
ln -s /etc/nginx/sites-available /etc/nginx/sites-enabled
If you want to assign /var/www as the root folder for your IP address on the web, we can create a default directory in sites-available.
vim /etc/nginx/sites-available/default
Add the following lines.
server{
listen 80 default_server;
server_name myserver.com;
root /var/www;
index index.php index.html index.html index.nginx-debian.html;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
}
location ~ /\.ht {
deny all;
}
}
To see it in action, let’s add some files in /var/www
.
vim /var/www/test.php
Then add the following codes:
<?php
phpinfo();
?>
Make sure to change owner the www directory again and assign www-data as user for Nginx to access it. If not, it will show a forbidden error when accessing it in the browser.
sudo chown -R www-data:www-data /var/www
Lastly, restart nginx.
sudo service nginx restart
You can now view your server on the web with PHP in action and with actual Pagespeed running on it. Access it at http://YOUR_SERVER_IP_ADDRESS/test.php.
Step 7: Adding your New Domain
To add a domain in your NGINX web server. We need to create a web directory and Nginx configuration for it.
First, we create a web directory dedicated to our new domain. For example, we create one for yourdomain.com.
mkdir /var/www/yourdomain.com
sudo chown -R www-data:www-data /var/www
Create a conf file for the new domain in the sites-available directory.
vim /etc/nginx/sites-available/yourdomain.com
Add these configurations. (Make sure you entered the correct domain name in server_name yourdomain.com www.yourdomain.com line or else Certbot will add a configuration to your default conf)
server{
listen 80;
root /var/www/yourdomain.com;
index index.php index.html index.html index.nginx-debian.html;
server_name yourdomain.com www.yourdomain.com;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
}
location ~ /\.ht {
deny all;
}
}
Save it and restart the Nginx.
nginx -t
sudo service nginx restart
Now, we have to add some files to our new domain.
vim /var/www/yourdomain.com/index.php
Let’s just print the PHP info.
<?php
phpinfo();
?>
Don’t forget to change the owner of the directory.
sudo chown -R www-data:www-data /var/www
You can now access your new domain on the web. Try it.
Step 8: Adding SSL and HTTP2 Protocol
We have our new domain added on our web server already. The remaining step is to add SSL and the newest HTTP2 protocol on it.
To install the SSL, we have to run the Certbot command. Make sure to include the www version of your website.
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
Once it’s finished, it will add its configuration automatically in the conf files. Let’s open it so we can add the http2 protocol configuration.
vim /etc/nginx/sites-available/yourdomain.com
To enable http2 on it and make our code clean. Let’s uncomment the line listen 443 ssl
added by the Certbot. And add it on the top instead with additional http2 on it. The final conf files should look like below.
server{
listen 443 ssl http2;
listen [::]:443 ssl http2;
root /var/www/yourdomain.com;
index index.php index.html index.html index.nginx-debian.html;
server_name yourdomain.com www.yourdomain.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
}
#listen 443 ssl http2; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server{
if ($host = www.codefaq.org) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = codefaq.org) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 404; # managed by Certbot
}
Save it and restart the Nginx.
sudo service nginx restart
Access your new website at https://yourdomain.com and the response header in Google Devtool should include Br, Pagespeed and the newest http2 protocol. You can also check it in console with the following command.
curl -s -I -H "accept-encoding: gzip, deflate, br" -X GET https://yourdomain.com
The output should show content-encoding: br
.
HTTP/2 200
server: YOUR_NAME nginx/1.19.6
content-type: text/html; charset=UTF-8
vary: Accept-Encoding
vary: Accept-Encoding, Cookie
date: Tue, 18 Feb 2020 19:13:22 GMT
x-page-speed: 1.13.35.2-0
cache-control: max-age=0, no-cache
content-encoding: br
To see if gzip are still enabled for an older browser, just run.
curl -s -I -H "accept-encoding: gzip, deflate" -X GET https://yourdomain.com
The output should show content-encoding: gzip
.
HTTP/2 200
server: YOUR_NAME nginx/1.19.6
content-type: text/html; charset=UTF-8
vary: Accept-Encoding
vary: Accept-Encoding, Cookie
date: Tue, 18 Feb 2020 19:14:53 GMT
x-page-speed: 1.13.35.2-0
cache-control: max-age=0, no-cache
content-encoding: gzip
Conclusion
That’s it! You just create your own server in the Digital Ocean with Pagespeed and Brotli module straight from the NGINX source. And you also learn how to install PHP, http2 protocol and how to get a free SSL for your domain.
Lastly, if you’d like to install MySQL, just run the following commands.
sudo apt-get install mysql-server
sudo mysql_secure_installation
In case there is a new update for NGINX, Brotli or Pagepeed, just repeat the process on step 1 and step 2. If the update is for the NGINX, you need to increment the current version you use. This will allow you to install an update on your current NGINX.
If you need to flush the Pagespeed cache, here is a handy command to refresh it.
rm -rf /var/ngx_pagespeed_cache/*
Good luck! If this post helps you, please do comment down below.
Error on Current Version of Ubuntu
There are new error on compiling Nginx on Ubuntu because of the new updates that says the following:
-o objs/addon/src/ngx_server_context.o \
/root/nginx-1.23.3/debian/modules/ngx_pagespeed/src/ngx_server_context.cc
/root/nginx-1.23.3/debian/modules/ngx_pagespeed/src/ngx_pagespeed.cc: In function ‘ngx_int_t net_instaweb::{anonymous}::ps_set_cache_control(ngx_http_request_t*, char*)’:
/root/nginx-1.23.3/debian/modules/ngx_pagespeed/src/ngx_pagespeed.cc:407:36: error: request for member ‘elts’ in ‘r->ngx_http_request_s::headers_out.ngx_http_headers_out_t::cache_control’, which is of pointer type ‘ngx_table_elt_t*’ {aka ‘ngx_table_elt_s*’} (maybe you meant to use ‘->’ ?)
407 | if (r->headers_out.cache_control.elts == NULL) {
| ^~~~
/root/nginx-1.23.3/debian/modules/ngx_pagespeed/src/ngx_pagespeed.cc:408:35: error: cannot convert ‘ngx_table_elt_t**’ {aka ‘ngx_table_elt_s**’} to ‘ngx_array_t*’
408 | ngx_int_t rc = ngx_array_init(&r->headers_out.cache_control, r->pool,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| ngx_table_elt_t** {aka ngx_table_elt_s**}
In file included from src/core/ngx_core.h:65,
from src/http/ngx_http.h:13,
from /root/nginx-1.23.3/debian/modules/ngx_pagespeed/src/ngx_pagespeed.h:33,
from /root/nginx-1.23.3/debian/modules/ngx_pagespeed/src/ngx_pagespeed.cc:26:
src/core/ngx_array.h:32:29: note: initializing argument 1 of ‘ngx_int_t ngx_array_init(ngx_array_t*, ngx_pool_t*, ngx_uint_t, size_t)’
32 | ngx_array_init(ngx_array_t *array, ngx_pool_t *pool, ngx_uint_t n, size_t size)
| ~~~~~~~~~~~~~^~~~~
/root/nginx-1.23.3/debian/modules/ngx_pagespeed/src/ngx_pagespeed.cc:415:22: error: cannot convert ‘ngx_table_elt_t**’ {aka ‘ngx_table_elt_s**’} to ‘ngx_array_t*’
415 | ngx_array_push(&r->headers_out.cache_control));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| ngx_table_elt_t** {aka ngx_table_elt_s**}
In file included from src/core/ngx_core.h:65,
from src/http/ngx_http.h:13,
from /root/nginx-1.23.3/debian/modules/ngx_pagespeed/src/ngx_pagespeed.h:33,
from /root/nginx-1.23.3/debian/modules/ngx_pagespeed/src/ngx_pagespeed.cc:26:
src/core/ngx_array.h:27:35: note: initializing argument 1 of ‘void* ngx_array_push(ngx_array_t*)’
27 | void *ngx_array_push(ngx_array_t *a);
| ~~~~~~~~~~~~~^
/root/nginx-1.23.3/debian/modules/ngx_pagespeed/src/ngx_pagespeed.cc: In function ‘bool net_instaweb::{anonymous}::ps_get_cache_control(ngx_http_request_t*, GoogleString*)’:
/root/nginx-1.23.3/debian/modules/ngx_pagespeed/src/ngx_pagespeed.cc:439:74: error: request for member ‘elts’ in ‘r->ngx_http_request_s::headers_out.ngx_http_headers_out_t::cache_control’, which is of pointer type ‘ngx_table_elt_t*’ {aka ‘ngx_table_elt_s*’} (maybe you meant to use ‘->’ ?)
439 | auto ccp = static_cast<ngx_table_elt_t**>(r->headers_out.cache_control.elts);
| ^~~~
/root/nginx-1.23.3/debian/modules/ngx_pagespeed/src/ngx_pagespeed.cc:444:59: error: request for member ‘nelts’ in ‘r->ngx_http_request_s::headers_out.ngx_http_headers_out_t::cache_control’, which is of pointer type ‘ngx_table_elt_t*’ {aka
ngx_table_elt_s*’} (maybe you meant to use ‘->’ ?)
444 | for (ngx_uint_t i = 0; i < r->headers_out.cache_control.nelts; i++) {
| ^~~~~
make[2]: *** [objs/Makefile:2005: objs/addon/src/ngx_pagespeed.o] Error 1
make[2]: *** Waiting for unfinished jobs....
make[2]: Leaving directory '/root/nginx-1.23.3/debian/build-nginx'
make[1]: *** [Makefile:10: build] Error 2
make[1]: Leaving directory '/root/nginx-1.23.3/debian/build-nginx'
make: *** [debian/rules:52: build-arch.nginx] Error 2
dpkg-buildpackage: error: debian/rules build subprocess returned exit status 2