Setting Up an Nginx Server with PHP, MySQL, and PhpMyAdmin: A Comprehensive Guide

 Introduction

Nginx is a powerful and efficient web server known for its high performance and low resource consumption. Combined with PHP and MySQL, it forms a robust stack for serving dynamic web applications. In this guide, we will walk you through the process of setting up an Nginx server with PHP, MySQL, and PhpMyAdmin. We will include detailed instructions, commands, and notes to ensure clarity at every step.

Prerequisites

  • A server with a fresh installation of Ubuntu (or any Debian-based distribution).

  • Root or sudo access to the server.

Step 1: Update and Upgrade the System

Before starting, ensure your system is up-to-date:

sudo apt update
sudo apt upgrade -y

Step 2: Install Nginx

Nginx can be installed from the default Ubuntu repositories.

sudo apt install nginx -y

After installation, start and enable Nginx to run on boot:

sudo systemctl start nginx
sudo systemctl enable nginx

Verify the installation by visiting your server's IP address in a web browser. You should see the Nginx welcome page.

Step 3: Install MySQL

MySQL is a widely used relational database management system. Install it using:

sudo apt install mysql-server -y

Secure the MySQL installation by running the following command and following the prompts:

sudo mysql_secure_installation

Step 4: Install PHP

To process PHP scripts, we need to install PHP along with some essential extensions:

sudo apt install php-fpm php-mysql -y

Notes:

  • The php-fpm package is for the FastCGI Process Manager, which helps in efficiently managing PHP processes.

  • The php-mysql extension allows PHP to communicate with MySQL.

Step 5: Configure Nginx to Use PHP

Edit the default Nginx server block to handle PHP files:

sudo nano /etc/nginx/sites-available/default

Modify the file as follows:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm;

    server_name _;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

Notes:

  • Adjust fastcgi_pass if you are using a different PHP version (e.g., php8.0-fpm.sock for PHP 8.0).

Restart Nginx to apply the changes:

sudo systemctl restart nginx

Step 6: Test PHP Processing

Create a PHP info file to test the configuration:

sudo nano /var/www/html/info.php

Add the following content:

<?php 
phpinfo();
?>

Visit http://your_server_ip/info.php in a web browser. You should see a page displaying PHP information.

Step 7: Install PhpMyAdmin

PhpMyAdmin is a popular tool for managing MySQL databases via a web interface. Install it using:

sudo apt install phpmyadmin -y

During installation, you will be prompted to configure PhpMyAdmin:

  1. Select a web server: Choose nginx by pressing Tab and Enter.

  2. Configure database for PhpMyAdmin: Select Yes.

  3. Set a password: Enter a password for the PhpMyAdmin user.

Manually Configure PhpMyAdmin with Nginx

If PhpMyAdmin does not automatically configure with Nginx, follow these steps:

Create a symbolic link to PhpMyAdmin:

sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin

Restart Nginx:

sudo systemctl restart nginx

Now, you can access PhpMyAdmin at http://your_server_ip/phpmyadmin.

Conclusion

Setting up an Nginx server with PHP, MySQL, and PhpMyAdmin involves several steps, but with careful attention to each stage, you can establish a powerful and efficient server environment. This stack provides a solid foundation for hosting dynamic web applications. By following this guide, you should now have a fully functional web server ready for development and deployment.

13/03/2025, 20:50
86
Similar Posts
Exploring IndexedDB: A Deep Dive into Browser Storage and Alternatives

Author: Suparva - 2 minutes 5 seconds

Introduction In the modern web ecosystem, managing client-side data efficiently is essential. IndexedDB is one of the powerful storage solutions available in browsers, providing a way to store significant amo...

More
Create Apps & Websites with AI – No Coding Needed! Best Free AI Tools & Guides

Author: Suparva - 4 minute

How to Use AI Tools to Create Apps and Websites Without Any Coding Hey everyone, I’m Suparva, a 16-year-old tech enthusiast, and welcome to my blog at suparva.com! If you’ve ever felt daunted by the ide...

More
Mastering AI: The Ultimate Guide to Using AI Models for Success in Work & Life

Author: Suparva - 2 minutes 35 seconds

​Artificial Intelligence (AI) is no longer a futuristic concept; its a present-day reality transforming the way we work and live. As a 16-year-old navigating this dynamic landscape, you might feel both excited and ove...

More
Unlocking the Future of AI: A Deep Dive into MCP Servers

Author: Suparva - 3 minutes 21 seconds

In todays rapidly evolving AI landscape, the ability to provide precise and timely context to large language models (LLMs) is more important than ever. One groundbreaking innovation that addresses this need is the Mod...

More