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.
A server with a fresh installation of Ubuntu (or any Debian-based distribution).
Root or sudo access to the server.
Before starting, ensure your system is up-to-date:
sudo apt update
sudo apt upgrade -y
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.
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
To process PHP scripts, we need to install PHP along with some essential extensions:
sudo apt install php-fpm php-mysql -y
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.
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;
}
}
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
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.
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:
Select a web server: Choose nginx
by pressing Tab
and Enter
.
Configure database for PhpMyAdmin: Select Yes
.
Set a password: Enter a password for the PhpMyAdmin user.
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
.
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.
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...
MoreAuthor: 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...
MoreAuthor: 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...
MoreAuthor: 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