How to install Apache, PHP and MySQL (LAMP) on Ubuntu

Post Reply
User avatar
Spray
Former staff
Former staff
Posts: 630
https://www.youtube.com/channel/UC40BgXanDqOYoVCYFDSTfHA
Joined: Wed Dec 28, 2011 10:41 pm
Location: Oregon

How to install Apache, PHP and MySQL (LAMP) on Ubuntu

Post by Spray »

This guide will show you how to install Apache2, PHP and MySQL on a Ubuntu Linux VDS. I'll write this assuming this is a fresh install of Ubuntu 18.04, but it would work on most builds.

The easiest way to do this would be to use the VNC Console tab in your control panel, but it would be smarter to use SSH, with a client such as Putty, available here.

The first thing you'll want to do is make sure that everything is updated on the operating system. Go ahead and enter this command:

Code: Select all

sudo apt-get update

Apache2

Once that's done its business, you can start installing Apache2 with this command:

Code: Select all

sudo apt-get install apache2
It will check for the package, then ask you to confirm the installation. Press "y", then hit Enter.

When this finishes, Apache2 will already be running and your webserver will be active. You can go to "http://www.xxx.yyy.zzz/" - where "www.xxx.yyy.zzz" is the IP address for your VDS - in your web browser and see the Apache test page.


MySQL

Next, we want to install MySQL. We'll use the following command to install MySQL Server 5.7, and the tools it needs to talk to Apache and PHP:

Code: Select all

sudo apt-get install mysql-server
Again, we'll hit "y" and Enter to confirm. You'll then be taken to a purple screen with a gray dialogue, asking for a root password to be set for MySQL. Please choose this password carefully and save it someplace you'll be able to remember it -- You'll need it later! It'll ask you to confirm immediately after.

Now you'll want to do a bit of housekeeping for MySQL:

Code: Select all

sudo mysql_secure_installation
This will clean up MySQL and close some security holes that are set by default on MySQL. It will ask you if you want to install the validate password plugin. This is a personal preference, but is safe to leave disabled as long as you choose strong passwords. It will ask for your root password, then ask you if you want to change your root password. Press "n", then Enter. To then remove anonymous users, press "y", then Enter. To disallow root logins remotely, press "y", then Enter. To remove the test database, press "y", then enter. You'll then want to make the changes take place immediately, so press "y", then Enter. The MySQL installation is now finished.

Next, we'll need to alter the root user account, to switch from auth_socket authentication to mysql_native_password, to allow local, root logins from phpMyAdmin later.

First, open a local MySQL connection using:

Code: Select all

sudo mysql
You will see a mysql> prompt. Use the following command to alter the root user account, ensuring to adjust the quoted password to your defined password:

Code: Select all

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password_here';
Then, you will want to flush privileges so that this takes effect:

Code: Select all

FLUSH PRIVILEGES;
Finally, exit MySQL using:

Code: Select all

exit
PHP

To install PHP, run the following command:

Code: Select all

sudo apt-get install php libapache2-mod-php php-mysql
You'll need to confirm this installation with "y" and Enter, as well.

Now, you'll want to make sure .php files take priority over .html when not specifying, so you'll want to use this:

Code: Select all

sudo nano /etc/apache2/mods-enabled/dir.conf
This will open the index files list in Nano, a text editor. The list will look like this:

Code: Select all

<IfModule mod_dir.c>
        DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
Navigating with your arrow keys, you'll want to delete index.php from where it is currently, and re-enter it just in front of index.html. After doing so, press CTRL+X to exit, then hit "y" to save, and press Enter to save to the same file we opened.

Now we need to restart Apache, using this:

Code: Select all

sudo service apache2 restart
phpMyAdmin

Now we want to install phpMyAdmin, to give you a web-based UI to interact with the MySQL server. To do this, you'll run:

Code: Select all

sudo apt-get install phpmyadmin
Again, you'll confirm with "y" and Enter.

Another purple screen will pop up, and Apache2 will be highlighted, but not selected. You need to hit the SPACE key to select it, then press Enter. After a moment, you'll see another purple screen, asking if you need to configure the database for phpmyadmin with dbconfig-common. Select Yes and create a password for the phpMyAdmin user for MySQL. You don't need to use this user account to login, this is used for phpMyAdmin for some underlying operations.

When this is complete, you can go to http://www.xxx.yyy.zzz/phpmyadmin/ and access the installation of phpMyAdmin!

NOTE: The root directory of your webserver will be in /var/www/html/ and files can be uploaded there to be viewed in the browser.

Also, you do not want to use the root account on MySQL, generally. Log into phpMyAdmin with the root account, and create a secondary account and then use that account. It's useful for keeping your server secure.
Post Reply