Setting up a local development environment on Ubuntu 22.04 with PHP 8.1, Nginx, and Composer is essential for web developers looking to build and test PHP applications efficiently. This comprehensive guide will walk you through installing PHP 8.1, configuring necessary extensions, setting up Nginx as the web server, installing Composer for managing dependencies, and creating a virtual host. Let’s get started!

Step 1: Deploying a Cloud Instance on Shape.host

  • Log in to Shape.host Dashboard:
  • Navigate to the Shape.host website and log in to your account.
  • Create a New Instance:
  • Click on the “Create” button located at the top right corner of the dashboard.
  • From the dropdown menu, select “Instances”.
Shape.host Resources dashboard showing the Create menu and instance rebuild controls.
  • Select Instance Location:
  • Choose the desired location for your server. For this tutorial, we’ll select “New York, USA”.
Shape.host Create New Instance form with the available data center locations.
  • Choose a Plan:
  • Select a plan that fits your requirements. For example, you might choose a plan with 2 cores CPU, 2 GB Memory, and 50 GB SSD disk space.
  • Select an Operating System:
  • Scroll down to the “Choose an image” section and select “Ubuntu 22.04”.
Shape.host instance form with a server plan and Ubuntu image selected.
  • Configure Additional Options:
  • (Optional) You can configure additional options like User Data Configuration and IPv6 Networking.
  • Enter a hostname for your instance, e.g., “Tutorial Ubuntu”.
  • Click on the “Create instance” button to deploy the instance.
Shape.host instance form showing hostname, instance count and the Create Instance button.

Step 2: Connecting to Your Instance

  • Retrieve SSH Credentials:
  • Note the IP address of your newly created instance from the Shape.host dashboard.
  • Connect via SSH:
  • Open a terminal on your local machine.
  • Use the following command to connect to your instance:
    ssh root@your_instance_ip
  • Replace your_instance_ip with the actual IP address of your instance.

Step 3: Update and Upgrade Your System

Ensure your system is up-to-date and all packages are upgraded to the latest versions:

apt update
apt upgrade -y
Terminal showing APT package management commands and output.

Step 4: Install Required Dependencies

Install essential packages needed for adding repositories and installing software:

apt install software-properties-common ca-certificates lsb-release apt-transport-https curl git -y
Terminal showing APT package management commands and output.

Step 5: Add the PHP Repository

Add the repository that contains PHP 8.1 packages:

add-apt-repository ppa:ondrej/php
apt update
Terminal showing APT package management commands and output.

Step 6: Install PHP 8.1 and Extensions

Install PHP 8.1 and the necessary PHP extensions:

apt install php8.1 php8.1-fpm php8.1-mysql php8.1-curl php8.1-mbstring php8.1-xml -y
Terminal showing APT package management commands and output.

Step 7: Install Composer

Composer is a dependency manager for PHP. Install Composer globally on your system:

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
chmod +x /usr/local/bin/composer
Terminal showing file and directory setup commands.

Verify the installation by checking the Composer version:

composer --version
Terminal showing composer commands or output during Install Composer.

You should see output indicating the installed Composer version.

Step 8: Configure PHP-FPM

PHP-FPM (FastCGI Process Manager) is needed for PHP processing with Nginx. Configure PHP-FPM by editing its configuration file:

nano /etc/php/8.1/fpm/php.ini
Terminal showing nano commands or output during Configure PHP-FPM.

Adjust settings as per your requirements, such as memory_limit and upload_max_filesize. Save and close the file.

Step 9: Install and Configure Nginx

Install Nginx web server:

apt install nginx -y
Terminal showing APT package management commands and output.

Start Nginx and enable it to start on boot:

systemctl start nginx
systemctl enable nginx
Terminal showing systemd service management commands or output.

Step 10: Create Web Directory

Create the directory for your website:

mkdir -p /var/www/tutorials.shape.host
Terminal showing service commands or output during Create Web Directory.

Set the ownership to the www-data user and group:

chown -R www-data:www-data /var/www/tutorials.shape.host
Terminal showing file ownership or permission commands.

Set the appropriate permissions:

chmod -R 755 /var/www/tutorials.shape.host
Terminal showing file ownership or permission commands.

Step 11: Configure Nginx for PHP

Create a new server block configuration file for your site:

nano /etc/nginx/sites-available/tutorials.shape.host
Terminal showing nano commands or output during Configure Nginx for PHP.

Add the following configuration, adjusting the server_name and root directives as needed:

server {
    listen 80;
    server_name tutorials.shape.host;
    root /var/www/tutorials.shape.host;

    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ /\.ht {
        deny all;
    }
}
Terminal showing the GNU nano editor and configuration text.

Save and close the file.

Step 12: Enable the Nginx Server Block

Enable the server block by creating a symbolic link from sites-available to sites-enabled:

ln -s /etc/nginx/sites-available/tutorials.shape.host /etc/nginx/sites-enabled/
Terminal showing nano commands or output during Enable the Nginx Server Block.

Verify the Nginx configuration syntax:

nginx -t
Terminal showing Nginx configuration test output.
Terminal showing Nginx configuration test output.

If the syntax is OK, reload Nginx to apply the changes:

systemctl reload nginx
Terminal showing systemd service management commands or output.

Step 13: Test Your Setup

Create a phpinfo.php file in your web root to verify PHP and Nginx configuration:

nano /var/www/tutorials.shape.host/phpinfo.php
Terminal showing nano commands or output during Test Your Setup.

Add the following PHP code to the file:

<?php
phpinfo();
?>
Terminal showing the GNU nano editor and configuration text.

Save and close the file. Then, access http://tutorials.shape.host/phpinfo.php in your web browser. You should see the PHP information page if everything is configured correctly.

PHP information page showing version and server configuration details.

For those seeking reliable and scalable hosting solutions, Shape.host offers a variety of Cloud VPS services. Whether you’re a developer needing a robust environment for your applications or a business looking for scalable solutions, Shape.host provides the infrastructure and support to meet your needs.