LAMP – SSD Nodes https://www.ssdnodes.com VPS Cloud Hosting For Hundreds Less Sun, 18 May 2025 13:40:28 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.1 https://www.ssdnodes.com/wp-content/uploads/2024/09/fav.svg LAMP – SSD Nodes https://www.ssdnodes.com 32 32 Simple Guide – How to Install LAMP on Ubuntu 22.04 https://www.ssdnodes.com/blog/installing-lamp-on-ubuntu-22-04-lts-jammy-jellyfish/ https://www.ssdnodes.com/blog/installing-lamp-on-ubuntu-22-04-lts-jammy-jellyfish/#respond Mon, 15 Jan 2024 08:15:42 +0000 https://blog.ssdnodes.com/blog/?p=6865 Are you looking for a simple tutorial on how to install LAMP on Ubuntu 22.04? You're in the right place! In this guide, you'll learn what a LAMP stack is in Linux, what a LAMP stack is used for, and how to install LAMP on Ubuntu 22.04.

What is a LAMP Stack in Linux?

A LAMP stack in Linux refers to a software bundle comprising Linux as the operating system, Apache as the web server, MySQL (or MariaDB) as the database system, and PHP (or Perl/Python) as the scripting language, commonly used for web development.

What is a LAMP Stack Used for?

A LAMP stack is used for building and deploying dynamic websites and web applications, combining Linux, Apache, MySQL, and PHP (or Python/Perl) components.

What Does LAMP Stack Stand for?

LAMP Stands for "Linux, Apache, MySQL, and PHP." Together, these software technologies can be used to create a fully-functional web server.

Linux is the most popular, secure and open source operating system used in web servers.

Apache HTTP Server is a free and open-source web server that delivers web content through the internet.

MySQL is a relational database engine that allows you to store data and manage it.

PHP is a widely used open source and general purpose server side scripting language used mainly in web development to create dynamic websites and applications.

NOTE: In this tutorial, we’ll be using MariaDB— a backward compatible, drop-in replacement of the MySQL® Database Server, which includes all major open source storage engines, and allows you to manage relational databases for storing and organizing data.

Note

If you want to skip all the technical steps of setting up LAMP on your server and have it installed in minutes, then I have some great news for you! Our team of engineers has prepared a ready-to-use 1-click LAMP application for your convenience. Just choose a server, and while prompted to choose the operating system, choose LAMP from the dropdown menu. This will set up LAMP in minutes. However, if you want to learn how to do it yourself, then keep reading.

Installing LAMP on Ubuntu 22.04 - Prerequisites

Before you proceed you'll need the following:

  • Root access to your server or a sudo user.
  • An Ubuntu 22.04 server with a non-root user with sudo privileges. If you haven't noticed, we offer extremely powerful Ubuntu servers at the globe's most affordable prices. Take a look at our offerings and prepare for your mind to be blown 🤯.

Check out our How to access your server using SSH guide to learn how to access your server and create a sudo user.

How to Install LAMP on Ubuntu 22.04

How to Install LAMP on Ubuntu 22.04

To install LAMP on Ubuntu 22.04, you will use the apt package manager to install Apache, MySQL, and PHP, and then ensure they are properly configured and integrated for web development. Apply the following steps to accomplish this.

Step 1: Update The Package Cache

Before you install LAMP, start by updating the Ubuntu packages in the package manager cache to the latest available versions using the following command:

sudo apt update

install lamp on ubuntu 22.04: Updating the System

Step 2: Install the MariaDB Database Server

After updating our Ubuntu package cache, we will now install the MariaDB database server, the M in the LAMP acronym. We'll use MariaDB instead of MySQL because it includes more features and supports new storage engines, in addition to its high performance.

To install MariaDB, execute the following command:

sudo apt install mariadb-server mariadb-client

Tap the y key then Enter to continue the installation.

In the preceding command, you install two packages:

  • mariadb-server: The MariaDB database server which actually stores data.
  • mariadb-client: The MariaDB database client which allows you to interact with and manage the database server via the command line.

Once the installation is finished, verify that the MariaDB database server is running properly by executing the following command to check out the MariaDB service status:

sudo service mariadb status

The output should show that the service is enabled and running:

install lamp on ubuntu 22.04: MariaDB

Here, you can see that the service is active and running in the line "Active: active (running) ...".

Ensure That MariaDB Starts at Boot

To make sure that the MariaDB database server starts with the system at boot, use the enable subcommand of the systemctl command. To do so, execute the following command:

sudo systemctl enable mariadb.service

You should receive the following output:

