{"id":444,"date":"2024-04-11T12:13:41","date_gmt":"2024-04-11T12:13:41","guid":{"rendered":"https:\/\/www.webhostinghyderabad.co.in\/tutorials\/?p=444"},"modified":"2024-04-11T12:13:47","modified_gmt":"2024-04-11T12:13:47","slug":"how-to-install-lamp-stack-on-ubuntu","status":"publish","type":"post","link":"https:\/\/www.webhostinghyderabad.co.in\/tutorials\/how-to-install-lamp-stack-on-ubuntu\/","title":{"rendered":"How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu<\/h2>\n\n\n\n<p>Installing a LAMP stack on Ubuntu is a fundamental process for setting up a <a href=\"https:\/\/www.squarebrothers.com\/vps-hosting-india\/\" target=\"_blank\" rel=\"noreferrer noopener\">web server<\/a> environment. This combination of Linux, Apache, MySQL, and PHP provides a robust foundation for hosting dynamic websites and web applications. Here\u2019s a step-by-step guide on how to install each component:<\/p>\n\n\n\n<p>A \u201cLAMP\u201d stack is a group of open-source software that is typically installed together in order to enable a server to host dynamic websites and web apps written in PHP. This term is an acronym which represents the Linux operating system with the Apache web server. The site data is stored in a MySQL database, and dynamic content is processed by PHP.<\/p>\n\n\n\n<p>In this guide, you\u2019ll set up a LAMP stack on an Ubuntu 22.04 server. These steps remain consistent for Ubuntu v18.04 and above.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1 \u2013 Installing Apache and Updating the Firewall<\/h2>\n\n\n\n<p>The LAMP Apache web server is among the most popular web servers in the world. It\u2019s 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.<\/p>\n\n\n\n<p>Start by updating the package manager cache. If this is the first time, you\u2019re using sudo within this session, you\u2019ll be prompted to provide your user\u2019s password to confirm you have the right privileges to manage system packages with apt:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>sudo apt update<\/strong><\/code><\/pre>\n\n\n\n<p>Then, install Apache with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>sudo apt install apache2<\/strong><\/code><\/pre>\n\n\n\n<p>You\u2019ll be prompted to confirm Apache\u2019s installation.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>sudo ufw app list<\/strong><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>Output<\/strong>\n<strong>Available applications:\n  Apache\n  Apache Full\n  Apache Secure\n  OpenSSH<\/strong><\/code><\/pre>\n\n\n\n<p>Here\u2019s what each of these profiles mean:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Apache: This profile opens only port 80 (normal, unencrypted web traffic).<\/li>\n\n\n\n<li>Apache Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS\/SSL encrypted traffic).<\/li>\n\n\n\n<li>Apache Secure: This profile opens only port 443 (TLS\/SSL encrypted traffic).<\/li>\n<\/ul>\n\n\n\n<p>Ubuntu\u2019s default firewall is provided by UFW (Uncomplicated Firewall). If UFW is inactive, you can enable it and allow traffic on Apache\u2019s default port (port 80).<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"900\" height=\"500\" src=\"https:\/\/www.webhostinghyderabad.co.in\/tutorials\/wp-content\/uploads\/2024\/04\/LAMP.png\" alt=\"LAMP\" class=\"wp-image-445\" srcset=\"https:\/\/www.webhostinghyderabad.co.in\/tutorials\/wp-content\/uploads\/2024\/04\/LAMP.png 900w, https:\/\/www.webhostinghyderabad.co.in\/tutorials\/wp-content\/uploads\/2024\/04\/LAMP-300x167.png 300w, https:\/\/www.webhostinghyderabad.co.in\/tutorials\/wp-content\/uploads\/2024\/04\/LAMP-768x427.png 768w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Enable UFW:<\/h4>\n\n\n\n<p>If UFW is not already enabled, you can do so with the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>sudo ufw enable<\/strong><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Allow Apache Through the Firewall:<\/h4>\n\n\n\n<p>To allow incoming traffic on port 80 (HTTP), use the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>sudo ufw allow 'Apache'<\/strong><\/code><\/pre>\n\n\n\n<p>This command allows HTTP traffic to reach your Apache web server.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Verify the Status of UFW:<\/h4>\n\n\n\n<p>You can verify the status of UFW to ensure that the rules have been correctly applied:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>sudo ufw status<\/strong><\/code><\/pre>\n\n\n\n<p>You should see a message indicating that Apache traffic is allowed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2 \u2013 Installing MySQL<\/h2>\n\n\n\n<p>Now that you have Ubuntu 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.<\/p>\n\n\n\n<p>Again, use apt to acquire and install this software:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>sudo apt install mysql-server<\/strong><\/code><\/pre>\n\n\n\n<p>When prompted, confirm installation by typing&nbsp;<code>Y<\/code>, and then&nbsp;<code>ENTER<\/code>.<\/p>\n\n\n\n<p>When the installation is finished, it\u2019s recommended that you run a security script that comes pre-installed with MySQL. This script will remove some insecure default settings and lock down access to your database system.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/webhostingpune.co.in\/blog\/wp-content\/uploads\/2024\/04\/R.jpg\" alt=\"\" class=\"wp-image-425\"\/><\/figure>\n\n\n\n<p>Start the interactive script by running:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>sudo mysql_secure_installation<\/strong><\/code><\/pre>\n\n\n\n<p>Answer&nbsp;<code>Y<\/code>&nbsp;for yes, or anything else to continue without enabling.<\/p>\n\n\n\n<p>When you\u2019re finished, test whether you\u2019re able to log in to the MySQL console by typing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>sudo mysql<\/strong><\/code><\/pre>\n\n\n\n<p>This will connect to the MySQL server as the administrative database user&nbsp;<strong>root<\/strong>, which is inferred by the use of&nbsp;<code>sudo<\/code>&nbsp;when running this command. Below is an example output:<\/p>\n\n\n\n<p>To exit the MySQL console, type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>mysql> exit<\/strong><\/code><\/pre>\n\n\n\n<p>Your MySQL server is now installed and secured. Next, you\u2019ll install PHP, the final component in the LAMP stack.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3 \u2013 Installing PHP<\/h2>\n\n\n\n<p>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&nbsp;<code>php<\/code>&nbsp;package, you\u2019ll need&nbsp;<code>php-mysql<\/code>, a PHP module that allows PHP to communicate with MySQL-based databases. You\u2019ll also need&nbsp;<code>libapache2-mod-php<\/code>&nbsp;to enable Apache to handle PHP files. Core PHP packages will automatically be installed as dependencies.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/webhostingpune.co.in\/blog\/wp-content\/uploads\/2024\/04\/maxresdefault-1-1024x576.jpg\" alt=\"\" class=\"wp-image-428\"\/><\/figure>\n\n\n\n<p>To install these packages, run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>sudo apt install php libapache2-mod-php php-mysql<\/strong><\/code><\/pre>\n\n\n\n<p>Once the installation is finished, run the following command to confirm your PHP version:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>php -v<\/strong><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4 \u2014 Creating a Virtual Host for your Website<\/h2>\n\n\n\n<p>When using the Apache web server, you can create&nbsp;virtual hosts&nbsp;(similar to server blocks in Nginx) to encapsulate configuration details and host more than one domain from a single server. In this guide, we\u2019ll set up a domain called&nbsp;<strong>your_domain<\/strong>, but you should&nbsp;<strong>replace this with your own domain name<\/strong>.<\/p>\n\n\n\n<p>Create the directory for&nbsp;<strong>your_domain<\/strong>&nbsp;as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>sudo mkdir \/var\/www\/your_domain<\/strong><\/code><\/pre>\n\n\n\n<p>Next, assign ownership of the directory with the&nbsp;<code>$USER<\/code>&nbsp;environment variable, which will reference your current system user:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>sudo chown -R $USER:$USER \/var\/www\/your_domain<\/strong><\/code><\/pre>\n\n\n\n<p>Then, open a new configuration file in Apache\u2019s&nbsp;<code>sites-available<\/code>&nbsp;directory using your preferred command-line editor. Here, we\u2019ll use&nbsp;<code>nano<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>sudo nano \/etc\/apache2\/sites-available\/your_domain.conf<\/strong><\/code><\/pre>\n\n\n\n<p>This will create a new blank file. Add in the following bare-bones configuration with your own domain name:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&lt;VirtualHost *:80><br>    ServerName your_domain<br>    ServerAlias www.your_domain<br>    ServerAdmin webmaster@localhost<br>    DocumentRoot \/var\/www\/your_domain<br>    ErrorLog ${APACHE_LOG_DIR}\/error.log<br>    CustomLog ${APACHE_LOG_DIR}\/access.log combined<br>&lt;\/VirtualHost><\/strong><\/pre>\n\n\n\n<p>Save and close the file when you\u2019re done. If you\u2019re using&nbsp;<code>nano<\/code>, do that by pressing&nbsp;<code>CTRL+X<\/code>, then&nbsp;<code>Y<\/code>&nbsp;and&nbsp;<code>ENTER<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/webhostingpune.co.in\/blog\/wp-content\/uploads\/2024\/04\/vAztlqe4m9Aq522hsSj7LNqEJtfZD9LMa7Y5v5zY.png\" alt=\"\" class=\"wp-image-430\"\/><\/figure>\n\n\n\n<p>With this\u00a0<code>VirtualHost<\/code>\u00a0configuration, we\u2019re telling Apache to serve\u00a0your_domain\u00a0using\u00a0<code>\/var\/www\/<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\">your_domain<\/mark><\/code>\u00a0as the web root directory. If you\u2019d like to test Apache without a domain name, you can remove or comment out the options\u00a0<code>ServerName<\/code>\u00a0and\u00a0<code>ServerAlias<\/code>\u00a0by adding a pound sign (<code>#<\/code>) the beginning of each option\u2019s lines.<\/p>\n\n\n\n<p>Now, use&nbsp;<code>a2ensite<\/code>&nbsp;to enable the new virtual host:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo a2ensite your_domain<\/code><\/pre>\n\n\n\n<p>You might want to disable the default website that comes installed with Apache. This is required if you\u2019re not using a custom domain name, because in this case Apache\u2019s default configuration would override your virtual host. To disable Apache\u2019s default website, type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo a2dissite 000-default<\/code><\/pre>\n\n\n\n<p>To make sure your configuration file doesn\u2019t contain syntax errors, run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apache2ctl configtest<\/code><\/pre>\n\n\n\n<p>Finally, reload Apache so these changes take effect:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl reload apache2<\/code><\/pre>\n\n\n\n<p>Your new website is now active, but the web root&nbsp;<code>\/var\/www\/<mark>your_domain<\/mark><\/code>&nbsp;is still empty. Create an&nbsp;<code>index.html<\/code>&nbsp;file in that location to test that the virtual host works as expected:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nano \/var\/www\/your_domain\/index.html<\/code><\/pre>\n\n\n\n<p>Include the following content in this file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/var\/www\/your_domain\/index.html<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;html&gt;<br>  &lt;head&gt;<br>    &lt;title&gt;your_domain website&lt;\/title&gt;<br>  &lt;\/head&gt;<br>  &lt;body&gt;<br>    &lt;h1&gt;Hello World!&lt;\/h1&gt;<br><br>    &lt;p&gt;This is the landing page of &lt;strong&gt;your_domain&lt;\/strong&gt;.&lt;\/p&gt;<br>  &lt;\/body&gt;<br>&lt;\/html&gt;<\/pre>\n\n\n\n<p>Save and close the file, then go to your browser and access your server\u2019s domain name or IP address:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">http:\/\/server_domain_or_IP<\/pre>\n\n\n\n<p>Your web page should reflect the contents in the file you just edited:<\/p>\n\n\n\n<p>You can leave this file in place as a temporary landing page for your application until you set up an&nbsp;<code>index.php<\/code>&nbsp;file to replace it. Once you do that, remember to remove or rename the&nbsp;<code>index.html<\/code>&nbsp;file from your document root, as it would take precedence over an&nbsp;<code>index.php<\/code>&nbsp;file by default.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 5 \u2014 Testing PHP Processing on your Web Server<\/h2>\n\n\n\n<p>Now that you have a custom location to host your website\u2019s files and folders, create a PHP test script to confirm that Apache is able to handle and process requests for PHP files.<\/p>\n\n\n\n<p>Create a new file named&nbsp;<code>info.php<\/code>&nbsp;inside your custom web root folder:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nano \/var\/www\/your_domain\/info.php<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\/var\/www\/your_domain\/info.php<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php<br>phpinfo();<\/pre>\n\n\n\n<p>When you are finished, save and close the file.<\/p>\n\n\n\n<p>To test this script, go to your web browser and access your server\u2019s domain name or IP address, followed by the script name, which in this case is&nbsp;<code>info.php<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">http:\/\/server_domain_or_IP\/info.php<\/pre>\n\n\n\n<p>After checking the relevant information about your PHP server through that page, it\u2019s best to remove the file you created as it contains sensitive information about your PHP environment and your Ubuntu server. Use&nbsp;<code>rm<\/code>&nbsp;to do so:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo rm \/var\/www\/your_domain\/info.php<\/code><\/pre>\n\n\n\n<p>You can always recreate this page if you need to access the information again later.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In conclusion, you have successfully set up a LAMP (Linux, Apache, MySQL, PHP) stack on your <a href=\"https:\/\/www.webhostinghyderabad.co.in\/web-hosting\/\">Ubuntu server<\/a>. This stack provides a robust foundation for hosting dynamic websites and web applications. Here\u2019s a brief summary of what you accomplished:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Installed Apache<\/strong>: Apache is a widely used web server that serves web content to clients over the HTTP protocol.<\/li>\n\n\n\n<li><strong>Configured Firewall<\/strong>: The firewall (UFW) was updated to allow traffic on Apache\u2019s default port (port 80), ensuring that incoming web requests can reach your server.<\/li>\n\n\n\n<li><strong>Installed MySQL<\/strong>: MySQL is a popular relational database management system used for storing and managing data for your web applications.<\/li>\n\n\n\n<li><strong>Secured MySQL Installation<\/strong>: The MySQL secure installation script was run to set a root password, remove insecure default settings, and enhance the security of your MySQL server.<\/li>\n\n\n\n<li><strong>Installed PHP<\/strong>: PHP is a server-side scripting language used for developing dynamic web content. It was installed and integrated with Apache to process PHP files.<\/li>\n\n\n\n<li><strong>Tested PHP Processing<\/strong>: A PHP test file was created to verify that PHP is installed and configured correctly. Accessing this file in a web browser displayed PHP configuration details.<\/li>\n<\/ol>\n\n\n\n<p>With the LAMP stack set up, you\u2019re now equipped to develop and host dynamic websites and web applications on your Ubuntu server. You can further customize and optimize each component according to your specific requirements. If you have any questions or need further assistance, feel free to ask!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu Installing a LAMP stack on Ubuntu is a fundamental process for setting up a web server environment. This combination of Linux, Apache, MySQL, and PHP provides a robust foundation for hosting dynamic websites and web applications. Here\u2019s a step-by-step guide on how to install each component: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":446,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[],"class_list":["post-444","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-lamp"],"_links":{"self":[{"href":"https:\/\/www.webhostinghyderabad.co.in\/tutorials\/wp-json\/wp\/v2\/posts\/444","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webhostinghyderabad.co.in\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webhostinghyderabad.co.in\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webhostinghyderabad.co.in\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webhostinghyderabad.co.in\/tutorials\/wp-json\/wp\/v2\/comments?post=444"}],"version-history":[{"count":1,"href":"https:\/\/www.webhostinghyderabad.co.in\/tutorials\/wp-json\/wp\/v2\/posts\/444\/revisions"}],"predecessor-version":[{"id":447,"href":"https:\/\/www.webhostinghyderabad.co.in\/tutorials\/wp-json\/wp\/v2\/posts\/444\/revisions\/447"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webhostinghyderabad.co.in\/tutorials\/wp-json\/wp\/v2\/media\/446"}],"wp:attachment":[{"href":"https:\/\/www.webhostinghyderabad.co.in\/tutorials\/wp-json\/wp\/v2\/media?parent=444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webhostinghyderabad.co.in\/tutorials\/wp-json\/wp\/v2\/categories?post=444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webhostinghyderabad.co.in\/tutorials\/wp-json\/wp\/v2\/tags?post=444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}