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.
Table of Contents
- 1 Update Your System
- 2 Install Nginx
- 3 Install MySQL
- 4 Install PHP
- 5 Configure Nginx for PHP
- 6 Create a PHP Info Page
- 7 Install PHPMyAdmin
- 8 Configure Nginx for PHPMyAdmin
- 9 Access PHPMyAdmin
- 10 Secure Your MySQL Installation
- 11 Finalize PHPMyAdmin Configuration
- 12 Firewall Configuration
- 13 SSL/TLS Configuration (Optional)
- 14 Test Your Configuration
- 15 FAQ
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
BashInstall Nginx
Nginx is a web server that will handle your PHP applications. Install it with the following command:
sudo apt install nginx
BashStart Nginx and enable it to start on boot:
sudo systemctl start nginx
sudo systemctl enable nginx
BashTo 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
BashDuring 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
BashInstall 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
BashConfigure 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
BashReplace 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;
}
}
NginxMake sure to change server_name
to your domain or IP address, and fastcgi_pas
s 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/
BashTest the Nginx configuration:
sudo nginx -t
BashIf there are no errors, restart Nginx:
sudo systemctl restart nginx
BashCreate 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
BashAdd the following content:
<?php
phpinfo();
?>
PHPSave 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
BashDuring 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
BashStart 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;
}
}
NginxMake 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/
BashTest the Nginx configuration:
sudo nginx -t
BashIf there are no errors, restart Nginx:
sudo systemctl restart nginx
BashAccess 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
BashSet 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
BashThis 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';
INIChange 'cookie'
to 'http'
:
$cfg['Servers'][$i]['auth_type'] = 'http';
INIThis 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'
BashEnable the firewall if it’s not already:
sudo ufw enable
BashSSL/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
BashRun Certbot to obtain and install a certificate:
sudo certbot --nginx -d your_domain
BashFollow 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
BashYou 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
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.
PHPMyAdmin offers a simple web interface for managing MySQL databases. It makes it easier to create databases, tables, and execute SQL queries.
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
.
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.
Install MySQL on Ubuntu by using the command sudo apt-get install mysql-server.
During installation, create a root password for MySQL.