How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 18.04

Introduction

Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu, A “LAMP” stack is a collection of open-supply software program.

This is commonly hooked up together to permit a server to host dynamic websites and web apps.

This term is sincerely an acronym which represents the Linux running system, with the Apache net server.

The website information is stored in a MySQL database, and dynamic content is processed by using PHP.

In this manual, we will set up a LAMP stack on an Ubuntu 18.04 server.

Prerequisites

Install Linux Apache MySQL PHP (LAMP) stack on Ubuntu,In order to finish this tutorial.

You may need to have an Ubuntu 18.04 server with a non-root sudo-enabled person account and a fundamental firewall.

This may be configured the usage of our initial server setup guide for Ubuntu 18.04.

Install Linux Apache MySQL PHP (LAMP) stack on Ubuntu,Step 1 — Installing Apache and Updating the Firewall

The Apache web server is among the most famous net servers in the international.

It’s nicely-documented and has been in huge use for a whole lot of the records of the web, which makes it a awesome default choice for web hosting a website.

Install Apache the use of Ubuntu’s package manager, apt:

$ sudo apt update
$ sudo apt install apache2

Since this is a sudo command, these operations are accomplished with root privileges. It will ask you to your regular user’s password to confirm your intentions.

Once you’ve got entered your password, apt will tell you which ones applications it plans to put in and what kind of more disk space.

They’ll take up. Press Y and hit ENTER to keep, and the installation will proceed.

Install Linux Apache MySQL PHP (LAMP) stack on Ubuntu,Adjust the Firewall to Allow Web Traffic

Install Linux Apache MySQL PHP (LAMP) stack on Ubuntu,Next, assuming which you have observed the initial server setup commands and enabled the UFW firewall.

Ensure that your firewall lets in HTTP and HTTPS site visitors. You can take a look at that UFW has an application profile for Apache like so:

$ sudo ufw app list
Output
Available applications:
  Apache
  Apache Full
  Apache Secure
  OpenSSH

If you examine the Apache Full profile, it must show that it allows visitors to ports 80 and 443:

$ sudo ufw app info "Apache Full"
Output
Profile: Apache Full
Title: Web Server (HTTP,HTTPS)
Description: Apache v2 is the next generation of the omnipresent Apache web
server.

Ports:
  80,443/tcp

Allow incoming HTTP and HTTPS visitors for this profile:

$ sudo ufw allow in "Apache Full"

Install Linux Apache MySQL PHP (LAMP) stack on Ubuntu,You can do a gap take a look at right away to affirm that the whole thing went as deliberate via visiting your server’s.

public IP deal with on your net browser (see the be aware underneath the subsequent heading to find out what your public IP cope with is if you do now not have this statistics already):

http://your_server_ip

You will see the default Ubuntu 18.04 Apache internet web page, that is there for informational and testing purposes. It should appearance some thing like this:

If you notice this page, then your web server is now effectively established and handy through your firewall.

Install Linux Apache MySQL PHP (LAMP) stack on Ubuntu,How To Find your Server’s Public IP Address

If you do no longer know what your server’s public IP cope with is, there are some of ways you may discover it.

Usually, this is the address you use to hook up with your server via SSH.

There are a few extraordinary methods to do this from the command line. First.

you could use the iproute2 gear to get your IP cope with via typing this:

$ ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

This will give you two or three traces returned. They are all correct addresses.

However your laptop might also most effective be capable of use certainly one of them, so experience unfastened to try every one.

An opportunity approach is to use the curl application to touch an out of doors birthday party to inform you how it sees your server.

This is carried out with the aid of asking a selected server what your IP address is:

$ sudo apt install curl
$ curl http://icanhazip.com

Regardless of the method you operate to get your IP address, type it into your net browser’s address bar to view the default Apache page.

Install Linux Apache MySQL PHP (LAMP) stack on Ubuntu,Step 2 — Installing MySQL

Install Linux Apache MySQL PHP (LAMP) stack on Ubuntu,Now which you have your web server up and going for walks.

it is time to put in MySQL. MySQL is a database control gadget.

Basically, it’ll arrange and provide get admission to to databases wherein your website can shop data.

Again, use apt to collect and installation this software program:

$ sudo apt install mysql-server

Note: In this case, you do now not have to run sudo apt replace prior to the command. This is because you lately ran it in the instructions above to put in Apache. The package deal index in your laptop have to already be up-to-date.

This command, too, will show you a listing of the programs on the way to be set up, along side the amount of disk space they will absorb. Enter Y to maintain.

When the set up is whole, run a simple safety script that comes pre-hooked up with MySQL to be able to put off a few risky defaults and lock down get admission to for your database machine. Start the interactive script via strolling:

$ sudo mysql_secure_installation

This will ask in case you want to configure the VALIDATE PASSWORD PLUGIN.

Note: Enabling this option is something of a judgment name. If enabled, passwords which don’t suit the required criteria might be rejected via MySQL with an error.

This will motive problems in case you use a weak password in conjunction with software which mechanically configures MySQL user credentials.

