DEV Community

Cover image for How To Install LAMP stack on Ubuntu
Eldaniz Babayev
Eldaniz Babayev

Posted on

How To Install LAMP stack on Ubuntu

Operating System: Ubuntu 20.04
Kernel Version: 5.8.0-48-generic

To use PHP as your local server, you have to install the LAMP stack first. Also, there is XAMPP, I'll use LAMP to show the pure server-side in Linux. Firstly, you have to install Apache2 with dpkg package management system:

sudo apt-get update
sudo apt-get install apache2
Enter fullscreen mode Exit fullscreen mode

Once the installation is finished, you have to update your firewall settings to allow HTTP(HyperText Transfer Protocol) traffic. UFW(Uncomplicated Firewall) has different application profiles that you can leverage for accomplishing that. To list all currently available UFW application profiles, you can run:

sudo ufw app list

Output:
Available applications:
  Apache
  Apache Full
  Apache Secure
  CUPS
Enter fullscreen mode Exit fullscreen mode

To only allow traffic on port 80, use the Apache profile:

sudo ufw allow in "Apache"
Enter fullscreen mode Exit fullscreen mode

After completing these steps, you can check out your localhost or localhost ip if it's working or not. You should see that page:
image
If you see this page, then your web server is now correctly installed and accessible through your firewall.

Now our web server is running and we have to set up our database. In general M in LAMP stand on MySQL, but I'll use MariaDB as my database. You can check out that link to look at function differences between MariaDB and MySQL.

sudo apt-get install mariadb-server
Enter fullscreen mode Exit fullscreen mode

These commands will install MariaDB, but will not prompt you to set a password or make any other configuration changes. Because the default configuration leaves your installation of MariaDB insecure, we will use a script that the mariadb-server package provides to restrict access to the server and remove unused accounts. For new MariaDB installations, the next step is to run the included security script. This script changes some of the less secure default options for things like remote root logins and sample users.

sudo mysql_secure_installation
Enter fullscreen mode Exit fullscreen mode

The first prompt will ask you to enter the current database root password. Since you have not set one up yet, press ENTER to indicate none. You should see something like that:

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! 
PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
Enter fullscreen mode Exit fullscreen mode

The next prompt asks you whether you’d like to set up a database root password. On Ubuntu, the root account for MariaDB is tied closely to automated system maintenance, so we should not change the configured authentication methods for that account. Doing so would make it possible for a package update to break the database system by removing access to the administrative account. Type N and then press ENTER.

OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorization.
Set root password? [Y/n] N
Enter fullscreen mode Exit fullscreen mode

On Ubuntu systems running MariaDB 10.5.9, the root MariaDB user is set to authenticate using the unix_socket plugin by default rather than with a password. This allows for some greater security and usability in many cases, but it can also complicate things when you need to allow an external program (for example: phpMyAdmin) administrative rights.

To this end, we will create a new account called admin with the same capabilities as the root account, but configured for password authentication. Open up the MariaDB prompt from your terminal:

sudo mariadb
Enter fullscreen mode Exit fullscreen mode

After completed step in the above, you can create a new user with root privileges and password-based access. Be sure to change the username and password to match your preferences:

MariaDB [(none)]> GRANT ALL ON *.* TO 'eldaniz'@'localhost' IDENTIFIED BY 'admin123' WITH GRANT OPTION;
Enter fullscreen mode Exit fullscreen mode

Flush the privileges to be aware that they are saved and available in the current session:

MariaDB [(none)]> FLUSH PRIVILEGES;
Enter fullscreen mode Exit fullscreen mode

For testing mariadb is working or not, you should write that command:

sudo systemctl status mariadb
Enter fullscreen mode Exit fullscreen mode

Output of that command should be like that:

● mariadb.service - MariaDB 10.5.9 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor prese>
    Drop-In: /etc/systemd/system/mariadb.service.d
             └─migrated-from-my.cnf-settings.conf
     Active: active (running) since Sun 2021-04-11 12:30:47 +04; 4h 28min ago
       Docs: man:mariadbd(8)
             <https://mariadb.com/kb/en/library/systemd/>
    Process: 742 ExecStartPre=/usr/bin/install -m 755 -o mysql -g root -d /var/>
    Process: 776 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_STA>
    Process: 783 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && V>
    Process: 1063 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_S>
    Process: 1071 ExecStartPost=/etc/mysql/debian-start (code=exited, status=0/>
   Main PID: 881 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 9 (limit: 4513)
     Memory: 74.4M
     CGroup: /system.slice/mariadb.service
             └─881 /usr/sbin/mariadbd
Enter fullscreen mode Exit fullscreen mode

If mariadb is not running, you can start it with
sudo systemctl start mariadb

You can try connecting to the database using the mysqladmin tool, which is a client that lets you run administrative commands. For example, this command says to connect to MariaDB as root using the Unix socket and return the version:

sudo mysqladmin version
Enter fullscreen mode Exit fullscreen mode

You will receive output similar to this:

mysqladmin  Ver 9.1 Distrib 10.5.9-MariaDB, for debian-linux-gnu on x86_64
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Server version      10.5.9-MariaDB-1:10.5.9+maria~focal
Protocol version    10
Connection      Localhost via UNIX socket
UNIX socket     /run/mysqld/mysqld.sock
Uptime:         4 hours 30 min 23 sec
Threads: 1  Questions: 58  Slow queries: 0  Opens: 32  Open tables: 25  Queries per second avg: 0.003
Enter fullscreen mode Exit fullscreen mode

The last step of our LAMP stack is the PHP. We have to use dpkg package controller to install php7.4 and it’s dependencies on Ubuntu:

sudo apt-get install php7.4 php7.4-cli php7.4-common php7.4-json php7.4-mysql php7.4-mbstring php7.4-mcrypt php7.4-zip php7.4-fpm libapache2-mod-php7.4
Enter fullscreen mode Exit fullscreen mode

You can check if php is working or not with that command:

php -v
Enter fullscreen mode Exit fullscreen mode

Output should be like this:

PHP 7.4.16 (cli) (built: May  17 2021 07:54:38) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.16, Copyright (c), by Zend Technologies
Enter fullscreen mode Exit fullscreen mode

Thanks for reading, You can connect with me:

GitHub
LinkedIn

Top comments (0)