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
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.
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.
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.
By default, the MaraiDB package on Ubuntu uses unix_socket to authenticate user login, which basically means you can use 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.
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.
I follow step by step tutorial and it worked. Thank you very much
To continue with this, usually you’ll want to install phpmyadmin.
sudo apt-get install phpmyadmin
Make sure to hit “space” to select apache2 during configuration, just hitting enter doesn’t actually select the option. It is selected when you see an asterisk inside the brackets. (ie. [*] )
Due to the new security, you can’t access phpmyadmin as root anymore. You can workaround this by doing the following:
sudo mysql –user=root mysql
Then:
(Replace ‘yourPasswordHere’ with your desired password.)
CREATE USER ‘phpmyadmin’@’localhost’ IDENTIFIED BY ‘yourPasswordHere’;
GRANT ALL PRIVILEGES ON *.* TO ‘phpmyadmin’@’localhost’ WITH GRANT OPTION;
Followed by:
FLUSH PRIVILEGES;
Then type “quit” to exit the mariadb monitor.
You’ll now have full access via the phpmyadmin user.
Great instructions, worked perfectly! Thanks!
Very good. Easy to understand. No unnecessary steps or distractions. First rate tutorial.
mariadb is not installing …….The following packages have unmet dependencies:
mariadb-server : Depends: mariadb-server-10.1 (>= 10.1.30-0ubuntu0.17.10.1) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
Fantastic worked like a charm
Hi man, how are you?
Your tutorial is perfect, I installed the linux stack successfully, but I want to configure phmmyadmin, do you have any tutorials to follow?
This article is for Ubuntu 17.10, which reached end of life.
You can set up LAMP on Ubuntu 18.04 following the tutorial below.
How to Install LAMP Stack on Ubuntu 18.04 Server/Desktop
And then install phpMyAdmin.
How to Install phpMyAdmin with Apache (LAMP) on Ubuntu 18.04
Hi Xiao, thanks for answering my question.