Nginx with fcgiwrap on Ubuntu 20.04
Requirements: user with root privileges or non-root user with sudo privileges.
Nginx installation
$ sudo apt update && sudo apt upgrade -y
$ sudo apt install nginx -y
$ sudo systemctl is-enabled nginx.service
$ sudo systemctl status nginx.service
## if nginx is not enabled, enable it and run ##
Important details for Nginx configuration:
- default HTTP port => 80
- default HTTPS port => 443
- user and group for Nginx server => www-data
- Nginx DocumentRoot => /var/www/html/
- Nginx config directory => /etc/nginx/
- global config file => /etc/nginx/nginx.conf
- virtual domain/host config directory => /etc/nginx/sites-available/
- currently enabled domain/host config directory => /etc/nginx/sites-enabled/
- various config options => /etc/nginx/snippets/
- server log file => /var/log/nginx/access.log
- server error log file => /var/log/nginx/error.log
Configuring Nginx virtual hosts
Create a config file for your domain. In my case for test domain ctx06vm.local.lnxorg:
$ sudo vi /etc/nginx/sites-available/ctx06vm-local-lnxorg.conf
server {
listen 80;
listen [::]:80;
server_name ctx06vm.local.lnxorg;
root /home/ctx06vm-local-lnxorg/html;
index index.html;
access_log /var/log/nginx/ctx06vm_local_lnxorg_access.log;
error_log /var/log/nginx/ctx06vm_local_lnxorg_error.log;
location / {
try_files $uri $uri/ =404;
}
}
Create a new user for your domain. An example for my case:
$ sudo useradd -d /home/ctx06vm-local-lnxorg -m -k /dev/null -s /usr/sbin/nologin pageadmin
Make sure we lock the Linux user account:
$ sudo passwd -l pageadmin
Create html folder with *.html files and set permission using the chown command:
## for my case: ##
$ sudo mkdir -pv /home/ctx06vm-local-lnxorg/html/
$ sudo chown -R pageadmin: /home/ctx06vm-local-lnxorg/
Turn on created virtual domain, test Nginx virtual host config and reload nginx service:
## for my case:
$ sudo ln -v -s /etc/nginx/sites-available/ctx06vm-local-lnxorg.conf /etc/nginx/sites-enabled/
$ sudo nginx -t
$ sudo systemctl reload nginx.service
Firewall configuration
$ sudo ufw allow 80/tcp comment 'accept HTTP Nginx connections'
$ sudo ufw allow 443/tcp comment 'accept HTTPS/TLS Nginx connections'
$ sudo ufw status
fcgiwrap installation and configuration
$ sudo apt install fcgiwrap
$ sudo systemctl status fcgiwrap.service
## if fcgiwrap is not enabled, enable it and run
Create a new configuration for fcgiwrap in /etc/nginx/fcgiwrap.conf. Example configuration:
location /cgi-bin/ {
gzip off;
root /usr/lib;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/lib$fastcgi_script_name;
}
Edit your Nginx virtual host config file. Locate the server section and add the following directive:
[...]
include /etc/nginx/fcgiwrap.conf;
[...]
Test Nginx virtual host config and reload nginx service:
$ sudo nginx -t
$ sudo systemctl reload nginx.service
Create a cgi-bin directory under /usr/lib/:
$ sudo mkdir -vp /usr/lib/cgi-bin/
Writing Hello World CGI script
Create the /usr/lib/cgi-bin/hello.cgi file with the following code:
#!/usr/bin/env bash
echo "Content-type: text/html"
echo ""
now="$(date)"
echo '<html><head><title>Hello World - CGI app</title></head>'
echo '<body>'
echo '<h2>Hello World!</h2>'
echo "The current date and time : ${now}<br/>"
echo '</body>'
echo '</html>'
Set execute permissions for this file:
$ sudo chmod +x -v /usr/lib/cgi-bin/hello.cgi
Test it. Open a web-browser and type url: https://your-domain/cgi-bin/hello.cgi.