Introduction
In this tutorial we will show you how to install an Apache web server with PHP 7 and MySQL support on Ubuntu/Debian.
Prerequisites
- Ubuntu or Debian server
- About 10 minutes of time
- Full root SSH (Secure Shell) access
- Updated dependencies (Run sudo apt update)
Step 1 - Setting up Apache Web Server
Apache Web Server is used to serve your site content via HTTP, so it is easily reachable for all web browsers. In this step we will cover how to implement Apache Web Server on your Ubuntu/Debian system.
Step 1.1 - Downloading Apache Web Server
To install Apache Web Server on our server we will use the following command to download a Apache via APT (Advanced Packaging Tool):
sudo apt install apache2
Apache Web Server should now be installed. To verify that it worked you can visit your server's public IP address using your web browser. A default Apache page should show up.
Note: If you do not know your server's public IP address, jump to the next step.
Step 1.2 - Finding public IP address (Optional)
We will use curl to find out our server's public IP address. It shows us how our server's IP is seen by a different server on the internet.
Install curl by running the following command:
sudo apt install curl
After it is installed, we can utilize it to show us our IP address:
curl -4 https://ip.hetzner.com
Our server's IP address will be shown, which we can use to verify that Apache works by inserting it into our web browser. If the default apache page is shown, the installation was successful.
Step 2 - Setting up MySQL
MySQL is used for database management and is used as a dependency for many common web applications such as Wordpress, Joomla or Drupal.
This part of the tutorial covers the steps to install the service.
Feel free to skip this step if you do not wish to implement MySQL support.
Step 2.1 - Downloading MySQL
To install MySQL on our server we will use the following command to download a MySQL server and client via APT:
sudo apt install mysql-server mysql-client
Step 2.2 - MySQL Secure Installation
This security script ensures that insecure anonymous users and default settings are changed to keep your database hidden from potential attackers. Run the following command:
mysql_secure_installation
You will be asked a few questions. This is what it will look like:
Securing the MySQL server deployment.
Enter password for user root:
Enter your MySQL root password here.
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No:
Choose "Y" here if you would like to enable the password validation. This is up to you. I will not be using it.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) :
It is recommended to remove anonymous users for improved security.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) :
--
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) :
--
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) :
Press Y to all of these for optimal security.
Step 3 - Setting up PHP
PHP is used to execute scripts and including programming functions into website code, which will then be showed on your website. It is also required for most Content Management Systems.
Step 3.1 - Installing PHP
Run the following command to install PHP and its connection with MySQL:
sudo apt install php libapache2-mod-php php-mysql
Note: If you decided not to install MySQL earlier, leave "php-mysql" out of your command.
PHP will be fully installed after. We will verify this now by creating a new PHP file that shows us our server's software information. This is done by using the notorious info.php file.
As Apache's default location is /var/www/html, we will create a new info.php file using Nano Text Editor by running this command:
sudo nano /var/www/html/info.php
Nano text editor will now be opened - It allows you to create and edit any type of text file. We will use it to run our info script with PHP. Insert the following text into the info.php file:
<?php
phpinfo();
?>
We will save the file and close Nano Text Editor by holding CTRL + X.
To run the info.php file we will open the page in our web browser.
http://<your_host>/info.php
Note: Use the server IP you found out in Step 1.2.
Make sure to remove the info.php file before uploading any production applications. You do not want your server's information be open to the public. It is recommended to share as little information about your hardware as possible to not attract attackers.
Conclusion
Apache, MySQL and PHP are now running on our server and harmonizing together. You can now upload your files and scripts to the /var/www/html/ folder and start using your web server.