Setup Nginx, PHP, MySQL, and PHPMyAdmin on Ubuntu

Setup Nginx, PHP, MySQL, and PHPMyAdmin on Ubuntu

Setting up PHP, MySQL, PhpMyAdmin, and Nginx on Ubuntu entails a few steps. This guide will take you through the procedure step by step, including installation and configuration of the components.

Update Your System

Before installing any software, make sure your system is up-to-date. Open a terminal and execute the following commands:

sudo apt update
sudo apt upgrade
Bash

Install Nginx

Nginx is a web server that will handle your PHP applications. Install it with the following command:

sudo apt install nginx
Bash

Start Nginx and enable it to start on boot:

sudo systemctl start nginx
sudo systemctl enable nginx
Bash

To see if Nginx is operating, open a web browser and enter your server’s IP address. You should see Nginx’s default welcome page.

Install MySQL

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

sudo apt install mysql-server
Bash

During the installation, you will be asked to create a root password. Make sure it is sturdy and secure. Once the installation is completed, start MySQL and set it to start on boot:

sudo systemctl start mysql
sudo systemctl enable mysql
Bash

Install PHP

Install PHP alongside common extensions that are frequently required:

sudo apt install php-fpm php-mysql php-curl php-gd php-mbstring php-mcrypt php-xml 
    php-xmlrpc php-zip
Bash

Configure Nginx for PHP

Set up a new Nginx server block configuration file for your PHP application. Use a text editor such as Nano or Vim.

sudo nano /etc/nginx/sites-available/your-site
Bash

Replace your-site with a name that is appropriate for your configuration file. Within the file, you can use the following setup as a starting point:

server {
    listen 80;
    server_name your_domain_or_ip;
    root /var/www/html;
    index index.php index.html index.htm;
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }
}
Nginx

Make sure to change server_name to your domain or IP address, and fastcgi_pass to the PHP version you’ve installed (check /run/php/).

Create a symbolic link to enable the site settings.

sudo ln -s /etc/nginx/sites-available/your-site /etc/nginx/sites-enabled/
Bash

Test the Nginx configuration:

sudo nginx -t
Bash

If there are no errors, restart Nginx:

sudo systemctl restart nginx
Bash

Create a PHP Info Page

Create a PHP info page to confirm that it is working properly. Create a file called info.php in the web root directory:

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

Add the following content:

<?php
phpinfo();
?>
PHP

Save the file and access it via your web browser http://your_domain_or_ip/info.php. You should see the PHP information page.

Install PHPMyAdmin

PHPMyAdmin is a web-based interface for administering MySQL databases. Install it with the following commands:

sudo apt install phpmyadmin
Bash

During installation, you will be prompted to set up a web server. Choose Nginx. After that, you will be prompted to configure the database for PHPMyAdmin, so provide your MySQL root password.

Configure Nginx for PHPMyAdmin

You must construct a server block for PHPMyAdmin. Create a new configuration file.

sudo nano /etc/nginx/sites-available/phpmyadmin
Bash

Start with the configuration shown below:

server {
    listen 80;
    server_name phpmyadmin_domain_or_ip;
    root /usr/share/phpmyadmin;
    index index.php index.html index.htm;
    location / {
        try_files $uri $uri/ =404;
    }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }
}
Nginx

Make sure to change the server_name to a suitable domain or IP.

Create a symbolic link to enable the PHPMyAdmin settings.

sudo ln -s /etc/nginx/sites-available/phpmyadmin /etc/nginx/sites-enabled/
Bash

Test the Nginx configuration:

sudo nginx -t
Bash

If there are no errors, restart Nginx:

sudo systemctl restart nginx
Bash

Access PHPMyAdmin

You should now be able to access PHPMyAdmin using your web browser by going to http://phpmyadmin_domain_or_ip. Log in with the MySQL root username and password.

Secure Your MySQL Installation

It’s critical to secure your MySQL installation. Run the MySQL secure installation script.

sudo mysql_secure_installation
Bash

Set a root password, delete anonymous users, disable remote root login, delete the test database, and reload privileges.

Finalize PHPMyAdmin Configuration

Edit PHPMyAdmin’s configuration file:

sudo nano /etc/phpmyadmin/config.inc.php
Bash

This file allows you to create a more secure authentication method than using the MySQL root user. Look for and edit the following section:

$cfg['Servers'][$i]['auth_type'] = 'cookie';
INI

Change 'cookie' to 'http':

$cfg['Servers'][$i]['auth_type'] = 'http';
INI

This requires you to input your username and password each time you access PHPMyAdmin, which is more secure.

Firewall Configuration

If you have a firewall enabled (such as UFW), you may need to allow HTTP and HTTPS traffic.

sudo ufw allow 'Nginx Full'
Bash

Enable the firewall if it’s not already:

sudo ufw enable
Bash

SSL/TLS Configuration (Optional)

Consider installing SSL/TLS certificates to increase the security of your server. Let’s Encrypt provides free certificates, which you can then setup to use with Nginx.

Install Certbot and the Nginx plugin:

sudo apt install certbot python3-certbot-nginx
Bash

Run Certbot to obtain and install a certificate:

sudo certbot --nginx -d your_domain
Bash

Follow the steps, and Certbot will configure Nginx to use the SSL/TLS certificate.

Follow this blog for more details: Install Let’s Encrypt with Nginx on Ubuntu

Test Your Configuration

Finally, restart Nginx to apply the modifications.

sudo systemctl restart nginx
Bash

You should now have a fully functional web server running PHP, MySQL, and PHPMyAdmin. You may host PHP applications, manage MySQL databases, and benefit from Nginx’s security and performance features.

Remember that this is a basic setup guide; for production systems, additional security and optimizations should be addressed. Always keep your server and software up-to-date, and make regular backups of your data.

After completing these instructions, you should have an LEMP stack (Linux, Nginx, MySQL, PHP) with PHPMyAdmin running on your Ubuntu server. This arrangement can provide a good basis for hosting web applications and databases.

FAQ

Why use Nginx, PHP, MySQL, and PHPMyAdmin together on Ubuntu?

This combination creates a powerful web development stack. Nginx is a web server, PHP is used for server-side scripting, MySQL is a database server, and PHPMyAdmin provides a graphical interface for database management.

What role does PHPMyAdmin play in this configuration?

PHPMyAdmin offers a simple web interface for managing MySQL databases. It makes it easier to create databases, tables, and execute SQL queries.

What is the Nginx configuration file, and where is it located?

The primary Nginx configuration file is normally placed at /etc/nginx/nginx.conf, but server-specific configurations are frequently found in distinct files within the /etc/nginx/sites-available/ directory.

How can I test if the entire setup is working correctly?

Create a PHP file (e.g., info.php) in the Nginx web root (/var/www/html/) with the following content: Accessing this file via a web browser should display PHP and Nginx information, confirming the setup’s operation.

How can I install MySQL on Ubuntu?

Install MySQL on Ubuntu by using the command sudo apt-get install mysql-server. During installation, create a root password for MySQL.

Have questions about this blog? Contact us for assistance!