{"id":13930,"date":"2022-06-14T21:42:19","date_gmt":"2022-06-14T19:42:19","guid":{"rendered":"https:\/\/contabo.com\/blog\/?p=13930"},"modified":"2022-06-14T21:42:35","modified_gmt":"2022-06-14T19:42:35","slug":"lamp-stack-cloud-init","status":"publish","type":"post","link":"https:\/\/contabo.com\/blog\/lamp-stack-cloud-init\/","title":{"rendered":"Get LAMP Stack Up and Running on Your Server in Less than 90 Seconds using Cloud-Init"},"content":{"rendered":"\n<p>Building software today consists of many different technologies including programming languages, frameworks, databases and other tools. All of this combined is what you call a \u201ccompany\u2019s tech stack\u201d. For example, a popular one is the MERN stack which stands for MongoDB, Express, React and Node. But today, let\u2019s take a look at LAMP stack as it\u2019s one of the most influential and popular ones.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/06\/blog-head_1200x630_lamp-cloudinit.jpg\" alt=\"\" class=\"wp-image-13963\" srcset=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/06\/blog-head_1200x630_lamp-cloudinit.jpg 1200w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/06\/blog-head_1200x630_lamp-cloudinit-600x315.jpg 600w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/06\/blog-head_1200x630_lamp-cloudinit-768x403.jpg 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/figure>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">What is LAMP Stack?<\/h2>\n\n\n\n<p>LAMP stands for Linux, Apache, MySQL and PHP and has been around since 1998 as a free and open-source alternative to commercial software to build websites. Since then, many companies and tools have adopted this tech stack and it\u2019s widely used until today.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Isn&#8217;t LAMP dead?<\/h2>\n\n\n\n<p>There are many other tech stacks gaining more and more popularity and web-development isn\u2019t as complex anymore as it was 20 years ago. Despite this, LAMP is still widely used and tools like <a href=\"https:\/\/wordpress.org\" rel=\"nofollow\">WordPress<\/a> powering 43% of the web are doing a great job at keeping LAMP alive.<\/p>\n\n\n\n<p>Here are just a few examples of big companies or tools using LAMP stack:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>WordPress<\/li><li>Wikipedia<\/li><li>Slack<\/li><li>Facebook<\/li><\/ol>\n\n\n\n<p>By the way, this blog also runs on WordPress and thus uses the LAMP tech stack.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Setting up LAMP Stack<\/h2>\n\n\n\n<p>So now you know what we\u2019re talking about, let\u2019s get to work and set up a website with the LAMP stack. The goal is to have an Apache web server running with PHP and MySQL and we can achieve this with a simple installation using the apt (Advanced Packaging Tool) on Linux. But besides this manual installation method, there are actually other ways to get the LAMP stack ready on our machine.<br>Although in Contabo it\u2019s possible to set up a new instance with LAMP stack preinstalled on a <a href=\"https:\/\/contabo.com\/en\/vps\">VPS<\/a> or <a href=\"https:\/\/contabo.com\/en\/vds\">VDS<\/a> when creating a new instance or using the reinstallation panel, sometimes it\u2019s necessary to tweak some settings and thus a manual installation may be the better way to go.<\/p>\n\n\n\n<p>To get a complete understanding of the cloud-init config file, we\u2019ll start with the manual installation first as the config is mostly based on the steps we\u2019re doing in the manual guide.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Manual Installation<\/h3>\n\n\n\n<p>To get started, all you need is a Linux server and root access to it so we can install and configure the required packages. We\u2019ll be using Debian 11 on a <a href=\"https:\/\/contabo.com\/en\/vps\/\">Contabo Cloud VPS<\/a> in this guide.<\/p>\n\n\n\n<p>Before installing new packages, make sure the system is up-to-date by updating and upgrading the packages:<\/p>\n\n\n\n<p><code>apt update &amp;&amp; apt upgrade -y<\/code><\/p>\n\n\n\n<p>Next we add a few packages required for the installation and also apache2:<\/p>\n\n\n\n<p><code>apt-get install software-properties-common wget curl apache2 -y<\/code><\/p>\n\n\n\n<p>In order to install the PHP packages, we first need to add the PHP repository:<\/p>\n\n\n\n<p><code>curl -sSL https:\/\/packages.sury.org\/php\/README.txt | sudo bash -x<\/code><\/p>\n\n\n\n<p>Now we can install the PHP packages:<\/p>\n\n\n\n<p><code>apt-get install php8.1 php8.1-cli php8.1-common php8.1-curl php8.1-gd php8.1-intl php8.1-mbstring php8.1-mysql php8.1-opcache php8.1-readline php8.1-xml php8.1-xsl php8.1-zip php8.1-bz2 libapache2-mod-php8.1 -y<\/code><\/p>\n\n\n\n<p>If you don\u2019t need the extensions above, you can also install PHP without all the additional packages:<\/p>\n\n\n\n<p><code>apt-get install php8.1 php8.1-cli libapache2-mod-php8.1 -y<\/code><\/p>\n\n\n\n<p>Let\u2019s restart the apache2 web server with service apache2 restart and if you visit your server\u2019s IP address, you should be able to see the apache2 default page:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"772\" height=\"277\" src=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/06\/apache2_default_page.png\" alt=\"apache2_default_page\" class=\"wp-image-13931\" srcset=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/06\/apache2_default_page.png 772w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/06\/apache2_default_page-600x215.png 600w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/06\/apache2_default_page-768x276.png 768w\" sizes=\"auto, (max-width: 772px) 100vw, 772px\" \/><\/figure>\n\n\n\n<p>To have a full LAMP stack installed, all there\u2019s left is MySQL. We can install it pretty easily with a simple command:<\/p>\n\n\n\n<p><code>apt install mariadb-server mariadb-client -y &amp;&amp; mysql_secure_installation<\/code><\/p>\n\n\n\n<p>This will install MariaDB (MySQL) and automatically start the installer. In the installation process you\u2019ll be asked to set a new root password. To access your database, you can do so with \u201croot\u201d as username and the password you just set.<\/p>\n\n\n\n<p>Congrats, you successfully installed the LAMP stack on your system. But we can be way more efficient, so let\u2019s head over to cloud-init and automate what we\u2019ve just done manually.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Installation using Cloud-Init<\/h3>\n\n\n\n<p> <a href=\"https:\/\/contabo.com\/blog\/what-is-cloud-init\/\">In a previous article<\/a>, we already explained the benefits of cloud-init and why it\u2019s awesome. Generally, cloud-init is a tool to set a cloud instance up using a predefined config file as blueprint. This not only automates the setup process but also helps when it comes to disaster recovery and clusters.<\/p>\n\n\n\n<p>With the manual installation guide in mind, we can now create the cloud-init config to automate the process and replicate it on many machines.<\/p>\n\n\n\n<p>To indicate that it\u2019s a cloud-init config, the first line looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>#cloud-config<\/code><\/code><\/pre>\n\n\n\n<p>Cloud-init provides <a href=\"https:\/\/cloudinit.readthedocs.io\/en\/latest\/topics\/modules.html#package-update-upgrade-install\" rel=\"nofollow\">two simple options<\/a> to control whether the packages should be updated and upgraded initially. To make sure we\u2019re working with the latest and greatest, we set them to true:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package_update: true\npackage_upgrade: true\n<\/code><\/pre>\n\n\n\n<p>The apache2 web server and a few other required packages can be installed right away and we can tell cloud-init to install them with the <a href=\"https:\/\/cloudinit.readthedocs.io\/en\/latest\/topics\/modules.html#package-update-upgrade-install\" rel=\"nofollow\">packages module<\/a>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>packages:\n  - software-properties-common\n  - wget\n  - curl\n  - apache2<\/code><\/pre>\n\n\n\n<p>The rest of the installation will be done in the runcmd module. It\u2019s a simple list of commands that will be executed in the <a href=\"https:\/\/cloudinit.readthedocs.io\/en\/latest\/topics\/boot.html\" rel=\"nofollow\">cloud-init \u201cconfig\u201d stage<\/a>. In order to install PHP, we can use the same commands as we did in the manual installation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>runcmd:\n  - curl -sSL https:\/\/packages.sury.org\/php\/README.txt | sudo bash -x\n  - sudo apt-get install php8.1 php8.1-cli php8.1-common php8.1-curl php8.1-gd php8.1-intl php8.1-mbstring php8.1-mysql php8.1-opcache php8.1-readline php8.1-xml php8.1-xsl php8.1-zip php8.1-bz2 libapache2-mod-php8.1 -y\n  - service apache2 restart<\/code><\/pre>\n\n\n\n<p>Setting up MySQL \/ MariaDB is a bit different. First we need to install it:<\/p>\n\n\n\n<p><code>apt install mariadb-server mariadb-client -y<\/code><\/p>\n\n\n\n<p>Because we can\u2019t use the mysql_secure_installation installer in this case, we have to set the password using the cloud-init config. We don\u2019t want a password to be stored in this file, so let\u2019s just create a random one and save it to a file on the server (\/home\/mysql_access.txt) so we can access it using SSH:<\/p>\n\n\n\n<p><code>pw=$(openssl rand -base64 18); echo \"mysql_password=$pw\" &gt;&gt; \/home\/mysql_access.txt<\/code><\/p>\n\n\n\n<p>To actually change the password, use the mysqladmin command:<\/p>\n\n\n\n<p><code>pw=$(openssl rand -base64 18); mysqladmin -u root -h localhost password \"$pw\"; echo \"mysql_password=$pw\" &gt;&gt; \/home\/mysql_access.txt<\/code><\/p>\n\n\n\n<p>Now let\u2019s reload the grant tables to make sure the changes are effective:<\/p>\n\n\n\n<p><code>mysqladmin reload<\/code><\/p>\n\n\n\n<p>If we now combine all these configurations, we get a complete cloud-init config that can be used to create new instances with the LAMP stack installed and configured:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#cloud-config\npackage_update: true\npackage_upgrade: true\npackages:\n  - software-properties-common\n  - wget\n  - curl\n  - apache2\nruncmd:\n  - curl -sSL https:\/\/packages.sury.org\/php\/README.txt | sudo bash -x\n  - sudo apt-get install php8.1 php8.1-cli php8.1-common php8.1-curl php8.1-gd php8.1-intl php8.1-mbstring php8.1-mysql php8.1-opcache php8.1-readline php8.1-xml php8.1-xsl php8.1-zip php8.1-bz2 libapache2-mod-php8.1 -y\n  - service apache2 restart\n  - apt install mariadb-server mariadb-client -y\n  - pw=$(openssl rand -base64 18); mysqladmin -u root -h localhost password \"$pw\"; echo \"mysql_password=$pw\" &gt;&gt; \/home\/mysql_access.txt\n  - mysqladmin reload\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">The Benefits of Using Cloud-Init<\/h2>\n\n\n\n<p>With this configuration file, you have created a single file that can be used to replicate this setup on multiple instances.<\/p>\n\n\n\n<p>Do you need another website built on top of LAMP? Simply spin up a new machine and you\u2019re ready to deploy your new project.<\/p>\n\n\n\n<p>Something bad happened and an instance went down? No problem, with this Cloud-Init config you can create a new instance quickly with all your configuration already applied to it.<\/p>\n\n\n\n<p>It\u2019s about replication and disaster recovery: You no longer need to install new instances manually, because with this config you\u2019ve created a blueprint that can be used when creating new instances &#8211; this means less work for you and a much more efficient workflow.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>LAMP Stack (Linux, Apache, MySQL &#038; PHP) is widely used for Web applications. In this article we will show you how to set up and run LAMP on your Server. This process only takes 90 seconds &#8211; thanks to Cloud-Init.<\/p>\n","protected":false},"author":57,"featured_media":13963,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[18],"tags":[1467,181,1468,510,894],"ppma_author":[1525],"class_list":["post-13930","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-cloud-init","tag-contabo","tag-lamp-stack","tag-linux","tag-vps"],"uagb_featured_image_src":{"full":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/06\/blog-head_1200x630_lamp-cloudinit.jpg",1200,630,false],"thumbnail":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/06\/blog-head_1200x630_lamp-cloudinit-150x150.jpg",150,150,true],"medium":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/06\/blog-head_1200x630_lamp-cloudinit-600x315.jpg",600,315,true],"medium_large":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/06\/blog-head_1200x630_lamp-cloudinit-768x403.jpg",768,403,true],"large":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/06\/blog-head_1200x630_lamp-cloudinit.jpg",1200,630,false],"1536x1536":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/06\/blog-head_1200x630_lamp-cloudinit.jpg",1200,630,false],"2048x2048":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/06\/blog-head_1200x630_lamp-cloudinit.jpg",1200,630,false]},"uagb_author_info":{"display_name":"Linus Benkner","author_link":"https:\/\/contabo.com\/blog\/author\/linus\/"},"uagb_comment_info":0,"uagb_excerpt":"LAMP Stack (Linux, Apache, MySQL & PHP) is widely used for Web applications. In this article we will show you how to set up and run LAMP on your Server. This process only takes 90 seconds - thanks to Cloud-Init.","authors":[{"term_id":1525,"user_id":57,"is_guest":0,"slug":"linus","display_name":"Linus Benkner","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/396885b0c71ca364e267b5804deaef19e48538c136b5287377b0d481091abd10?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/13930","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/users\/57"}],"replies":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/comments?post=13930"}],"version-history":[{"count":0,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/13930\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/media\/13963"}],"wp:attachment":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/media?parent=13930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/categories?post=13930"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/tags?post=13930"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=13930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}