Wednesday, July 26, 2017

Install PHPMyAdmin and Nginx on Ubuntu 19.04

Knowing how to install PHPMyAdmin and Nginx is essential when developing web applications, it is also very useful while taking comprehensive course projects, which prefer local installations.

References: Practical Ubuntu Linux Server for beginners

You can watch the video on the subject: 

So let's start first the installation of Nginx:
sudo apt-get install nginx
(optional) check if its ports are enabled in the firewall: sudo ufw status
and enable port 80 if necessary:
sudo ufw allow 'Nginx HTTP'
find and note your ip address with: ifconfig

open up in browser: http://127.0.0.1 and you should see: Welcome to NGINX

now is time to install MySQL-server:
sudo apt-get install mysql-server
remember to set a password!

(optional) secure mysql: sudo mysql_secure_installation


change the connection of MySQL from a socket to password authentication and set its password to password (to be able to use the root account within PHPMyAdmin):
sudo mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
next, apply the changes with:
FLUSH PRIVILEGES;
EXIT


install php-fpm to enable Nginx to communicate with PHP, as well as php-mysql to enable PHP to communicate with MySQL databases:
sudo apt-get install php-fpm php-mysql
(optional) security for PHP (fix closest name script execution): sudo nano /etc/php/7.3/fpm/php.ini (replace /php/7.x with your PHP version (type: php -v)
inside php.ini change cgi.fix_pathinfo=0

enable Nginx to process PHP:
sudo nano /etc/nginx/sites-available/default

server {

listen 80 default_server;

listen [::]:80 default_server;

root /var/www/html;

#add index.php for processing PHP files first

index index.php index.html index.htm index.nginx-debian.html;


#you can replace 127.0.0.1 with your own ip address

server_name 127.0.0.1;


location / {

try_files $uri $uri/ =404;

}

#the actual PHP processing

location ~ \.php$ {

#enable only these two:

include snippets/fastcgi-php.conf;

fastcgi_pass unix:/run/php/php7.3-fpm.sock;

}


location ~ /\.ht {

deny all;

}

}


reload Nginx configuration:

sudo systemctl reload nginx

(optional) if problems occur: test the configuration with: sudo nginx -t


test if php is running: by creating a test file:

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

<?php

echo "php is running";

?>

save and exit with ctrl + W and ctrl + X


(optional)(if you don't have nano: sudo apt install nano )


point the browser to 127.0.0.1 you should get: php is running


Final step:

Install PHPMyAdmin (remember your MySQL password)
sudo apt-get install phpmyadmin
select apache2 installation from the dialogue.

add PHPMyAdmin configuration into Nginx in order enable /phpmyadmin directory location (127.0.0.1/phpmyadmin)
sudo nano /etc/nginx/sites-available/default
then add into the server directive:


location /phpmyadmin {

root /usr/share/;

index index.php index.html index.htm;

location ~ ^/phpmyadmin/(.+\.php)$ {

try_files $uri =404;

root /usr/share/;

fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include /etc/nginx/fastcgi_params;

}

location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {

root /usr/share/;

}

}



location /phpMyAdmin {

rewrite ^/* /phpmyadmin last;

}


notice the php7.3-fpm.sock -> replace it with your PHP version!


Here you go you are ready to run: 127.0.0.1/phpmyadmin
you can log in with "phpmyadmin" and "your_mysql_password"
( if you don't have permissions to create databases log as root and "your_mysql_password" )

phpmyadmin running on nginx in ubuntu

Cheers!

Subscribe To My Channel for updates