Synchronizing state of mariadb.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable mariadb

Securing The MariaDB Database Server

MariaDB comes with some unsafe default settings which may expose your database server to dangerous security vulnerabilities. It's recommended that you run a security script that comes with MariaDB to strengthen your database server and minimize the risk of database intrusions or breaches.

To secure your MariaDB database server, execute the following command, where you will be presented with seven prompts:

First, run the script:

sudo mysql_secure_installation

You’ll be asked for your current root password:

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
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 

Tap Enter.

Next, you'll be asked whether you want to use the unix_socket authentication method:

OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

Switch to unix_socket authentication [Y/n] 

Tap Y to enable unix_socket authentication for better security.

The unix_socket authentication method uses the Unix user's credentials instead of a password to connect to the MariaDB account with the same username, which makes it a passwordless security mechanism, and because it depends on the user's credentials, it cannot be used to grant multiple Unix users access to a single MariaDB user account. With this, access to your MariaDB database server is limited to the Unix user, adding a security layer for your MariaDB account and preventing attackers from brute forcing or taking advantage of an accidental password leak.

The unix_socket authentication method is particularly strong due to the default sturdiness of Unix user security in preventing remote access. However, unskilled administration of your Unix user system may expose dangerous vulnerabilities. So, keep a wide and open eye on potential Unix user security issues such as weak passwords or accidental password exposure, excessive sudo permissions that allow users to execute commands of a different Unix user, scripts of other users executed by your MariaDB Unix user, running unsafe scripts, or weak remote access security.

Next, you'll be asked to change the root password:

Enabled successfully!
Reloading privilege tables..
 ... Success!

Change the root password? [Y/n] 

Tap Y to set a new password for root, and re-enter it for validation.

Next, you'll be asked to remove anonymous users:

Change the root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] 

Tap Y to remove the anonymous users that come with your MariaDB installation.

Next, you’ll be asked whether you want to disallow remote root logins:

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] 

Tap Y to disable remote root login.

Next, you’ll be asked whether you want to remove the test database that comes with your MariaDB installation:

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] 

Tap Y to remove your MariaDB test database and disable access to it.

In order for the changes you've made to take effect, you’ll be asked to reload your MariaDB privilege tables:

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] 

Tap Y to reload your MariaDB privilege tables. This will ensure that your changes will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

With this, your MariaDB installation is more secure than before, provided that you follow security best practices.

You now have the MariaDB database server installed, and you’re ready to store and manage your data. Next, you’ll need to serve the data you store using an HTTP server, which is the Apache HTTP server in the LAMP stack.

Step 3: Install the Apache HTTP Server

After installing and securing the MariaDB database server, the next step in our LAMP stack installation is installing the Apache HTTP server, which serves web content.

Use apt to install Apache using the following command:

sudo apt install apache2

How to install lamp on ubuntu 22.04: Installing Apache

You’ll be asked to confirm the installation. Tap the y key then Enter to continue.

Once the installation is finished, check Apache’s version to confirm that it was properly installed using the following command:

sudo apachectl -v

You should receive an output that looks as follows:

Server version: Apache/2.4.52 (Ubuntu)
Server built:   2022-06-14T12:30:21

Then check Apache’s status:

sudo systemctl status apache2

The output should show that the Apache service is enabled and running:

● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-08-02 19:00:13 UTC; 2min 31s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 52442 (apache2)
      Tasks: 55 (limit: 19072)
     Memory: 5.2M
        CPU: 44ms
     CGroup: /system.slice/apache2.service
             ├─52442 /usr/sbin/apache2 -k start
             ├─52444 /usr/sbin/apache2 -k start
             └─52445 /usr/sbin/apache2 -k start

You now have Apache installed. Next, you’ll install the PHP language.

Step 4: Install PHP

With MariaDB you can store and manage data, and the Apache HTTP server allows you to serve it. You now need to install PHP to dynamically display data, and allow the final user to interact with your web service via easy-to-use web forms or API calls.

Use the following command to install the main PHP package, along with basic PHP packages that allow you to interact with your database and HTTP server:

sudo apt install php php-mysql php-xml php-mbstring libapache2-mod-php

install lamp on ubuntu 22.04: Installing PHP

When prompted, tap the y key then Enter to continue the installation.

In the preceding command, you install the following packages:

  • php: The main PHP language package.
  • php-mysql: A package that allows PHP to communicate with MySQL-based databases such as MariaDB.
  • php-xml: A package that provides a DOM, SimpleXML, WDDX, XML, and XSL module for PHP.
  • php-mbstring: A package that provides the MBSTRING module for PHP, which is used to manage non-ASCII strings.
  • libapache2-mod-php: A package that allows Apache to handle PHP files.

