Atlantic.Net Blog

How to Install and Configure Caddy Web Server with PHP on Oracle Linux 8

Hitesh Jethva
by Atlantic.Net (446 posts) under Dedicated Server Hosting, Tutorials
0 Comments

Caddy is a free, open-source web server used to host websites and applications on the internet. It is a simple, lightweight, modern web server written in the Go language. Caddy can be used as a file server, dynamic server, and scalable reverse proxy. It offers a lot of plugins that help you to extend the server functionality. Caddy offers a rich set of features including HTTP/2 support, virtual hosting support, Let’s Encrypt SSL support, a dependency-free codebase, and more.

In this post, we will show you how to install the Caddy web server with PHP support on Oracle Linux 8.

Prerequisites

  • A server running Oracle Linux 8 on the Atlantic.Net Cloud Platform
  • A root password configured on your server

Step 1 – Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server. Create a new server, choosing Oracle Linux 8 as the operating system with at least 2GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are logged in to your server, run the following command to update your base system with the latest available packages.

dnf update -y

Step 2 – Install Caddy Web Server on Oracle Linux 8

By default, the Caddy package is not included in the Oracle Linux 8 default repo, so you will need to enable the special repository on your system.

dnf install 'dnf-command(copr)'
dnf copr enable @caddy/caddy

Once the repository is enabled, you can install the Caddy web server with the following command:

dnf install caddy -y

Once Caddy is installed, verify the Caddy version using the following command:

caddy version

Sample output:

v2.5.1 h1:bAWwslD1jNeCzDa+jDCNwb8M3UJ2tPa8UZFFzPVmGKs=

Next, start the Caddy service and enable it to start at system reboot:

systemctl start caddy
systemctl enable caddy

You can also check the status of the Caddy service using the following command:

systemctl status caddy

You should see the following output:

