What is localhost, what does it do?
Localhost is the computer’s ability to operate as a server thanks to the software installed on the computer. We cannot run PHP files on a normal computer and do not perform operations on the database. Thanks to the installation of the localhost, our computer will gain a server feature and we will be able to run files such as
We can access the localhost by typing
http://localhost in our browser or at
As we have seen, we encountered a blank page in our browser as there was no installation.
Ubuntu Localhost setup
Now let’s come to the installation of Localhost in
Ubuntu; we will do our operations from the terminal (end unit). First, we update the packages. Our command is:
sudo apt update
Ubuntu Apache Web Server (Server) Setup
After updating our packages, we can start the installation. First, we will install Apache. Thanks to this process, our computer will have gained a server feature.
The command we will use is as follows:
sudo apt install apache2
After typing the command, we enter our password and continue processing. While installing, we will get
y/n to download the files, type y and press
ENTER to continue.
After the installation, our server is ready. Since our server is ready, we can see that our server has been installed by typing
http://127.0.0.1 in the search bar of our browser.
Ubuntu PHP and MySQL setup
After the server was installed, it was time for the software we will use on this server. We will install PHP using the commands below.
sudo apt install php
PHP will be installed with the command. The latest version of PHP will be loaded with this command. But if you want to stay an older version, you can use the command (For example;
sudo apt install php5). As it seems, the latest version of PHP, PHP7.2, has been installed.
Now we will install PHP-MySQL. We will use the following command as we want the updated files again in the installation. Those who want a specific version can still download with the version code. (For example:
sudo apt install php5-mysql)
sudo apt install php-mysql
Now we will install the MySQL server. There are some necessary points to consider in this setup. First, we will start the installation with the command below.
sudo apt install mysql-server
You will be asked to enter the “root” password to be used in MySQL. We will use the password when installing PHPMyAdmin in the future. In the meantime, we will press the TAB key to change the options and confirm with the ENTER key.
PHP and MySQL installation is now complete. We will restart the server because we have added new software on the server and to make these software work without any problems.
sudo /etc/init.d/apache2 restart
Now let’s check if PHP files are working or not since our installation is finished. If we have an existing PHP file, you can throw it into
/var/www/html/ directory and check it from the browser at
http: //localhost/filename.php. If not, you can check if PHP is working by following the next steps.
First, we will create a test page using the code below.
sudo nano /var/www/html/trial.php
Then we will write a PHP code as below and then press CTRL and X and press y to save our operation.
We will check if PHP is working now. Since we call our PHP file trial.php, we will write
http://localhost/trial.php in the browser.
If we see it with a page like the one below, it means it is ringing.
Installing phpMyAdmin on Ubuntu
Apache Server installed, PHP installed, MySQL Server installed, and phpMyAdmin installed. By installing phpMyAdmin, we will be able to create and manage a database. We will start the installation with the command below.
sudo apt install phpmyadmin
After entering the command and starting the download process, we will see a screen like below. Since we have installed the apache server, we will select and confirm the apache option.
Then the config page will come and we will say yes to this and move on to the next step.
Now we will enter the password so that the phpMyAdmin and MySQL server can match. The point to be noted is the following; We will use the password created here when installing MySQL server. It will not work if you enter the wrong password.
Finally, we will link with the code below to run phpMyAdmin on the localhost.
sudo ln -sf /usr/share/phpmyadmin /var/www/html/phpmyadmin
After this process, we will go to
http://localhost/phpmyadmin/ in our browser to check if phpMyAdmin is working.
If you have not had problems so far, we will now be able to run PHP files and create databases on the localhost.
I hope it was a useful article.