To ensure PHP was successfully installed, check its version using the following command:

php -v

You should receive an output similar to the following:

PHP 8.1.2 (cli) (built: Jul 21 2022 12:10:37) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2, Copyright (c), by Zend Technologies

This means that you've successfully installed PHP.

Step 5: Testing with a Custom-Made Web Page

Installing lamp on ubuntu 22.04: Testing LAMP

After we've finished installing all the components of the LAMP stack, we will now test our Apache server by creating a small HTML file in an Apache Document Root folder –a folder that contains your website files.

To create a document root folder to serve our test file, create a new directory called mysite inside the /var/www directory, which is the default document root for Apache. Note that you can use your site's name instead of mysite:

sudo mkdir /var/www/mysite

Next, create a file called index.html inside your mysite directory:

sudo nano /var/www/mysite/index.html

Paste the following into the file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My website</title>
</head>
<body>
    <h1>Hello World!</h1>
    <p>This is the landing page of <strong>My Site</strong></p>
</body>
</html>

As you can see, this is straightforward HTML code.

Save and close the file.

For Apache to know about your mysite document root directory and serve its index.html on your server's IP address or domain, you will need to modify the default Apache configuration file. To do this, first open it using the following command:

sudo nano /etc/apache2/sites-enabled/000-default.conf

Then make sure to set the correct server name and document root path:

ServerName your_server_domain_or_IP
DocumentRoot /var/www/mysite

Run the following command to ensure that your new configuration file does not contain any errors:

sudo apache2ctl configtest

You might receive a AH00558: apache2: Could not reliably determine the server's fully qualified domain name message. You can ignore this message because it’s just an informational note that does not affect the purposes of this tutorial, and Apache will run as expected.

You should receive an output that ends with the following:

Syntax OK

This means you can safely reload Apache. Otherwise, you will get a specific description pointing out the error you have to fix.
Restart Apache for these modifications to take effect:

sudo systemctl restart apache2

After you do so, use your browser and navigate to your server’s domain name or IP address:

http://your_server_domain_or_IP

You should see that the index.html file you created earlier is properly served.

Important Note:

What we’ve done in this article, is suitable for testing purposes and personal use, whereas if you want to publish your site to production, and securely process your customers’ online transactions, we do suggest you check our How To Install Let’s Encrypt on Ubuntu 24.04 blog article, that explains in detail how to secure your site with Let's Encrypt TLS/SSL certificates.

Reader Alert!

If you feel that the technical instructions in this tutorial are time-consuming, or beyond your expertise, you can choose a very convenient and practical solution, ready-made, fully and professionally tested, and developed by SSD Nodes (That is us 😊). Just visit our website, choose the server’s specifications that fit your needs, and while prompted to choose among the operating systems and the 1-Click Applications we have, choose LAMP from the dropdown menu, complete your checkout, and in a couple of minutes our algorithms will take care of all the technical aspects smoothly and effortlessly, just for you!

Bonus: All our 1-Click applications are now installed with an active HTTPS self-signed certificate!

Way more practical, smooth, and headacheless, isn’t it?

Conclusion

For the next steps to go live, it is important to secure your web server connections by serving requests with HTTPS instead of HTTP. You can accomplish this using a CA certificate, which is the more convenient solution when going public; you can check our article explaining the difference between self-signed and CA certificates. You should also register a domain name with a registrar, so your customers can find you easily by using your site URL (such as www.example.com) instead of the server’s IP address (as seen in our testing example).

]]>
https://www.ssdnodes.com/blog/installing-lamp-on-ubuntu-22-04-lts-jammy-jellyfish/feed/ 0
Automated LAMP Setup Using Ansible https://www.ssdnodes.com/blog/ansible-lamp-setup/ https://www.ssdnodes.com/blog/ansible-lamp-setup/#respond Mon, 31 May 2021 19:58:02 +0000 https://blog.ssdnodes.com/blog/?p=5935 Continuing with our Ansible Series, we will move on to a pretty common use case for most VPSes, and that is of LAMP Stack setup. LAMP stands for Linux, Apache, MySQL and PHP.This is a very popular stack of technologies that web developers use to build apps.

Linux refers to the base operating system which in our case will be Ubuntu 20.04 LTS, Apache is the web server that will serve the web content, MySQL (or MariaDB) is the database that will be used to store the state of the app (things like user content, usernames, encrypted passwords, etc) and PHP is the programming language that is used to generate dynamic content. This tech stack is the base for many popular apps like WordPress, Drupal and more.

