How to install PHP and Apache on Linux (Ubuntu, Linux Mint)?

In this article, you will learn how to install PHP and Apache on your Debian based machines. I have especially focused on Ubuntu 18.04 and Linux Mint 19.1 operating systems. Also, you will learn how to run PHP scripts via PHP interpreter and Apache server on them.

PHP Installation

Firstly, we need to install the current PHP version on our machine. when this article is written latest stable PHP release is 7.3.9.

Normally old PHP packages always can be found in main or universe distro’s repositories but they are not always up to date. So we need to add an additional external repository to install the latest packages. These external repositories are called Personal Package Archive (PPA).

Especially PHP community is releasing new versions frequently. These new versions have more speed, less memory consumption, crucial bug fixes, and increased security features. It is recommended that always use up to date PHP versions.

But be careful about Beta releases which can cause some problems to use them in production. So always check the current stable releases from After deciding the right PHP version, you need to add a PPA to your operating system repository list. Thanks to Ondřej Surý, we can add his PPA which serves the latest PHP packages. Open the terminal (you can use the shortcut: CTRL+ALT+t) and type the command below:

$sudo add-apt-repository ppa:ondrej/php

Then in order to fetch the newest package information from your currently saved repositories, type this command:

$sudo apt update

Now your machine has the all necessary package information to install. So just type the below command in the terminal and just before hitting the enter press TAB to see all available packages:

$apt install php7.3

If you press tab without pressing enter, you will see the other PHP 7.3 packages also as:

You can look at them and install them according to your needs. But right now we just need to deal with the main PHP 7.3 package. To install the main PHP package just type the below and hit the ENTER:

$sudo apt install php7.3

After the installation, you can check the installed PHP version by:

$php -v

You will see:

PHP Running via Interpreter

Now you are ready to run PHP scripts. The easiest way is that you can use PHP interpreter right away to type and test some code from in the terminal:

$php -a

You can type some PHP code and test its behavior right away.

But also you can write PHP code snippets in external files and run them through the terminal as:

$nano test.php

Write an example PHP code:


echo “Hello World!\n”;


To save these changes on nano editor, do these in order:

Hit “CTRL+x”.

Then hit “y”.

Finally, hit “ENTER”.

To run this PHP script you saved, just type the below command in the same directory of the script:

$php test.php

As you can see, you can run any PHP script in the terminal just by typing the command as

$php script_name

Also, you can save your PHP script output by:

$php test.php > testoutput.txt

This creates a file to save your PHP script output. You can see your output file by cat command.

But wait a minute! It is not finished yet. We need a webserver to serve PHP files to build web sites or API projects.

Apache Installation

The most commonly used web server for PHP is Apache HTTP Server. The latest version at this time is Apache HTTP Server 2.4.41.

This time we don’t need an additional repository to install it because its packages are already available. So just open the terminal and type the below command:

$sudo apt install apache2

To see installed Apache version:

$apache2 -v

By default, the installed apache server is already started. You can see its status by:

$service apache2 status

Again by default, your apache server is already configured to handle your localhost request. By opening your browser and typing this:


You will see the default the apache 2 default page:

Also, you can use instead of http://localhost/ which is also indicates localhost. You can see your localhost configuration on the terminal by:

$cat /etc/hosts

This served default apache page is in the /var/www/html directory and the file is named as index.html. By default, Apache server serves files in this directory and if you don’t enter any additional path to your localhost URL, it automatically searches for index.html file to serve.

PHP Running via Apache

This time we will run PHP scripts through Apache Server. So just like the previous test.php file, create a similar file under /var/www/html directory:

$sudo nano test.php

Please be careful about editting files under /var/www/html directory. Because of this directory’s default permissions, you need to use sudo at the beginning of the nano command. Otherwise, it will give you an error during saving.

Copy and paste the below text:


echo "<h1>Hello World!</h1>";


Then open your browser and add test.php file path to your localhost URL as:


You will see:

What the hell! Apache server has just served the related file but it’s not executing the PHP script. Instead of that, it is showing the code snippet exactly. Because by default PHP interpreter is not active on Apache server.

In Apache environment, Apache server has lots of modules. In order to enable the PHP script running feature, we need to enable its related module. First, we need to install the related module in the terminal:

$sudo apt install libapache2-mod-php7.3

Second, we need to enable this installed module on Apache:

$sudo a2enmod php7.3

An error has occurred! Because mpm_event module creates a conflict. In order to solve this problem first disable this module by:

$sudo a2dismod mpm_event

Then try to enable php7.3 module again:

$sudo a2enmod php7.3

Finally, just restart your Apache server to activate new configurations:

$sudo service apache2 restart

Now open your browser again enter this link again:


Tadaa! Now you can run any PHP scripts under the /var/www/html directory through your browser.

NOTE: Sometimes you may not experience the Apache Server not running the php script case. For example using apt-get instead of apt command for php7.3 installation may install libapache2-mod-php7.3 package already together and during apache2 installation, the related apache module will already be enabled. I just want to give you the necessary background of how PHP and Apache is working together by showing this situation.

This article doesn’t cover how to configure a virtual host on your computer. By using virtual hosts, you can configure fake URLs working in your local machine. For example, you can make pointing your PHP project. But this article is written as a basic entry on how to install PHP and Apache on your computer.

NOTE: To see the related output in the terminal or your browser, you have to use some PHP functions as echo, var_dump, and print_r. You can see their usage and behavior as:

For echo:
For var_dump():
For print_r():

Thanks for reading this article. I hope you can easily install PHP and Apache on your machine and start to code right away. 
If you have any questions, comment down below. If you want to learn more about Web Development, please subscribe to our Blog Website:

4 comments on “How to install PHP and Apache on Linux (Ubuntu, Linux Mint)?

  1. Oh my goodness! Awesome article dude! Thanks, However I am having
    troubles with your RSS. I don’t understand the reason why I can’t join it.
    Is there anyone else having similar RSS problems?
    Anyone that knows the answer can you kindly respond? Thanx!!

Leave a Reply

Your email address will not be published. Required fields are marked *