including the Ubuntu packages for phpMyAdmin. It is safe to go away validation disabled, but you must continually use robust, unique passwords for database credentials.

Answer Y for sure, or whatever else to keep without allowing.

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:

If you solution “yes”, you will be asked to pick out a level of password validation.

Keep in mind that in case you input 2 for the most powerful level, you’ll receive errors when trying to set any password.

Which does not contain numbers, higher and lowercase letters, and unique characters, or which is based on not unusual dictionary phrases.

Install Linux Apache MySQL PHP (LAMP) stack on Ubuntu

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1

Regardless of whether or not you chose to set up the VALIDATE PASSWORD PLUGIN, your server will subsequent ask you to pick and affirm a password for the MySQL root user.

This is an administrative account in MySQL that has elevated privileges. Think of it as being similar to the basis account for the server itself (despite the fact that the one you are configuring now’s a MySQL-particular account).

Make positive that is a sturdy, particular password, and do now not depart it blank.

If you enabled password validation, you will be proven the password strength for the foundation password you simply entered and your server will ask in case you need to exchange that password.

If you’re happy along with your modern-day password, enter N for “no” at the prompt:

Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n

Install Linux Apache MySQL PHP (LAMP) stack on Ubuntu,For the rest of the questions, press Y and hit the ENTER key at each prompt.

This will remove some anonymous customers and the test database, disable faraway root logins, and cargo those new policies so that MySQL immediately respects the adjustments you have got made.

Note that during Ubuntu systems going for walks MySQL five.7 (and later variations).

The foundation MySQL person is about to authenticate the usage of the auth_socket plugin with the aid of default in place of with a password.

This lets in for a few greater protection and usability in lots of cases.

But it may also complicate things whilst you want to allow an outside program (e.G., phpMyAdmin) to get entry to the person.

If you favor to use a password when connecting to MySQL as root.

You’ll want to switch its authentication method from auth_socket to mysql_native_password.

To do this, open up the MySQL set off from your terminal:

$ sudo mysql

Next, check which authentication method each of your MySQL consumer bills use with the subsequent command:

mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;
+------------------+-------------------------------------------+-----------------------+-----------+
| user             | authentication_string                     | plugin                | host      |
+------------------+-------------------------------------------+-----------------------+-----------+
| root             |                                           | auth_socket           | localhost |
| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)

In this example, you may see that the root consumer does in fact authenticate the usage of the auth_socket plugin.

To configure the foundation account to authenticate with a password, run the subsequent ALTER USER command. Be positive to change password to a sturdy password of your choosing:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Then, run FLUSH PRIVILEGES which tells the server to reload the provide tables and placed your new adjustments into impact:

mysql> FLUSH PRIVILEGES;

Check the authentication techniques employed by way of every of your customers again to confirm that root no longer authenticates the usage of the auth_socket plugin:

mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;
output
+------------------+-------------------------------------------+-----------------------+-----------+
| user             | authentication_string                     | plugin                | host      |
+------------------+-------------------------------------------+-----------------------+-----------+
| root             | *3636DACC8616D997782ADD0839F92C1571D6D78F | mysql_native_password | localhost |
| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)

You can see in this example output that the foundation MySQL person now authenticates the use of a password.

Once you verify this on your very own server, you can go out the MySQL shell:

mysql> exit

At this factor, your database system is now set up and you can flow on to putting in PHP, the very last issue of the LAMP stack.

Step 3 — Installing PHP

PHP is the component of your setup with a purpose to manner code to show dynamic content.

It can run scripts, connect with your MySQL databases to get data, and hand the processed content material over on your web server to show.

Once once more, leverage the apt gadget to install PHP. In addition, consist of a few helper packages this time in order that PHP code can run underneath the Apache server and speak on your MySQL database:

$ sudo apt install php libapache2-mod-php php-mysql

This must deploy PHP without any problems. We’ll check this in a second.

In maximum instances, you will want to adjust the manner that Apache serves documents when a directory is asked. Currently.

if a consumer requests a listing from the server, Apache will first look for a file known as index.Html.

We want to inform the net server to decide on PHP files over others, so make Apache look for an index.Hypertext Preprocessor document first.

To try this, type this command to open the dir.Conf record in a text editor with root privileges:

$ sudo nano /etc/apache2/mods-enabled/dir.conf

It will look like this:

/etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c>
    DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>

Move the PHP index document (highlighted above) to the first position after the DirectoryIndex specification, like this:

/etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

When you are finished, store and near the report through pressing CTRL+X. Confirm the shop by using typing Y after which hit ENTER to affirm the document save vicinity.

After this, restart the Apache web server so as in your modifications to be identified. Do this through typing this:

$ sudo systemctl restart apache2

You also can take a look at at the repute of the apache2 provider using systemctl:

