How to Install LAMP Stack (Apache, MariaDB, PHP7.1) on Ubuntu 17.10

This tutorial is going to show you how to install Apache, MariaDB and PHP7.1 (LAMP) on Ubuntu 17.10. You can follow this tutorial on a VPS (Virtual Private Server) or on a local Ubuntu 17.10 computer.

Step 1: Update Software Packages

Before we install the LAMP stack, it’s a good idea to update repository and software packages. Run the following command on your Ubuntu 17.10 OS.

sudo apt update

sudo apt upgrade

Step 2: Install Apache Web Server

Enter the following command to install Apache Web server. The apache2-utils package will install some useful utilities like Apache HTTP server benchmarking tool (ab).

sudo apt install -y apache2 apache2-utils

Ubuntu 17.10 LAMP

After it’s installed, Apache should be automatically started. Check its status with systemctl.

systemctl status apache2

Output:

 apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: 
  Drop-In: /lib/systemd/system/apache2.service.d
           └─apache2-systemd.conf
   Active: active (running) since Sat 2017-10-21 14:37:01 CST; 3s ago
  Process: 17568 ExecStop=/usr/sbin/apachectl stop (code=exited, status=0/SUCCES
  Process: 17573 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCC
 Main PID: 17577 (apache2)
    Tasks: 55 (limit: 4915)
   CGroup: /system.slice/apache2.service
           ├─17577 /usr/sbin/apache2 -k start
           ├─17578 /usr/sbin/apache2 -k start
           └─17579 /usr/sbin/apache2 -k start

If it’s not running, use systemctl to start it.

sudo systemctl start apache2

It’s also a good idea to enable Apache to automatically start at boot time.

sudo systemctl enable apache2

Check Apache version:

apache2 -v

Output:

Server version: Apache/2.4.27 (Ubuntu)
Server built: 2017-09-18T15:05:48

Now type in the public IP address of your Ubuntu 17.10 server in the browser address bar. You should see “It works!” Web page, which means Apache Web server is running properly. If you are installing LAMP on your local Ubuntu 17.10 computer, then type 127.0.0.1 or localhost in the browser address bar.

Ubuntu 17.10 apache web server

Now we need to set www-data (Apache user) as the owner of document root. By default it’s owned by the root user.

sudo chown www-data:www-data /var/www/html/ -R

Step 3: Install MariaDB Database Server

MariaDB is a drop-in replacement for MySQL. Enter the following command to install it on Ubuntu 17.10.

sudo apt install mariadb-server mariadb-client

After it’s installed, MariaDB server should be automatically stared. Use systemctl to check its status.

systemctl status mariadb

Output:

 mariadb.service - MariaDB database server
   Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: 
   Active: active (running) since Sat 2017-10-21 14:53:04 CST; 11s ago
 Main PID: 19695 (mysqld)
   Status: "Taking your SQL requests now..."
    Tasks: 26 (limit: 4915)
   CGroup: /system.slice/mariadb.service
           └─19695 /usr/sbin/mysqld

If it’s not running, start it with this command:

sudo systemctl start mariadb

To enable MariaDB to automatically start at boot time, run

sudo systemctl enable mariadb

Now run the post installation security script.

sudo mysql_secure_installation

When it asks you to enter MariaDB root password, press Enter key as the root password isn’t set yet. Then enter y to set the root password for MariaDB server.

Ubuntu 17.10 MariaDB server

Next, you can press Enter to answer all remaining questions, which will remove anonymous user, disable remote root login and remove test database. This step is a basic requirement for MariaDB database security.

install lamp stack on ubuntu 17.10

By default, the MaraiDB package on Ubuntu uses unix_socket to authenticate user login, which basically means you can username and password of the OS to log into MariaDB console. So you can run the following command to login without providing MariaDB root password.

sudo mariadb -u root

To exit, run

exit;

Check MariaDB server version information.

mariadb --version

Output:

mariadb Ver 15.1 Distrib 10.1.25-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

Step 4: Install PHP7.1

At the the time of this writing, PHP7.1 is the latest stable version of PHP and has a minor performance edge over PHP7.0. Enter the following command to install PHP7.1.

sudo apt install php7.1 libapache2-mod-php7.1 php7.1-mysql php-common php7.1-cli php7.1-common php7.1-json php7.1-opcache php7.1-readline

Enable the Apache php7.1 module then restart Apache Web server.

sudo a2enmod php7.1

sudo systemctl restart apache2

Check PHP version information.

php --version

Output:

PHP 7.1.8-1ubuntu1 (cli) (built: Aug  8 2017 15:57:37) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.1.8-1ubuntu1, Copyright (c) 1999-2017, by Zend Technologies

To test PHP scripts with Apache server, we need to create a info.php file in the document root directory.

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

Paste the following PHP code into the file.

<?php phpinfo(); ?>

Save and close the file. Now in the browser address bar, enter server-ip-address/info.php. Replace sever-ip-address with your actual IP. If you follow this tutorial on your local computer, then type 127.0.0.1/info.php or localhost/info.php.

You should see your server’s PHP information. This means PHP scripts can run properly with Apache web server.

install php7.1 on ubuntu 17.10

Congrats! You have successfully installed Apache, MariaDB and PHP7.1 on Ubuntu 17.10. For your server’s security, you should delete info.php file now to prevent prying eyes.

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

That’s it! I hope this tutorial helped you install LAMP stack on Ubuntu 17.10. As always, if you found this post useful, then subscribe to our free newsletter to get new tutorials.

Rate this tutorial
[Total: 13 Average: 5]