Install PHP on Ubuntu 18.04

In this post, I will show you how to install PHP Ubuntu 18.04 on both Apache and NGINX web servers.  This will allow you to serve dynamic web pages.

You must already have Apache or NGINX installed in order to proceed with this article.  If you don’t then please refer to these articles to get that done and come back here to continue:

Install NGINX on Ubuntu 18.04

Install Apache on Ubuntu 18.04

Now that we have our web server installed we are free to install PHP.

Install PHP for Apache

Installing PHP for Apache is a pretty easy process.  All you have to do is run the following command to install it.

# apt install php

This will install PHP and configure Apache to serve PHP pages.  Skip ahead to the Testing Your PHP Installation section below.

Install PHP for NGINX

Install PHP for NGINX is a little bit more involved but still not too bad.  The first thing we need to do is install PHP:

# apt install php-fpm

Next we need to change the NGINX configuration to utilize PHP.  Edit the default configuration file /etc/nginx/sites-available/default.  Edit it to look like the following:

server {
  listen 80 default_server;
  listen [::]:80 default_server;
  root /var/www/html;
  index index.php index.html index.htm index.nginx-debian.html;
  server_name _;
  location / {
    try_files $uri $uri/ =404;
  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;

Here we added index.php to the index line.  We also added the location PHPsection.  Take note that if you uncomment the location PHP section it will have the wrong version for php.  Make sure it says php7.2-fpm.sock so that it will work.

To wrap things up go ahead and reload NGINX.

# systemctl reload nginx

You are now ready to test your PHP installation

Testing Your PHP Installation

Now that you have PHP installed you need to test it to ensure that everything is working as expected.  Create a new file in /var/www/html named phpinfo.php with the following contents:


Save and exit the editor.  Now browse to the address below.

http://{your server ip}/phpinfo.php

You should see the PHP information page:

install php ubuntu 18.04

This means that everything is correctly configured and you are all set for serving dynamic web pages using PHP.

Click here for more great articles from AdminTome Blog.



I hope you have enjoyed this article, if so please leave a comment below.  For more articles, please signup for the AdminTome Blog below.  Also please feel free to share the article to your friends using the buttons to the left.  Thanks again for reading this post.