Migrating from Apache to Nginx

First we need to make sure we have nginx installed

sudo apt install nginx -y
sudo apt install php-fpm -y

Converting a virtual host to nginx block


Apache section:

DocumentRoot "/var/www/html/exemple"
ServerName exemple.eu
<Directory "/var/www/html/exemple">
Allow from all
Require all granted
SSLOptions +StdEnvVars

Include /etc/letsencrypt/options-ssl-apache.conf

SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/exemple.eu/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/exemple.eu/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/exemple.eu/chain.pem</
Nginx section:

server {

root /var/www/html/exemple/; index index.php; server_name exemple.eu www.exemple.eu; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { include /etc/nginx/fastcgi_params; fastcgi_index index.php; fastcgi_pass unix:/run/php/php-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } listen 443 ssl http2; listen [::]:443 ssl http2; ssl_certificate /etc/letsencrypt/live/exemple.eu/cert.pem; ssl_certificate_key /etc/letsencrypt/live/exemple.eu/privkey.pem; include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot gzip on; gzip_disable "msie6"; gzip_vary on; gzip_proxied any; gzip_min_length 1024; gzip_comp_level 6; gzip_buffers 16 8k; gzip_http_version 1.1; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; } server { if ($host = www.exemple.eu) { return 301 https://$host$request_uri; } # managed by Certbot if ($host = exemple.eu) { return 301 https://$host$request_uri; } # managed by Certbot listen 80; listen [::]:80; server_name exemple.eu www.exemple.eu; return 404; # managed by Certbot }

Of course you have to have letsencrypt already installed. This migration was tested on Debian 11.


Leave a Reply

Your email address will not be published. Required fields are marked *