
In this blog post, I will show you how to set up a web server on Ubuntu using Nginx, PHP 8.1, and Certbot. Nginx is a fast and lightweight web server that can handle high traffic and serve static and dynamic content. PHP 8.1 is the latest version of the popular scripting language that can run on the server side and generate dynamic web pages. Certbot is a tool that automates the process of obtaining and renewing SSL certificates from Let’s Encrypt, a free and trusted certificate authority. SSL certificates enable HTTPS, which encrypts the communication between the web server and the clients, and also verifies the identity of the website.
By following this guide, you will be able to:
Before you start, you will need:
The first step is to install the latest Ubuntu version on your server. You can use any method that suits your preference, such as using an ISO image, a pre-built image, or a cloud-init script. For this guide, I will assume that you are using a cloud provider that offers Ubuntu images, such as DigitalOcean.
To install Ubuntu on your server, follow these steps:
Once your server is up and running, you can log in to it using SSH. You can use any SSH client, such as [PuTTY], [Termius], or [OpenSSH]. For example, if you are using OpenSSH on Linux or macOS, you can run this command in your terminal:
ssh username@server_ip_address
Replace username with your sudo user name and server_ip_address with your server’s IP address. You should see a welcome message like this:
Welcome to Ubuntu 20.04.3 LTS (GNU/Linux 5.4.0-80-generic x86_64)
System information as of Wed Nov 17 09:11:55 UTC 2021
System load: 0.0 Processes: 103 Usage of /: 3.5% of 24.06GB Users logged in: 0 Memory usage: 16% IPv4 address for eth0: 203.0.113.10 Swap usage: 0%
0 updates can be installed immediately. 0 of these updates are security updates.
The list of available updates is more than a week old. To check for new updates run: sudo apt update
You have successfully installed Ubuntu on your server.
The next step is to install Nginx and PHP 8.1 on your server. Nginx is a web server that can serve static and dynamic content, as well as act as a reverse proxy, load balancer, or cache server. PHP 8.1 is the latest version of the popular scripting language that can run on the server side and generate dynamic web pages.
To install Nginx and PHP 8.1 on your server, follow these steps:
sudo apt update
sudo apt install nginx
sudo systemctl status nginx
You should see an output like this:
● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2021-11-17 09:15:23 UTC; 2min 13s ago Docs: man:nginx(8) Process: 1010 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 1028 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 1029 (nginx) Tasks: 3 (limit: 1137) Memory: 3.5M CGroup: /system.slice/nginx.service ├─1029 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; ├─1030 nginx: worker process └─1031 nginx: worker processNov 17 09:15:23 ubuntu-s-1vcpu-1gb-nyc1-01 systemd[1]: Starting A high performance web server and a reverse proxy server... Nov 17 09:15:23 ubuntu-s-1vcpu-1gb-nyc1-01 systemd[1]: Started A high performance web server and a reverse proxy server.
This means that Nginx is running and enabled to start automatically on boot.
sudo add-apt-repository ppa:ondrej/php sudo apt update sudo apt install php8.1-fpm
sudo systemctl status php8.1-fpm
You should see an output like this:
● php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2021-11-17 09:18:12 UTC; 1min 7s ago Docs: man:php-fpm8.1(8) Process: 1119 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/8.1/fpm/pool.d/www.conf 81 (code=exited, status=0/SUCCESS) Main PID: 1118 (php-fpm8.1) Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec" Tasks: 3 (limit: 1137) Memory: 8.2M CGroup: /system.slice/php8.1-fpm.service ├─1118 php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf) ├─1120 php-fpm: pool www └─1121 php-fpm: pool wwwNov 17 09:18:12 ubuntu-s-1vcpu-1gb-nyc1-01 systemd[1]: Starting The PHP 8.1 FastCGI Process Manager... Nov 17 09:18:12 ubuntu-s-1vcpu-1gb-nyc1-01 systemd[1]: Started The PHP 8.1 FastCGI Process Manager.
This means that PHP is running and enabled to start automatically on boot.
You have successfully installed Nginx and PHP 8.1 on your server.
The next step is to configure Nginx with one virtual host - www.example.com. A virtual host is a configuration file that defines the settings and behavior of a website hosted on the same server. You can create multiple virtual hosts for different domains or subdomains, and Nginx will serve the appropriate website based on the request.
To configure Nginx with one virtual host - www.example.com, follow these steps:
sudo mkdir -p /var/www/example.com
sudo chown -R $USER:$USER /var/www/example.com
nano /var/www/example.com/index.php
<?php echo "Hello, world!"; ?>
sudo nano /etc/nginx/sites-available/example.com
server {
listen 80;
listen [::]:80;
root /var/www/example.com;
index index.php index.html index.htm;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
}
This code tells Nginx to listen on port 80 for both IPv4 and IPv6, and to serve the files from the /var/www/example.com directory. It also specifies that the server name is example.com or www.example.com, and that it should use PHP to process any files ending with .php.
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
You should see an output like this:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
sudo systemctl reload nginx
You have successfully configured Nginx with one virtual host - www.example.com.
The final step is to install Certbot and set up automatic renew SSL certificates for your domain. Certbot is a tool that automates the process of obtaining and renewing SSL certificates from Let’s Encrypt, a free and trusted certificate authority. SSL certificates enable HTTPS, which encrypts the communication between the web server and the clients, and also verifies the identity of the website.
To install Certbot and set up automatic renew SSL certificates for your domain, follow these steps:
sudo add-apt-repository ppa:certbot/certbot sudo apt update
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx
Congratulations! You have successfully enabled https://example.com and https://www.example.comYou should test your configuration at: https://www.ssllabs.com/ssltest/analyze.html?d=example.com https://www.ssllabs.com/ssltest/analyze.html?d=www.example.com
sudo certbot renew --dry-run
You should see an output like this:
Cert not due for renewal, but simulating renewal for dry run Plugins selected: Authenticator nginx, Installer nginx Renewing an existing certificate Performing the following challenges: http-01 challenge for example.com http-01 challenge for www.example.com Waiting for verification... Cleaning up challengesnew certificate deployed with reload of nginx server; fullchain is /etc/letsencrypt/live/example.com/fullchain.pem
** DRY RUN: simulating 'certbot renew' close to cert expiry ** (The test certificates below have not been saved.)
Congratulations, all renewals succeeded. The following certs have been renewed: /etc/letsencrypt/live/example.com/fullchain.pem (success) ** DRY RUN: simulating 'certbot renew' close to cert expiry ** (The test certificates above have not been saved.)
This means that Certbot is working and will automatically renew your SSL certificates before they expire.
You have successfully installed Certbot and set up automatic renew SSL certificates for your domain.
You have learned how to set up a web server on Ubuntu using Nginx, PHP 8.1, and Certbot. You have also learned how to configure Nginx with one virtual host - www.example.com, and how to secure your website with SSL certificates from Let’s Encrypt. You can now use your web server to host your own website or web application, and enjoy the benefits of speed, security, and scalability.
I hope this blog post has helped you understand the steps and concepts involved in setting up a web server on Ubuntu. If you have any questions or feedback, please feel free to leave a comment below. Thank you for reading. 😊