Setting up a robust web server configuration is crucial for hosting dynamic applications. While Docker is excellent, configuring a native environment allows you to maximize resource efficiency on smaller virtual servers. This comprehensive guide will show you exactly how to install php mysql phpmyadmin nginx on an Ubuntu system to build a high-performance web environment.
Prerequisites
Before beginning the installation process, ensure that you have:
sudo administrative privileges.Step 1: Update the Package Index
To ensure system stability and prevent dependency conflicts, begin by updating your local package repository and upgrading any outdated core packages.
bashsudo apt update && sudo apt upgrade -yStep 2: Install Nginx Web Server
Nginx is a lightweight, high-performance web server optimized for speed. Install it using the default Ubuntu package manager by executing the following command:
bashsudo apt install nginx -yOnce the installation concludes, verify that the web service is active and running under systemd control:
bashsudo systemctl status nginxStep 3: Install MySQL Database Server
Next, you need a relational database to store your application data. Run this command to install the official MySQL server package:
bashsudo apt install mysql-server -yAfter the installation completes, execute the built-in security script to configure access limitations, set password complexity policies, and remove anonymous user access:
bashsudo mysql_secure_installationStep 4: Install PHP FastCGI Process Manager
Unlike other web servers, Nginx does not contain native PHP processing capabilities. You must utilize PHP-FPM (FastCGI Process Manager) to handle dynamic script executions. Execute the command below to fetch PHP along with its common database extensions:
bashsudo apt install php-fpm php-mysql php-mbstring php-zip php-gd php-json php-curl -yCheck the active status of the PHP-FPM component to confirm it is operational before proceeding further:
bashsudo systemctl status php*-fpmStep 5: Configure Nginx to Process PHP
To link your web server with the PHP processor, you need to modify the default Nginx server block. Open the configuration file using your preferred terminal text editor:
bashsudo nano /etc/nginx/sites-available/defaultLocate the server block directive and adjust its contents to match the structure defined below, ensuring the default index reads index.php:
nginxserver {
listen 80;
root /var/www/html;
index index.php index.html index.htm;
server_name your_server_ip;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}Save the modified file, test the Nginx configuration syntax for any hidden structural errors, and safely reload the service daemon:
bashsudo nginx -t
sudo systemctl reload nginxStep 6: Install PhpMyAdmin Database Controller
PhpMyAdmin provides a convenient, web-based graphical user interface to manage your active MySQL databases. Run the following command to begin fetching the configuration files:
bashsudo apt install phpmyadmin -yDuring the interactive installation terminal prompts, select none when asked to automatically configure a web server (since Nginx is not listed natively), and select Yes to allow dbconfig-common to initialize the database backend structures.
Step 7: Create a Symlink for PhpMyAdmin
To make the graphical web interface accessible through your Nginx web root directory, you must establish a symbolic link pointing to the system installation path:
bashsudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadminEnsure that the proper folder ownership properties are applied across your web directory so the process worker can interact with it safely:
bashsudo chown -R www-data:www-data /var/www/html/phpmyadminStep 8: Test the Web Interface Setup
To ensure you successfully manage to install php mysql phpmyadmin nginx, create a temporary testing file within your root deployment directory:
bashecho "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.phpOpen a web browser and navigate to http://your_server_ip/info.php to verify the active configurations. Afterward, navigate over to http://your_server_ip/phpmyadmin to log into your clean database panel instance.
Optional: Create a Dedicated Database Administrative User
Modern MySQL installations rely on the auth_socket plugin for root system authentication, which can block standard web-based logins. To resolve this, create a separate administrative database account:
bashsudo mysqlExecute the following SQL commands sequentially inside the open prompt to initialize your admin profile and flush privileges:
sqlCREATE USER 'adminuser'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_secure_password';
GRANT ALL PRIVILEGES ON *.* TO 'adminuser'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;