So let us create an Ansible playbook that will reliably setup LAMP stack on Ubuntu 20.04 LTS. As a developer, this would allow you to experiment and break things, and simply reinstall your VPS and get LAMP up and running with a single ansible command!

Prerequisites

  • A VPS running Ubuntu 20.04 LTS with a public IP. If you don't have one, feel free to pick one up here.
  • Initial Server Setup using Ansible to get you up to speed with ansible, and to harden your server's security.

Installing Packages

Let's start by create a lamp-setup.yaml Ansible Playbook like we did during our initial setup blog, except this time we will use the package builtin module to install the necessary packages:

---
- name: LAMP Setup
  hosts: all
  remote_user: root

  tasks:

  - name: Installing Packages
    package:
      name: "{{ item }}"
      state: present
    with_items:
      - apache2
      - mysql-server
      - php 
      - libapache2-mod-php 
      - php-mysql
      - python3-pymysql

This will install all the basic packages that are needed for a typical LAMP setup. Linux is obviously the base system, mysql-server, apache2, php are pretty self-explanatory as well. We will also need libapache2-mod-php to enable apache2 to talk to the PHP language interpreter. We would also need php-mysql which is a library that allows php to interface with the mysql database. Finally, we will also install python3-pymysql which is not a part of LAMP stack but it is required by Ansible to make MySQL requests on the target system.

Ansible Modules for Setting Up MySQL