$ sudo systemctl status apache2
Sample Output
● apache2.service - LSB: Apache2 web server
   Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           └─apache2-systemd.conf
   Active: active (running) since Tue 2018-04-23 14:28:43 EDT; 45s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 13581 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS)
  Process: 13605 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCESS)
    Tasks: 6 (limit: 512)
   CGroup: /system.slice/apache2.service
           ├─13623 /usr/sbin/apache2 -k start
           ├─13626 /usr/sbin/apache2 -k start
           ├─13627 /usr/sbin/apache2 -k start
           ├─13628 /usr/sbin/apache2 -k start
           ├─13629 /usr/sbin/apache2 -k start
           └─13630 /usr/sbin/apache2 -k start

Install Linux Apache MySQL PHP (LAMP) stack on Ubuntu

Press Q to exit this popularity output.

To beautify the capability of PHP, you have the choice to put in some additional modules.

To see the available alternatives for PHP modules and libraries, pipe the consequences of apt search into less, a pager which helps you to scroll thru the output of different commands:

$ apt search php- | less

Use the arrow keys to scroll up and down, and press Q to stop.

The effects are all elective additives that you could set up. It will provide you with a quick description for every:

bandwidthd-pgsql/bionic 2.0.1+cvs20090917-10ubuntu1 amd64
  Tracks usage of TCP/IP and builds html files with graphs

bluefish/bionic 2.2.10-1 amd64
  advanced Gtk+ text editor for web and software development

cacti/bionic 1.1.38+ds1-1 all
  web interface for graphing of monitoring systems

ganglia-webfrontend/bionic 3.6.1-3 all
  cluster monitoring toolkit - web front-end

golang-github-unknwon-cae-dev/bionic 0.0~git20160715.0.c6aac99-4 all
  PHP-like Compression and Archive Extensions in Go

haserl/bionic 0.9.35-2 amd64
  CGI scripting program for embedded environments

kdevelop-php-docs/bionic 5.2.1-1ubuntu2 all
  transitional package for kdevelop-php

kdevelop-php-docs-l10n/bionic 5.2.1-1ubuntu2 all
  transitional package for kdevelop-php-l10n
…
:

To study extra approximately what every module does, you can search the net for extra statistics approximately them. Alternatively.

Take a look at the long description of the bundle with the aid of typing:

$ apt show package_name

There might be plenty of output, with one subject known as Description with the intention to have an extended explanation of the capability that the module gives.

For instance, to discover what the php-cli module does, you could type this:

$ apt show php-cli

Along with a large amount of other information, you may discover something that seems like this:

Output
…
Description: command-line interpreter for the PHP scripting language (default)
 This package provides the /usr/bin/php command interpreter, useful for
 testing PHP scripts from a shell or performing general shell scripting tasks.
 .
 PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used
 open source general-purpose scripting language that is especially suited
 for web development and can be embedded into HTML.
 .
 This package is a dependency package, which depends on Ubuntu's default
 PHP version (currently 7.2).
…

After learning, you decide you would love to put in a bundle, you can do so by way of using the apt installation command like you have been doing for the other software program.

you made a decision that php-cli is some thing which you want, you may kind:

$ sudo apt install php-cli

you want to put in a couple of module, you may try this by using listing every one, separated via a area, following the apt installation command, like this:

$ sudo apt install package1 package2 ...

At this factor, your LAMP stack is set up and configured. Before making any greater changes or deploying an software, though.

it’d be beneficial to proactively check out your PHP configuration in case there are any issues that should be addressed.

Step 4 — Testing PHP Processing on your Web Server

order to test that your system is configured well for PHP, create a very fundamental PHP script known as info.Hypertext Preprocessor.

order for Apache to discover this file and serve it effectively, it need to be stored to a completely particular listing, which is called the “internet root”.

Ubuntu 18.04, this directory is located at /var/www/html/. Create the file at that location by walking:

$ sudo nano /var/www/html/info.php

This will open a clean file. Add the following textual content, which is valid PHP code, within the file:

info.php
<?php
phpinfo();
?>

When you’re finished, store and near the file.

Now you can take a look at whether or not your internet server is able to correctly display content generated via this PHP script.

To do this out, visit this web page to your internet browser. You’ll want your server’s public IP address again.

The cope with you will need to go to is:

http://your_server_ip/info.php

The page that you come to should look some thing like this:

This page provides a few simple records approximately your server from the angle of PHP.

It is useful for debugging and to make certain that your settings are being applied efficiently.

If you may see this web page in your browser, then your PHP is running as expected.

You probably need to dispose of this file after this take a look at because it could clearly give statistics about your server to unauthorized users.

To do that, run the following command:

$ sudo rm /var/www/html/info.php

You can constantly recreate this web page if you want to get admission to the records again later.

Conclusion

Now which you have a LAMP stack mounted, you’ve got many alternatives for what to do next.

Basically, you have hooked up a platform so as to will let you set up maximum sorts of websites and net software program in your server.

As an immediate subsequent step, you ought to ensure that connections in your web server are secured, by way of serving them through HTTPS.

The simplest alternative right here is to apply Let’s Encrypt to relaxed your web page with a loose TLS/SSL certificates.

Some different famous options are:

  • Install WordPress the most popular content material control device on the net.
  • Set Up PHPMyAdmin to help manipulate your MySQL databases from net browser.
  • Learn how to use SFTP to transfer files to and from your server.