● caddy.service - Caddy
   Loaded: loaded (/usr/lib/systemd/system/caddy.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2022-06-28 07:07:28 EDT; 7s ago
     Docs: https://caddyserver.com/docs/
 Main PID: 1784 (caddy)
    Tasks: 5 (limit: 11409)
   Memory: 14.5M
   CGroup: /system.slice/caddy.service
           └─1784 /usr/bin/caddy run --environ --config /etc/caddy/Caddyfile

Jun 28 07:07:28 oraclelinux8 caddy[1784]: JOURNAL_STREAM=9:27345
Jun 28 07:07:28 oraclelinux8 caddy[1784]: {"level":"info","ts":1656414448.1275246,"msg":"using provided configuration","config_file":"/etc/ca>
Jun 28 07:07:28 oraclelinux8 caddy[1784]: {"level":"info","ts":1656414448.1344466,"logger":"admin","msg":"admin endpoint started","address":">
Jun 28 07:07:28 oraclelinux8 caddy[1784]: {"level":"warn","ts":1656414448.1346254,"logger":"http","msg":"server is listening only on the HTTP>
Jun 28 07:07:28 oraclelinux8 caddy[1784]: {"level":"info","ts":1656414448.1351585,"msg":"autosaved config (load with --resume flag)","file":">
Jun 28 07:07:28 oraclelinux8 caddy[1784]: {"level":"info","ts":1656414448.1352255,"msg":"serving initial configuration"}
Jun 28 07:07:28 oraclelinux8 caddy[1784]: {"level":"info","ts":1656414448.135435,"logger":"tls.cache.maintenance","msg":"started background c>
Jun 28 07:07:28 oraclelinux8 caddy[1784]: {"level":"info","ts":1656414448.1354668,"logger":"tls","msg":"cleaning storage unit","description":>
Jun 28 07:07:28 oraclelinux8 caddy[1784]: {"level":"info","ts":1656414448.1354911,"logger":"tls","msg":"finished cleaning storage units"}
Jun 28 07:07:28 oraclelinux8 systemd[1]: Started Caddy.

Now, open your web browser and access the Caddy web server using the URL http://your-server-ip. You should see the Caddy test page on the following screen:
Caddy test page

Step 3 – Create a Simple Website Using Caddy

First, create a directory structure for the new website using the following command:

mkdir -p /var/www/example.com
mkdir /var/log/caddy

Next, set proper ownership to the new website:

chown caddy:caddy /var/www/example.com
chown caddy:caddy /var/log/caddy

Next, create an index.html page for a new website:

nano /var/www/example.com/index.html

Add the following HTML codes:

<!DOCTYPE html>
<html>
<head>
<title>Caddy Web Server</title>
</head>
<body>
<h1>Congratulations! Caddy Web Server Works on Oracle Linux 8</h1>
</body>
</html>

Save and close the file when you are finished.

Step 4 – Create a Virtual Host Configuration File

Next, you will need to edit the Caddy default configuration file to host the new website. You can edit it with the following command:

nano /etc/caddy/Caddyfile

Remove all lines and add the following lines:

test.example.com:80 {
        root * /var/www/example.com
        file_server
        encode gzip

        log {
             	output file /var/log/caddy/example.access.log
        }

	@static {
                file
                path *.ico *.css *.js *.gif *.jpg *.jpeg *.png *.svg *.woff *.pdf *.webp
        }
	header @static Cache-Control max-age=5184000

}

Save and close the file when you are finished. Next, validate the Caddy configuration file:

caddy validate --adapter caddyfile --config /etc/caddy/Caddyfile

You should see the following output:

2022/06/28 11:11:49.935	INFO	using provided configuration	{"config_file": "/etc/caddy/Caddyfile", "config_adapter": "caddyfile"}
2022/06/28 11:11:49.938	WARN	Caddyfile input is not formatted; run the 'caddy fmt' command to fix inconsistencies	{"adapter": "caddyfile", "file": "/etc/caddy/Caddyfile", "line": 2}
2022/06/28 11:11:49.938	WARN	http	server is listening only on the HTTP port, so no automatic HTTPS will be applied to this server	{"server_name": "srv0", "http_port": 80}
2022/06/28 11:11:49.939	INFO	tls.cache.maintenance	started background certificate maintenance	{"cache": "0xc0005b4380"}
2022/06/28 11:11:49.939	INFO	tls.cache.maintenance	stopped background certificate maintenance	{"cache": "0xc0005b4380"}
Valid configuration

Next, restart the Caddy service to apply the changes:

systemctl restart caddy

You can also check the Caddy service using the following command:

systemctl status caddy

You will get the following output:

● caddy.service - Caddy
   Loaded: loaded (/usr/lib/systemd/system/caddy.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2022-06-28 07:12:22 EDT; 8s ago
     Docs: https://caddyserver.com/docs/
 Main PID: 1812 (caddy)
    Tasks: 5 (limit: 11409)
   Memory: 12.6M
   CGroup: /system.slice/caddy.service
           └─1812 /usr/bin/caddy run --environ --config /etc/caddy/Caddyfile

Jun 28 07:12:22 oraclelinux8 caddy[1812]: {"level":"info","ts":1656414742.7249784,"msg":"using provided configuration","config_file":"/etc/ca>
Jun 28 07:12:22 oraclelinux8 caddy[1812]: {"level":"warn","ts":1656414742.7270412,"msg":"Caddyfile input is not formatted; run the 'caddy fmt>
Jun 28 07:12:22 oraclelinux8 caddy[1812]: {"level":"info","ts":1656414742.7286003,"logger":"admin","msg":"admin endpoint started","address":">
Jun 28 07:12:22 oraclelinux8 caddy[1812]: {"level":"warn","ts":1656414742.728899,"logger":"http","msg":"server is listening only on the HTTP >
Jun 28 07:12:22 oraclelinux8 caddy[1812]: {"level":"info","ts":1656414742.7295816,"msg":"autosaved config (load with --resume flag)","file":">
Jun 28 07:12:22 oraclelinux8 caddy[1812]: {"level":"info","ts":1656414742.729656,"msg":"serving initial configuration"}
Jun 28 07:12:22 oraclelinux8 systemd[1]: Started Caddy.
Jun 28 07:12:22 oraclelinux8 caddy[1812]: {"level":"info","ts":1656414742.7376888,"logger":"tls.cache.maintenance","msg":"started background >
Jun 28 07:12:22 oraclelinux8 caddy[1812]: {"level":"info","ts":1656414742.7378275,"logger":"tls","msg":"cleaning storage unit","description":>
Jun 28 07:12:22 oraclelinux8 caddy[1812]: {"level":"info","ts":1656414742.737864,"logger":"tls","msg":"finished cleaning storage units"}

Now, open your web browser and access the Caddy website using the URL http://test.example.com. You should see your website on the following screen:
Caddy sample website test page

Step 5 – Enable PHP Support on Caddy Web Server

To enable PHP support on Caddy, PHP must be installed on your server. You can install PHP and other required extensions using the following command:

dnf install php-fpm php-cli php-gd -y

Once all the packages are installed, edit the Caddy configuration file:

nano /etc/caddy/Caddyfile

Define the php_fastcgi location as shown below:

test.example.com:80 {
        root * /var/www/example.com
	php_fastcgi unix//run/php-fpm/www.sock
        file_server
        encode gzip

        log {
             	output file /var/log/caddy/example.access.log
        }

	@static {
                file
                path *.ico *.css *.js *.gif *.jpg *.jpeg *.png *.svg *.woff *.pdf *.webp
        }
	header @static Cache-Control max-age=5184000

}

Save and close the file, then restart the Caddy service:

systemctl restart caddy

Next, edit the PHP-FPM file:

nano /etc/php-fpm.d/www.conf

Change the following lines:

user = caddy
group = caddy
listen.acl_users = apache,nginx,caddy

Save and close the file, then start the PHP-FPM service and enable it to start at system reboot:

systemctl start php-fpm
systemctl enable php-fpm

Next, create a sample info.php page:

nano /var/www/example.com/info.php

Add the following line:

<?php phpinfo();
?>

Save and close the file, then open your web browser and access the info.php page using the URL http://test.example.com/info.php. You will get the following page:
Caddy PHP test page

Conclusion

In the above post, you learned how to install Caddy with PHP on Oracle Linux 8. You can now use Caddy on your production environment to deploy a website and test its performance. Try it on dedicated hosting from Atlantic.Net!

Get A Free To Use Cloud VPS

Free Tier Includes:
G3.2GB Cloud VPS Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


Looking for a Hosting Solution?

We Provide Cloud, Dedicated, & Colocation.

  • Seven Global Data Center Locations.
  • Flexible Private, Public, & Hybrid Hosting.
  • 24x7x365 Security, Support, & Monitoring.
Contact Us Now! Med Tech Award FTC
SOC Audit HIPAA Audit HITECH Audit

Recent Posts

How to Install Sails.js Framework with Nginx as a Reverse Proxy on Oracle Linux 8
Are Data Breaches In The Cloud Getting Better Or Worse?
How to setup HTTP Strict Transport Security (HSTS) for Apache on Oracle Linux 8
How to Install Kanban Kanboard on Oracle Linux 8
How To Install Kamailio SIP Server on Oracle Linux 8

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G3.2GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


New York, NY

100 Delawanna Ave, Suite 1

Clifton, NJ 07014

United States

San Francisco, CA

2820 Northwestern Pkwy,

Santa Clara, CA 95051

United States

Dallas, TX

2008 Lookout Dr,

Dallas, Texas 75044

United States

Ashburn, VA

1807 Michael Faraday Ct,

Reston, VA 20190

United States

Orlando, FL

440 W Kennedy Blvd, Suite 3

Orlando, FL 32810

United States

Toronto, Canada

20 Pullman Ct, Scarborough,

Ontario M1X 1E4

Canada

London, UK

14 Liverpool Road, Slough,

Berkshire SL1 4QZ

United Kingdom

Resources