How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu _ DigitalOcean
How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu _ DigitalOcean
CONTENTS
Prerequisites
Setup a LAMP Stack on Ubuntu
Step 1 — Installing Apache and Updating the Firewall
Step 2 — Installing MySQL
Step 3 — Installing PHP
Step 4 — Creating a Virtual Host for your Website
Step 5 — Testing PHP Processing on your Web Server
Step 6 — Testing Database Connection from PHP (Optional)
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 1/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
Conclusion
// TUTORIAL //
on Ubuntu
Introduction
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 2/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
In this guide, you’ll set up a LAMP stack on an Ubuntu 22.04 server. These steps
remain consistent for Ubuntu v18.04 and above.
Prerequisites
If you are using Ubuntu version 16.04 or below, we recommend you upgrade to a
more latest version since Ubuntu no longer provides support for these versions. This
collection of guides will help you in upgrading your Ubuntu version.
In order to complete this tutorial, you will need to have a server running Ubuntu,
along with a non-root user with sudo privileges and an active firewall. For guidance
on how to set these up, please choose your distribution from this list and follow our
Initial Server Setup Guide.
Firewall
The Apache web server is among the most popular web servers in the world. It’s
well documented, has an active community of users, and has been in wide use for
much of the history of the web, which makes it a great choice for hosting a website.
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 3/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
Start by updating the package manager cache. If this is the first time you’re using
sudo within this session, you’ll be prompted to provide your user’s password to
confirm you have the right privileges to manage system packages with apt :
Copy
Copy
Once the installation is finished, you’ll need to adjust your firewall settings to allow
HTTP traffic. Ubuntu’s default firewall configuration tool is called Uncomplicated
Firewall (UFW). It has different application profiles that you can leverage. To list all
currently available UFW application profiles, execute this command:
Copy
Output
Available applications:
Apache
Apache Full
Apache Secure
OpenSSH
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 4/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
For now, it’s best to allow only connections on port 80 , since this is a fresh Apache
installation and you don’t yet have a TLS/SSL certificate configured to allow for
HTTPS traffic on your server.
To only allow traffic on port 80 , use the Apache profile:
Copy
Copy
Output
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Apache ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache (v6) ALLOW Anywhere (v6)
http:// your_server_ip
The default Ubuntu Apache web page is there for informational and testing
purposes. Below is an example of the Apache default web page for Ubuntu 22.04:
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 5/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
If you can view this page, your web server is correctly installed and accessible
through your firewall.
If you do not know what your server’s public IP address is, there are a number
of ways to find it. Usually, this is the address you use to connect to your server
through SSH.
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 6/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
There are a few different ways to do this from the command line. First, you
could use the iproute2 tools to get your IP address by typing this:
Copy
$ ip addr show ens3 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
This will give you two or three lines back. They are all correct addresses, but
your computer may only be able to use one of them, so feel free to try each
one.
An alternative method is to use the curl utility to contact an outside party to tell
you how it sees your server. This is done by asking a specific server what your
IP address is:
Copy
$ curl http://icanhazip.com
Whichever method you choose, type in your IP address into your web browser
to verify that your server is running.
Now that you have a web server up and running, you need to install the database
system to be able to store and manage data for your site. MySQL is a popular
database management system used within PHP environments.
Again, use apt to acquire and install this software:
Copy
When the installation is finished, it’s recommended that you run a security script that
comes pre-installed with MySQL. This script will remove some insecure default
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 7/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
Warning: As of July 2022, an error will occur when you run the
mysql_secure_installation script without some further configuration. The reason is
that this script will attempt to set a password for the installation’s root MySQL
account but, by default on Ubuntu installations, this account is not configured to
connect using a password.
Prior to July 2022, this script would silently fail after attempting to set the root
account password and continue on with the rest of the prompts. However, as of this
writing the script will return the following error after you enter and confirm a
password:
Output
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost'
New password:
This will lead the script into a recursive loop which you can only get out of by closing
your terminal window.
Because the mysql_secure_installation script performs a number of other actions
that are useful for keeping your MySQL installation secure, it’s still recommended
that you run it before you begin using MySQL to manage your data. To avoid
entering this recursive loop, though, you’ll need to first adjust how your root MySQL
user authenticates.
First, open up the MySQL prompt:
Copy
$ sudo mysql
Then run the following ALTER USER command to change the root user’s
authentication method to one that uses a password. The following example changes
the authentication method to mysql_native_password :
Copy
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 8/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
Copy
mysql> exit
Following that, you can run the mysql_secure_installation script without issue.
Copy
$ sudo mysql_secure_installation
This will ask if you want to configure the VALIDATE PASSWORD PLUGIN .
If you answer “yes”, you’ll be asked to select a level of password validation. Keep in
mind that if you enter 2 for the strongest level, you will receive errors when
attempting to set any password which does not contain numbers, upper and
lowercase letters, and special characters:
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 9/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
Regardless of whether you chose to set up the VALIDATE PASSWORD PLUGIN , your
server will next ask you to select and confirm a password for the MySQL root user.
This is not to be confused with the system root. The database root user is an
administrative user with full privileges over the database system. Even though the
default authentication method for the MySQL root user doesn’t involve using a
password, even when one is set, you should define a strong password here as an
additional safety measure.
If you enabled password validation, you’ll be shown the password strength for the
root password you just entered and your server will ask if you want to continue with
that password. If you are happy with your current password, enter Y for “yes” at the
prompt:
For the rest of the questions, press Y and hit the ENTER key at each prompt. This will
remove some anonymous users and the test database, disable remote root logins,
and load these new rules so that MySQL immediately respects the changes you
have made.
When you’re finished, test whether you’re able to log in to the MySQL console by
typing:
Copy
$ sudo mysql
This will connect to the MySQL server as the administrative database user root,
which is inferred by the use of sudo when running this command. Below is an
example output:
Output
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.28-0ubuntu4 (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Copy
mysql> exit
Notice that you didn’t need to provide a password to connect as the root user, even
though you have defined one when running the mysql_secure_installation script.
That is because the default authentication method for the administrative MySQL
user is unix_socket instead of password . Even though this might seem like a security
concern, it makes the database server more secure because the only users allowed
to log in as the root MySQL user are the system users with sudo privileges
connecting from the console or through an application running with the same
privileges. In practical terms, that means you won’t be able to use the administrative
database root user to connect from your PHP application. Setting a password for the
root MySQL account works as a safeguard, in case the default authentication
method is changed from unix_socket to password .
For increased security, it’s best to have dedicated user accounts with less expansive
privileges set up for every database, especially if you plan on having multiple
databases hosted on your server.
Note: There are some older versions of PHP that doesn’t support
caching_sha2_password , the default authentication method for MySQL 8. For that
reason, when creating database users for PHP applications on MySQL 8, you may
need to configure your application to use the mysql_native_password plug-in instead.
This tutorial will demonstrate how to do that in Step 6.
Your MySQL server is now installed and secured. Next, you’ll install PHP, the final
component in the LAMP stack.
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 11/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
You have Apache installed to serve your content and MySQL installed to store and
manage your data. PHP is the component of our setup that will process code to
display dynamic content to the final user. In addition to the php package, you’ll need
php-mysql , a PHP module that allows PHP to communicate with MySQL-based
databases. You’ll also need libapache2-mod-php to enable Apache to handle PHP
files. Core PHP packages will automatically be installed as dependencies.
To install these packages, run the following command:
Copy
Once the installation is finished, run the following command to confirm your PHP
version:
Copy
$ php -v
Output
PHP 8.1.2 (cli) (built: Mar 4 2022 18:13:46) (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
In some cases, you’ll want to modify the way that Apache serves files when a
directory is requested. Currently, if a user requests a directory from the server,
Apache will first look for a file called index.html . We want to tell the web server
to prefer PHP files over others, to make Apache look for an index.php file first. If
you don’t do that, an index.html file placed in the document root of the
application will always take precedence over an index.php file.
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 12/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
To make this change, open the dir.conf configuration file in a text editor of your
choice. Here, we’ll use nano :
Copy
/etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c>
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml inde
</IfModule>
Move the PHP index file (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 inde
</IfModule>
When you are finished, save and close the file by pressing CTRL+X . Confirm the
save by typing Y and then hit ENTER to verify the file save location.
After this, restart the Apache web server in order for your changes to be
recognized. You can do that with the following command:
Copy
You can also check on the status of the apache2 service using systemctl :
Copy
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 13/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
Sample Output
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor prese
Drop-In: /lib/systemd/system/apache2.service.d
└─apache2-systemd.conf
Active: active (running) since Thu 2021-07-15 09:22:59 UTC; 1h 3min ago
Main PID: 3719 (apache2)
Tasks: 55 (limit: 2361)
CGroup: /system.slice/apache2.service
├─3719 /usr/sbin/apache2 -k start
├─3721 /usr/sbin/apache2 -k start
└─3722 /usr/sbin/apache2 -k start
To extend the functionality of PHP, you have the option to install some additional
modules. To see the available options for PHP modules and libraries, pipe the
results of apt search into less , a pager which lets you scroll through the output
of other commands:
Copy
Use the arrow keys to scroll up and down, and press Q to quit.
The results are all optional components that you can install. It will give you a
short description for each:
To learn more about what each module does, you could search the internet for
more information about them. Alternatively, look at the long description of the
package by typing:
Copy
There will be a lot of output, with one field called Description which will have a
longer explanation of the functionality that the module provides.
For example, to find out what the php-cli module does, you could type this:
Copy
Along with a large amount of other information, you’ll find something that looks
like this:
Output
…
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 15/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
If, after researching, you decide you would like to install a package, you can do
so by using the apt install command like you have been doing for the other
software.
If you decided that php-cli is something that you need, you could type:
Copy
If you want to install more than one module, you can do that by listing each one,
separated by a space, following the apt install command, like this:
Copy
At this point, your LAMP stack is installed and configured. Before you do
anything else, we recommend that you set up an Apache virtual host where you
can store your server’s configuration details.
At this point, your LAMP stack is fully operational, but before testing your setup
with a PHP script, it’s best to set up a proper Apache Virtual Host to hold your
website’s files and folders.
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 16/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
When using the Apache web server, you can create virtual hosts (similar to server
blocks in Nginx) to encapsulate configuration details and host more than one
domain from a single server. In this guide, we’ll set up a domain called your_domain,
but you should replace this with your own domain name.
Note: In case you are using DigitalOcean as DNS hosting provider, check out our
product documentation for detailed instructions on how to set up a new domain
name and point it to your server.
Apache on Ubuntu has one virtual host enabled by default that is configured to
serve documents from the /var/www/html directory. While this works well for a single
site, it can become unwieldy if you are hosting multiple sites. Instead of modifying
/var/www/html , we’ll create a directory structure within /var/www for the
your_domain site, leaving /var/www/html in place as the default directory to be
served if a client request doesn’t match any other sites.
Create the directory for your_domain as follows:
Copy
Next, assign ownership of the directory with the $USER environment variable, which
will reference your current system user:
Copy
Copy
This will create a new blank file. Add in the following bare-bones configuration with
your own domain name:
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 17/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
/etc/apache2/sites-available/your_domain.conf
<VirtualHost *:80>
ServerName your_domain
ServerAlias www. your_domain
ServerAdmin webmaster@localhost
DocumentRoot /var/www/ your_domain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save and close the file when you’re done. If you’re using nano , do that by pressing
CTRL+X , then Y and ENTER .
Copy
You might want to disable the default website that comes installed with Apache.
This is required if you’re not using a custom domain name, because in this case
Apache’s default configuration would override your virtual host. To disable Apache’s
default website, type:
Copy
To make sure your configuration file doesn’t contain syntax errors, run the following
command:
Copy
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 18/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
Copy
Your new website is now active, but the web root /var/www/ your_domain is still
empty. Create an index.html file in that location to test that the virtual host works as
expected:
Copy
/var/www/your_domain/index.html
<html>
<head>
<title> your_domain website</title>
</head>
<body>
<h1>Hello World!</h1>
Save and close the file, then go to your browser and access your server’s domain
name or IP address:
http:// server_domain_or_IP
Your web page should reflect the contents in the file you just edited:
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 19/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
You can leave this file in place as a temporary landing page for your application until
you set up an index.php file to replace it. Once you do that, remember to remove or
rename the index.html file from your document root, as it would take precedence
over an index.php file by default.
With the default DirectoryIndex settings on Apache, a file named index.html will
always take precedence over an index.php file. This is useful for setting up
maintenance pages in PHP applications, by creating a temporary index.html file
containing an informative message to visitors. Because this page will take
precedence over the index.php page, it will then become the landing page for
the application. Once maintenance is over, the index.html is renamed or
removed from the document root, bringing back the regular application page.
In case you want to change this behavior, you’ll need to edit the
/etc/apache2/mods-enabled/dir.conf file and modify the order in which the
index.php file is listed within the DirectoryIndex directive:
Copy
/etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml
</IfModule>
After saving and closing the file, you’ll need to reload Apache so the changes
take effect:
Copy
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 20/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
In the next step, we’ll create a PHP script to test that PHP is correctly installed
and configured on your server.
Server
Now that you have a custom location to host your website’s files and folders, create
a PHP test script to confirm that Apache is able to handle and process requests for
PHP files.
Create a new file named info.php inside your custom web root folder:
Copy
This will open a blank file. Add the following text, which is valid PHP code, inside the
file:
/var/www/your_domain/info.php Copy
<?php
phpinfo();
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 21/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
This page provides information about your server from the perspective of PHP. It is
useful for debugging and to ensure that your settings are being applied correctly.
If you see this page in your browser, then your PHP installation is working as
expected.
After checking the relevant information about your PHP server through that page, it’s
best to remove the file you created as it contains sensitive information about your
PHP environment and your Ubuntu server. Use rm to do so:
Copy
You can always recreate this page if you need to access the information again later.
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 22/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
(Optional)
If you want to test whether PHP is able to connect to MySQL and execute database
queries, you can create a test table with test data and query for its contents from a
PHP script. Before you do that, you need to create a test database and a new
MySQL user properly configured to access it.
Create a database named example_database and a user named example_user. You
can replace these names with different values.
First, connect to the MySQL console using the root account:
Copy
$ sudo mysql
To create a new database, run the following command from your MySQL console:
Copy
Now create a new user and grant them full privileges on the custom database
you’ve just created.
Products
The following command creates a new user named example_user that
Solutions
authenticates with the caching_sha2_password method. We’re defining this user’s
password as password , but you should replace this value with a secure password
Developers
of your own choosing.
Partners
Copy
Pricing
mysql> CREATE USER ' example_user '@'%' IDENTIFIED BY ' password ';
Log in Sign up
Note: The previous ALTER USER statement sets the root MySQL user to authenticate
with the caching_sha2_password plugin. Per the official MySQL documentation,
caching_sha2_password is MySQL’s preferred authentication plugin, as it provides
Blog
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 23/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
more secure password encryption than the older, but still widely used,
mysql_native_password . Docs
However, some versions of PHP don’t work reliably with caching_sha2_password . PHP
has reported that this issue was fixed as of PHPGet
7.4,Support
but if you encounter an error
when trying to log in to phpMyAdmin later on, you may want to set root to
authenticate with mysql_native_password instead:
Contact Sales
Copy
Tutorials Questions Product Docs Search Community
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '
Copy
This will give the example_user user full privileges over the example_database
database, while preventing this user from creating or modifying other databases on
your server.
Now exit the MySQL shell with:
Copy
mysql> exit
Test if the new user has the proper permissions by logging in to the MySQL console
again, this time using the custom user credentials:
Copy
$ mysql -u example_user -p
Notice the -p flag in this command, which will prompt you for the password used
when creating the example_user user. After logging in to the MySQL console,
confirm that you have access to the example_database database:
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 24/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
Copy
Output
+--------------------+
| Database |
+--------------------+
| example_database |
| information_schema |
+--------------------+
2 rows in set (0.000 sec)
Next, create a test table named todo_list. From the MySQL console, run the
following statement:
Copy
Insert a few rows of content in the test table. Repeat the next command a few times,
using different values, to populate your test table:
Copy
To confirm that the data was successfully saved to your table, run:
Copy
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 25/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
Output
+---------+--------------------------+
| item_id | content |
+---------+--------------------------+
| 1 | My first important item |
| 2 | My second important item |
| 3 | My third important item |
| 4 | and this one more thing |
+---------+--------------------------+
4 rows in set (0.000 sec)
After confirming that you have valid data in your test table, exit the MySQL console:
Copy
mysql> exit
Now you can create the PHP script that will connect to MySQL and query for your
content. Create a new PHP file in your custom web root directory using your
preferred editor:
Copy
The following PHP script connects to the MySQL database and queries for the
content of the todo_list table, exhibiting the results in a list. If there’s a problem with
the database connection, it will throw an exception.
Add this content into your todo_list.php script, remembering to replace the
example_user and password with your own:
/var/www/your_domain/todo_list.php Copy
<?php
$user = " example_user ";
$password = " password ";
$database = " example_database ";
$table = " todo_list ";
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 26/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
try {
$db = new PDO("mysql:host=localhost;dbname=$database", $user, $password);
echo "<h2>TODO</h2><ol>";
foreach($db->query("SELECT content FROM $table") as $row) {
echo "<li>" . $row['content'] . "</li>";
}
echo "</ol>";
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
This web page should reveal the content you’ve inserted in your test table to your
visitor:
That means your PHP environment is ready to connect and interact with your
MySQL server.
Conclusion
In this guide, you’ve built a flexible foundation for serving PHP websites and
applications to your visitors, using Apache as a web server and MySQL as a
database system.
As an immediate next step, you should ensure that connections to your web server
are secured, by serving them via HTTPS. In order to accomplish that, you can use
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 27/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
Let’s Encrypt on Ubuntu 22.04 / 20.04 / 18.04 to secure your site with a free
TLS/SSL certificate.
Thanks for learning with the DigitalOcean Community. Check out our offerings
for compute, storage, networking, and managed databases.
Learn more about our products ->
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 28/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
Comments
4 Comments
Leave a comment...
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 29/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 30/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
Reply
Click below to sign up and get $200 of credit to try our products over 60 days!
Sign up
Popular Topics
AI/ML
Ubuntu
Linux Basics
JavaScript
Python
MySQL
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 31/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
Docker
Kubernetes
Sign up
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 32/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
Learn more
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 33/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
Become a contributor
Get paid to write technical tutorials and select a tech-focused charity to receive
a matching donation.
Learn more
Featured Tutorials
DigitalOcean Products
Learn more
Company
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 34/35
31/10/2024, 16:34 How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu | DigitalOcean
Products
Resources
Solutions
Contact
https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu 35/35