Setting Up an Ubuntu Server with Nginx and PHP 8.1

Setting up a web server with Ubuntu, Nginx, and PHP 8.1 is a crucial step for deploying modern web applications. This guide will walk you through the installation and configuration process step-by-step.

Step 1: Update and Upgrade the System

Before installing any software, it’s always good practice to update and upgrade the existing packages to ensure compatibility and security.

sudo apt-get update
sudo apt-get upgrade -y

Step 2: Install Nginx

Nginx is a high-performance web server. Install it using:

sudo apt install nginx -y

After installation, verify that Nginx is running:

sudo systemctl status nginx

If it’s not running, start the service:

sudo systemctl start nginx

To enable Nginx to start at boot:

sudo systemctl enable nginx

Step 3: Install PHP 8.1

Since PHP 8.1 is not included in the default Ubuntu repositories, we need to add an external repository.

sudo apt update
sudo apt install -y software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install -y php8.1

Verify the installation:

php -v

Step 4: Install Required PHP Modules

For PHP to work properly with Nginx and databases, install necessary extensions:

sudo apt install -y php8.1-mysql php8.1-curl php8.1-xml php-mbstring

Step 5: Setting Up the Web Directory

Navigate to the web directory:

cd /var/www/html
ls

Create a new directory for your project:

sudo mkdir circulo
sudo chown -R ubuntu circulo
sudo chgrp -R ubuntu circulo
cd circulo

Step 6: Configure Git and Clone Repository

Generate an SSH key to authenticate with GitHub:

ssh-keygen -t rsa -b 4096 -C "ajender210282@gmail.com"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub

Add the SSH key to GitHub and clone the repository:

git init
git remote add origin git@github.com:*******************************************
git fetch
git checkout -t origin/branch
ls

Step 7: Set Up File Permissions

Create necessary directories for file storage:

mkdir captcha_images
mkdir uploads
sudo chown -R www-data uploads
sudo chgrp -R www-data uploads

Step 8: Install GD Library for Image Processing

GD is a popular library for handling images in PHP.

sudo apt update
sudo apt install php8.1-gd
php8.1 -m | grep gd

Step 9: Restart Services

After installing PHP and configuring the necessary modules, restart the services to apply changes:

After installing PHP and configuring the necessary modules, restart the services to apply changes:

You have now successfully set up an Ubuntu server with Nginx and PHP 8.1. Your server is ready for deployment, and you can now host your web applications efficiently.

If you encounter any issues, check the logs:

tail -f /var/log/nginx/error.log
systemctl status php8.1-fpm

Happy coding!

RSS
Follow by Email
LinkedIn
Share
Scroll to Top