How to migrate from NGINX Ubuntu 20

I am not really sure what to do, other than create a brand new server and set up my site again from scratch.

I am currently using Ubuntu 20.04, with NGINX and the site is built on PHP 7.2.

Do I just install Caddy (and change config files), deactivate NGINX and then it should work?

I remember once I was prompted to update Ubuntu, and it exposed all the PHP code in plaintext whenever users visited the website.

What would be the best way to migrate to Caddy, without site issues (apart from downtime).

Or would I have to spin up a new server, and rebuild from scratch?

Here is the NGINX config file:

server {
	listen 443 ssl;
	listen [::]:443 ssl;

	root /var/www/html;
	index index.php index.html index.htm index.nginx-debian.html;
	server_name example.com;

	ssl on;
        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

	error_page 404 /404.html;

	location ~*  \.(jpg|jpeg|png|gif|ico|css|js|pdf)$ {	
		 expires 7d;
	}


        location = /404.html {
		ssi on;
		internal;
                root /var/www/html;
        }

        location / {
                #try_files $uri $uri/ =404;
		try_files $uri $uri/ $uri.html $uri.php?$query_string;
	}

        location ~ \.php$ {
		include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
		
	}

        location ~ /\.ht {
                deny all;
        }

}

#this points all subdomains to their directories automatically

server {
        listen 443;
        listen [::]:443 ssl;
        index index.php index.html index.htm index.nginx-debian.html;
        server_name "~^(?<sub>.+)\.example\.com$";

        root /var/www/html/$sub/;

	ssl on;
        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

        error_page 404 /404.html;

        location / {
                #try_files $uri $uri/ =404;
                try_files $uri $uri/ $uri.html $uri.php;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        }

	location = /404.html {
                ssi on;
                internal;
                root /var/www/html;
        }

        location ~ /\.ht {
                deny all;
	}

}

server {
    listen 80;
    listen [::]:80;

    server_name example.com;

    return 302 https://$server_name$request_uri;
}

Thanks!

Yep, it should.

Probably best if you try it locally or on a staging server first, if you’re concerned.

For your *.example.com server, are you using a wildcard certificate? If so, you’ll need to configure Caddy to use a DNS provider module. See here:

Your config would probably look something like this:

example.com {
	root /var/www/html
	encode gzip
	php_fastcgi unix//var/run/php/php7.2-fpm.sock
	file_server
}

*.example.com {
	tls {
		dns <provider> <config>
	}

	root * /var/www/html/{labels.2}
	encode gzip
	php_fastcgi unix//var/run/php/php7.2-fpm.sock
	file_server

	handle_errors {
		root * /var/www/html
		rewrite * /{http.error.status_code}.html
		file_server
	}
}

This topic was automatically closed after 30 days. New replies are no longer allowed.