{"id":1187,"date":"2026-11-27T13:00:00","date_gmt":"2026-11-27T13:00:00","guid":{"rendered":"https:\/\/vpslake.com\/blog\/?p=1187"},"modified":"2026-11-27T13:00:00","modified_gmt":"2026-11-27T13:00:00","slug":"install-lamp-stack-ubuntu-22-04","status":"publish","type":"post","link":"https:\/\/vpslake.com\/blog\/2026\/11\/27\/install-lamp-stack-ubuntu-22-04\/","title":{"rendered":"How to Install LAMP Stack on Ubuntu 22.04"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Install LAMP stack on Ubuntu 22.04 with Apache, MariaDB, PHP 8.1, PHP-FPM, and a UFW firewall in about ten minutes. You will finish with a web server, a local application database account, and a temporary health check that proves PHP is executing.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n\n<li>A fresh 64-bit Ubuntu 22.04 (Jammy Jellyfish) VPS with at least 2 GB of RAM and 10 GB of free disk space.<\/li>\n\n\n<li>SSH access as <code>root<\/code>, or a normal user that can run commands with <code>sudo<\/code>.<\/li>\n\n\n<li>A public IP address and outbound access to Ubuntu\u2019s package repositories.<\/li>\n\n\n<li>A domain is optional for this base setup, but you need one before configuring trusted HTTPS for a production website.<\/li>\n\n\n<li>A <a href=\"https:\/\/vpslake.com\/buy-vps\">VPSLake Linux VPS<\/a> if you need a root-access Ubuntu server.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The commands use <code>sudo<\/code> for a non-root SSH account. If you are already logged in as <code>root<\/code>, remove <code>sudo<\/code> from each command.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Confirm Ubuntu 22.04 and apply updates<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Checking the release first keeps Jammy-specific PHP and service commands from being used on another Ubuntu version.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep -E '^(PRETTY_NAME|VERSION_ID|VERSION_CODENAME)=' \/etc\/os-release\nuname -m\nsudo apt update\nsudo apt full-upgrade -y<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The release should show Ubuntu 22.04 with the codename <code>jammy<\/code>; a typical VPS reports <code>x86_64<\/code> for its architecture. The exact package patch levels will change as Ubuntu publishes updates.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PRETTY_NAME=\"Ubuntu 22.04.x LTS\"\nVERSION_ID=\"22.04\"\nVERSION_CODENAME=jammy\nx86_64<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the upgrade installs a new kernel, keep this SSH session open until you know the VPS can reboot and accept a new connection.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Install Apache and protect web traffic<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Apache serves the site, while UFW limits public access to SSH and the two standard web ports.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install -y apache2 apache2-utils curl ufw\nsudo systemctl enable --now apache2\nsudo ufw allow OpenSSH\nsudo ufw allow 80\/tcp\nsudo ufw allow 443\/tcp\nsudo ufw --force enable\nsudo systemctl is-active apache2\nsudo ufw status verbose<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the SSH rule before enabling UFW. If SSH listens on a custom port, allow that port instead of relying on the <code>OpenSSH<\/code> application profile. The service check should print <code>active<\/code>, and the firewall should list SSH, port 80, and port 443.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Test Apache locally before troubleshooting DNS or a provider-level firewall.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apachectl configtest\ncurl -fsSI http:\/\/127.0.0.1\/<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Syntax OK\nHTTP\/1.1 200 OK<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Install and secure MariaDB<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">MariaDB provides the SQL database layer; keeping its application account on <code>localhost<\/code> avoids exposing database traffic to the Internet.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install -y mariadb-server mariadb-client\nsudo systemctl enable --now mariadb\nsudo systemctl is-active mariadb\nsudo mariadb-secure-installation<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The security script is interactive. On a new Ubuntu installation, press Enter if it asks for an existing root password, keep Unix-socket authentication if that option is offered, remove anonymous users, disallow remote root login, remove the test database, and reload the privilege tables. MariaDB explains these prompts in its <a href=\"https:\/\/mariadb.com\/docs\/server\/clients-and-utilities\/deployment-tools\/mariadb-secure-installation\" target=\"_blank\" rel=\"noopener\">official <code>mariadb-secure-installation<\/code> documentation<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create a database and a separate login for an application. Replace <code>YOUR_DB_PASSWORD<\/code> with a long, unique password before running this block; do not use the placeholder as the real secret.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo mariadb &lt;&lt;'SQL'\nCREATE DATABASE lampdb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;\nCREATE USER 'lampuser'@'localhost' IDENTIFIED BY 'YOUR_DB_PASSWORD';\nGRANT ALL PRIVILEGES ON lampdb.* TO 'lampuser'@'localhost';\nFLUSH PRIVILEGES;\nSQL\nsudo mariadb -e \"SHOW DATABASES LIKE 'lampdb';\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The final query should return one row named <code>lampdb<\/code>. Keep the real password in your application\u2019s secret configuration, not in a file under <code>\/var\/www\/html<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Install PHP 8.1 with PHP-FPM<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Ubuntu 22.04\u2019s native PHP packages use the 8.1 series, and PHP-FPM keeps PHP workers separate from Apache instead of using the older embedded <code>mod_php<\/code> model. PHP\u2019s <a href=\"https:\/\/www.php.net\/manual\/en\/install.unix.apache2.php\" target=\"_blank\" rel=\"noopener\">Apache installation guidance<\/a> recommends PHP-FPM with Apache\u2019s <code>mod_proxy_fcgi<\/code> for modern deployments.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install -y php8.1-fpm php8.1-cli php8.1-mysql php8.1-curl php8.1-gd php8.1-mbstring php8.1-xml php8.1-zip php8.1-opcache\nphp -v\nsudo a2enmod proxy_fcgi setenvif\nsudo a2enconf php8.1-fpm\nsudo systemctl enable --now php8.1-fpm\nsudo systemctl reload apache2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The version command should begin with <code>PHP 8.1<\/code>. The Apache module and configuration commands make <code>.php<\/code> requests go to the FPM socket instead of returning PHP source code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create a short-lived endpoint that returns a fixed string. This checks PHP execution without publishing the detailed server information exposed by <code>phpinfo()<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>printf '%s\\n' '&lt;?php echo \"PHP OK\\n\"; ?&gt;' | sudo tee \/var\/www\/html\/health.php &gt; \/dev\/null\ncurl -fsS http:\/\/127.0.0.1\/health.php\nsudo rm -f \/var\/www\/html\/health.php<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>PHP OK<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Verify the LAMP stack<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Run these checks from SSH to confirm that the three services start at boot, PHP has its MariaDB connector, Apache accepts its configuration, and the web server answers locally.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl is-active apache2 php8.1-fpm mariadb\nsudo systemctl is-enabled apache2 php8.1-fpm mariadb\nsudo apachectl configtest\nphp -m | grep -E 'mysqli|mysqlnd|PDO|mbstring|xml'\nsudo mariadb -e \"SELECT VERSION();\"\ncurl -fsSI http:\/\/127.0.0.1\/\nsudo ufw status<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Expect three <code>active<\/code> lines, three <code>enabled<\/code> lines, <code>Syntax OK<\/code>, the PHP module names, a MariaDB version, an HTTP success response, and an active UFW policy.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>active\nactive\nactive\nenabled\nenabled\nenabled\nSyntax OK\nHTTP\/1.1 200 OK\nStatus: active<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">From your computer, browse to <code>http:\/\/SERVER_IP\/<\/code> and replace <code>SERVER_IP<\/code> with the VPS public address. You should see Apache\u2019s default page; a domain and HTTPS certificate can be added when you deploy the application.<\/p>\n\n\n\n<aside class=\"vl-element vl-cta-banner\" aria-label=\"Start your LAMP server\">\n\t<div class=\"vl-cta-banner__content\">\n\t\t<p class=\"vl-cta-banner__eyebrow\">Ubuntu VPS<\/p>\n\t\t<h3 class=\"vl-cta-banner__title\">Start your LAMP server<\/h3>\n\t\t<p class=\"vl-cta-banner__text\">Get a root-access Ubuntu VPS for Apache, PHP, and MariaDB.<\/p>\n\t<\/div>\n\t<a class=\"vl-cta-banner__button\" href=\"\/buy-vps\" aria-label=\"View Linux VPS plans\">View Linux VPS plans<\/a>\n<\/aside>\n\n\n\n<h2 class=\"wp-block-heading\">Troubleshooting<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><code>Unit php8.1-fpm.service not found<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The FPM package is missing, or the machine is not actually running Ubuntu 22.04 repositories. Confirm the release and install the Jammy package rather than adding a third-party PHP repository.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep -E '^(PRETTY_NAME|VERSION_ID|VERSION_CODENAME)=' \/etc\/os-release\nsudo apt update\nsudo apt install -y php8.1-fpm\nsudo systemctl enable --now php8.1-fpm<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the codename is not <code>jammy<\/code>, stop and use instructions for the installed Ubuntu release instead of mixing package sources.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">PHP source appears in the browser or Apache returns <code>503 Service Unavailable<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Apache is not connected to the FPM socket, or the FPM service failed to start. Re-enable the packaged configuration, inspect the service log, and then reload Apache.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl status php8.1-fpm --no-pager\nsudo journalctl -u php8.1-fpm -n 40 --no-pager\nsudo a2enmod proxy_fcgi setenvif\nsudo a2enconf php8.1-fpm\nsudo apachectl configtest\nsudo systemctl restart php8.1-fpm apache2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Recreate the temporary <code>health.php<\/code> file from Step 4, test it with <code>curl<\/code>, and remove it again. Do not leave a diagnostic file in the public document root.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">SSH stops working after enabling UFW<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The firewall was enabled before the correct SSH port was allowed, or SSH is listening on a nonstandard port. Use <strong>VPS control panel (Virtualizor) \u2192 VNC console<\/strong> from the VPSLake client area to regain console access, then check the listener and add the actual port.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ss -ltnp | grep ssh\nsudo ufw status numbered\nsudo ufw allow YOUR_SSH_PORT\/tcp<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace <code>YOUR_SSH_PORT<\/code> with the numeric port shown by <code>ss<\/code>. Keep the current session open and test a second SSH connection before removing any old firewall rule.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>Access denied for user 'lampuser'@'localhost'<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The application password does not match the value used when the account was created, or the application is connecting with a different database host. Test the credentials locally, then reset the account only if needed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mariadb -u lampuser -p lampdb -e \"SELECT 1;\"\nsudo mariadb -e \"ALTER USER 'lampuser'@'localhost' IDENTIFIED BY 'YOUR_NEW_DB_PASSWORD';\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Use the real password at the prompt, not the placeholder. If you reset it, update the application configuration at the same time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Hardening<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n\n<li>Keep port 3306 closed in UFW. An application on this VPS can use <code>localhost<\/code> without a public MariaDB listener.<\/li>\n\n\n<li>Point your domain to the server, issue a trusted TLS certificate, and redirect HTTP to HTTPS before accepting logins or other sensitive data.<\/li>\n\n\n<li>Remove temporary health checks, keep database credentials out of the document root, and apply Ubuntu security updates regularly.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">FAQ<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Does Ubuntu 22.04 include PHP 8.1?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Ubuntu 22.04\u2019s Jammy repositories provide the PHP 8.1 package series used in this guide. Installing the versioned packages keeps the Apache FPM configuration and systemd service name aligned with that release.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why use PHP-FPM instead of <code>libapache2-mod-php<\/code>?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">PHP-FPM runs PHP as a separate FastCGI service and works with Apache\u2019s event-based worker model. It also lets you restart PHP independently from Apache, which is a better starting point for a new deployment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Should I open MariaDB port 3306?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Not for a normal single-server website. The <code>lampuser<\/code> account is restricted to <code>localhost<\/code>, so the application can connect locally while UFW leaves the database unreachable from the public Internet.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Do I need a domain to install LAMP?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. You can verify Apache with the VPS public IP while building the stack. You need a domain later for a clean production URL and a trusted HTTPS certificate.<\/p>\n\n\n\n<script type=\"application\/ld+json\">{\"@context\": \"https:\/\/schema.org\", \"@type\": \"FAQPage\", \"mainEntity\": [{\"@type\": \"Question\", \"name\": \"Does Ubuntu 22.04 include PHP 8.1?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"Yes. Ubuntu 22.04\u2019s Jammy repositories provide the PHP 8.1 package series used in this guide. Installing the versioned packages keeps the Apache FPM configuration and systemd service name aligned with that release.\"}}, {\"@type\": \"Question\", \"name\": \"Why use PHP-FPM instead of `libapache2-mod-php`?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"PHP-FPM runs PHP as a separate FastCGI service and works with Apache\u2019s event-based worker model. It also lets you restart PHP independently from Apache, which is a better starting point for a new deployment.\"}}, {\"@type\": \"Question\", \"name\": \"Should I open MariaDB port 3306?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"Not for a normal single-server website. The `lampuser` account is restricted to `localhost`, so the application can connect locally while UFW leaves the database unreachable from the public Internet.\"}}, {\"@type\": \"Question\", \"name\": \"Do I need a domain to install LAMP?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"No. You can verify Apache with the VPS public IP while building the stack. You need a domain later for a clean production URL and a trusted HTTPS certificate.\"}}]}<\/script>\n\n","protected":false},"excerpt":{"rendered":"Install LAMP stack on Ubuntu 22.04 with Apache, MariaDB, PHP-FPM, UFW, and a safe health check for a secure web server you can deploy today.","protected":false},"author":1,"featured_media":1188,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"csco_singular_sidebar":"","csco_page_header_type":"","csco_page_load_nextpost":"","footnotes":""},"categories":[5,9],"tags":[71,117,73,72,101],"class_list":["post-1187","post","type-post","status-publish","format-standard","has-post-thumbnail","category-guides","category-vps-hosting","tag-apache","tag-lamp-stack","tag-mariadb","tag-php","tag-ubuntu-22-04","cs-entry"],"_links":{"self":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/1187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/comments?post=1187"}],"version-history":[{"count":1,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/1187\/revisions"}],"predecessor-version":[{"id":4238,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/1187\/revisions\/4238"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/media\/1188"}],"wp:attachment":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/media?parent=1187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/categories?post=1187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/tags?post=1187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}