Next we need to setup MySQL. This would involve setting up MySQL's root user's password (which is quit different from Linux's root user). Please be very careful and read the following:

  • DO NOT USE the same password as below, rather generate a random, unique and strong password.
  • DO NOT LEAVE the password in your ansible playbook. We will learn about dealing with passwords later in the future.
  • DO NOT PUSH the file .mycnfin your target's root directory to a git repository. This will contain your MySQL credentials and it should be ignored by git and other tools.

Now, that we have that out of the way, we can start using Ansible to configure MySQL. To do this, we will first need to download and install a collection of Ansible modules on our Ansible host machine

$ ansible-galaxy collection install community.mysql

Now we can use the module mysql_user to set root user's password. We will also create a .my.cnf file in the Linux root user's Home directory. This file contains username and password that can be used by Linux's root user to log into MySQL. This will also be used by Ansible in subsequent tasks to automatically authenticate as root user for MySQL without us having to repeatedly supply username and password for each task.

  - name: Set root password
    no_log: true
    community.mysql.mysql_user:
      name: root
      password: PLACE_YOUR_PASSWORD_HERE
      login_unix_socket: /var/run/mysqld/mysqld.sock

  - name: Copy .my.cnf for easier mysql automation
    blockinfile:
      path: ~/.my.cnf
      create: yes
      block: |
        [client]
        user=root
        password="PLACE_YOUR_PASSWORD_HERE"

Notice, we also add no_log: true for each of the tasks above, this is to prevent Ansible from accidentally mentioning the MySQL root password in any of its log files.

Secure MySQL Installation

Most installation of MySQL comes with a default script called mysql_secure_installation which you are meant to run as root. This script prompts you through a list of choices such as setting root password, removing anonymous user and test databases, as well as prohibiting the root user to connect to the the database remotely.

We will use Ansible to carry out these tasks without explicitly calling the script. Since we have already set root password, we only need to prohibit remote root login, remove anonymous user (represented by an empty string '') and we need to delete the test database. The following tasks help us with this:

  - name: Removes test database
    no_log: true
    community.mysql.mysql_db:
      name: test
      state: absent

  - name: Prohibit Remote Root login
    no_log: true
    community.mysql.mysql_query:
      login_db: mysql
      query: "{{ item }}"
    with_items:
      - DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
      - FLUSH PRIVILEGES;

We used different modules above, namely mysql_db and mysql_query. For more information on this you can always refer to their excellent documentation.

Configuring Apache

Next we need to configure Apache to serve php files. By default, Apache is configured to look into the directory /var/www/html directory and serve the index.html file first. If that is not found, it will look for index.htm, followed by a few other extensions.

We will configure it to look for index.php first. After this, you can deploy your PHP application to /var/www/html and Apache will automatically serve it.

  - name: Configure apache2
    lineinfile:
      path: /etc/apache2/mods-enabled/dir.conf
      regexp: "^\tDirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm$"
      line: "\tDirectoryIndex index.php index.html index.cgi index.pl index.php index.xhtml index.htm"
    notify: Restart Apache2

  handlers:
      - name: Restart Apache2
        service:
          name: apache2
          state: restarted

The configuration was in file /etc/apache2/mods-enabled/dir.conf and after the task results in a changed state of the file Ansible restarts the Apache web server as specified by notify: Restart Apache2.

The Restart Apache2 handler is defined inside the handlers section.

Final Playbook and Testing PHP rendering

To combine all the above setups, the lamp-setup.yaml playbook would look something like below:

---
- name: LAMP Setup
  hosts: all
  remote_user: root

  tasks:

  - name: Installing Packages
    package:
      name: "{{ item }}"
      state: present
    with_items:
      - apache2
      - mysql-server
      - php 
      - libapache2-mod-php 
      - php-mysql
      - python3-pymysql

  - name: Set root password
    no_log: true
    community.mysql.mysql_user:
      name: root
      password: PLACE_YOUR_PASSWORD_HERE
      login_unix_socket: /var/run/mysqld/mysqld.sock

  - name: Copy .my.cnf for easier mysql automation
    blockinfile:
      path: ~/.my.cnf
      create: yes
      block: |
        [client]
        user=root
        password="PLACE_YOUR_PASSWORD_HERE"

  - name: Removes test database
    no_log: true
    community.mysql.mysql_db:
      name: test
      state: absent

  - name: Prohibit Remote Root login
    no_log: true
    community.mysql.mysql_query:
      login_db: mysql
      query: "{{ item }}"
    with_items:
      - DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
      - FLUSH PRIVILEGES;
  - name: Configure apache2
    lineinfile:
      path: /etc/apache2/mods-enabled/dir.conf
      regexp: "^\tDirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm$"
      line: "\tDirectoryIndex index.php index.html index.cgi index.pl index.php index.xhtml index.htm"
    notify: Restart Apache2

  handlers:
      - name: Restart Apache2
        service:
          name: apache2
          state: restarted

To run the playbook use the below commands:

$ ansible-galaxy collection install community.mysql
$ ansible-playbook lamp-setup.yaml

Conclusion

With the playbook handy, you can really accelerate your development workflow. If something breaks in your testing server, you can reinstall the VPS back to a clean state, setup LAMP stack with a single command. If you wish to deploy your application directly from your git repository we have a blog post showing you how to do that as well!

]]>
https://www.ssdnodes.com/blog/ansible-lamp-setup/feed/ 0
How To Install WordPress On Debian 9 With LAMP (Tutorial) https://www.ssdnodes.com/blog/how-to-install-wordpress-on-debian-9-with-lamp-tutorial/ https://www.ssdnodes.com/blog/how-to-install-wordpress-on-debian-9-with-lamp-tutorial/#respond Wed, 11 Sep 2019 00:01:48 +0000 https://blog.ssdnodes.com/blog/?p=4312 https://www.ssdnodes.com/blog/how-to-install-wordpress-on-debian-9-with-lamp-tutorial/feed/ 0 How To Install WordPress On CentOS 7 With LAMP (Tutorial) https://www.ssdnodes.com/blog/how-to-install-wordpress-on-centos-7-with-lamp-tutorial/ https://www.ssdnodes.com/blog/how-to-install-wordpress-on-centos-7-with-lamp-tutorial/#respond Tue, 10 Sep 2019 13:51:30 +0000 https://blog.ssdnodes.com/blog/?p=4253 https://www.ssdnodes.com/blog/how-to-install-wordpress-on-centos-7-with-lamp-tutorial/feed/ 0 How To Install WordPress On Ubuntu 18.04 With LAMP (Tutorial) https://www.ssdnodes.com/blog/how-to-install-wordpress-on-ubuntu-18-04-with-lamp-tutorial/ https://www.ssdnodes.com/blog/how-to-install-wordpress-on-ubuntu-18-04-with-lamp-tutorial/#respond Thu, 08 Aug 2019 15:10:05 +0000 https://blog.ssdnodes.com/blog/?p=4090 https://www.ssdnodes.com/blog/how-to-install-wordpress-on-ubuntu-18-04-with-lamp-tutorial/feed/ 0 Install A LAMP Stack On Your VPS (Tutorial) https://www.ssdnodes.com/blog/install-a-lamp-stack-on-your-vps-tutorial/ https://www.ssdnodes.com/blog/install-a-lamp-stack-on-your-vps-tutorial/#respond Fri, 19 Jul 2019 15:39:09 +0000 https://blog.ssdnodes.com/blog/?p=4061 https://www.ssdnodes.com/blog/install-a-lamp-stack-on-your-vps-tutorial/feed/ 0