banner



How To Install Lamp In Linux

Introduction

A "LAMP" stack is a grouping of open-source software that is typically installed together to enable a server to host dynamic websites and web apps. This term is actually an acronym which represents the Linux operating organization, with the Apache web server. The site data is stored in a GySQL database, and dynamic content is processed by PHP.

In this guide, nosotros'll install a LAMP stack on an Ubuntu xviii.04 server.

Prerequisites

In order to complete this tutorial, you'll need to have an Ubuntu 18.04 server with a non-root sudo-enabled user account and a bones firewall configured. To gear up this up, you can follow our initial server setup guide for Ubuntu 18.04.

Stride 1 — Installing Apache and Updating the Firewall

The Apache web server is a popular open source web server that can be used along with PHP to host dynamic websites. It'southward well-documented and has been in wide apply for much of the history of the web.

Starting time, make certain your apt cache is updated with:

                      
  1. sudo apt update

If this is your showtime time using sudo in this session, you'll be prompted to provide your regular user's password to validate your permissions.

Once the cache has been updated, you can install Apache with:

                      
  1. sudo apt install apache2

After inbound this control, apt volition tell you which packages information technology plans to install and how much extra deejay space they'll accept upward. Press Y and hit ENTER to confirm, and the installation will proceed.

Adjust the Firewall to Allow Web Traffic

Next, assuming that yous take followed the initial server setup instructions and enabled the UFW firewall, make sure that your firewall allows HTTP and HTTPS traffic. You can check that UFW has an application contour for Apache similar so:

                      
  1. sudo ufw app list
                      

Output

Available applications: Apache Apache Full Apache Secure OpenSSH

If you await at the Apache Full profile details, you'll see that information technology enables traffic to ports eighty and 443:

                      
  1. sudo ufw app info "Apache Total"
                      

Output

Profile: Apache Total Title: Web Server (HTTP,HTTPS) Description: Apache v2 is the next generation of the omnipresent Apache web server. Ports: eighty,443/tcp

To let incoming HTTP and HTTPS traffic for this server, run:

                      
  1. sudo ufw allow "Apache Full"

You can do a spot check correct abroad to verify that everything went as planned by visiting your server's public IP accost in your web browser (see the note under the next heading to find out what your public IP address is if you do not have this information already):

          http://your_server_ip                  

Yous volition see the default Ubuntu 18.04 Apache web page, which is at that place for advisory and testing purposes. It should look something like this:

Ubuntu 18.04 Apache default

If you run into this page, then your web server is now correctly installed and attainable through your firewall.

How To Find your Server's Public IP Address

If you do not know what your server's public IP accost is, at that place are a number of ways you tin observe it. Usually, this is the address you employ to connect to your server through SSH.

There are a few different ways to exercise this from the command line. First, yous could use the iproute2 tools to get your IP address by typing this:

                      
  1. ip addr show eth0 | grep inet | awk '{ impress $two; }' | sed 's/\/.*$//'

This volition give you lot two or three lines back. They are all correct addresses, but your estimator may only exist able to utilize one of them, so feel costless to try each 1.

An alternative method is to use the coil utility to contact an outside party to tell yous how it sees your server. This is done by asking a specific server what your IP address is:

                      
  1. sudo apt install curl
  2. curl http://icanhazip.com

Regardless of the method you use to get your IP accost, type information technology into your spider web browser'southward address bar to view the default Apache page.

Step ii — Installing MySQL

At present that you have your web server upwards and running, it is time to install MySQL. MySQL is a database management system. Basically, it volition organize and provide access to databases where your site tin shop information.

Again, use apt to acquire and install this software:

                      
  1. sudo apt install mysql-server

Note: In this example, y'all do not have to run sudo apt update prior to the command. This is considering y'all recently ran it in the commands above to install Apache. The package index on your figurer should already be up-to-date.

This command, besides, will evidence you a list of the packages that will be installed, along with the amount of disk space they'll accept upward. Enter Y to continue.

When the installation is complete, run a simple security script that comes pre-installed with MySQL which will remove some dangerous defaults and lock downward access to your database system. First the interactive script past running:

                      
  1. sudo mysql_secure_installation

This will ask if you lot want to configure the VALIDATE Countersign PLUGIN.

Note: Enabling this feature is something of a judgment phone call. If enabled, passwords which don't match the specified criteria will be rejected by MySQL with an error. This will cause issues if you use a weak password in conjunction with software which automatically configures MySQL user credentials, such as the Ubuntu packages for phpMyAdmin. It is rubber to leave validation disabled, but you should always use potent, unique passwords for database credentials.

Answer Y for yeah, or annihilation else to continue without enabling.

          VALIDATE Password PLUGIN can exist used to exam passwords and ameliorate security. It checks the strength of password and allows the users to gear up only those passwords which are secure enough. Would y'all like to setup VALIDATE PASSWORD plugin?  Printing y|Y for Yes, whatsoever other key for No:                  

If you lot answer "yes", you'll be asked to select a level of password validation. Keep in mind that if you enter ii for the strongest level, you lot will receive errors when attempting to fix any password which does non contain numbers, upper and lowercase letters, and special characters, or which is based on mutual dictionary words.

          At that place are three levels of password validation policy:  LOW    Length >= viii MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= viii, numeric, mixed case, special characters and dictionary                  file  Please enter 0 = LOW, 1 = MEDIUM and 2 = Stiff:            1                  

Regardless of whether you chose to set up the VALIDATE PASSWORD PLUGIN, your server volition next ask you to select and confirm a password for the MySQL root user. This is not to be dislocated with the organisation root. The database root user is an administrative user with total privileges over the database arrangement. Even though the default hallmark method for the MySQL root user dispenses the utilise of a password, even when one is set up, you should define a strong countersign here equally an additional safety mensurate. We'll talk about this in a moment.

If yous've enabled password validation, yous'll exist shown the password force for the root password you simply entered and your server will ask if y'all want to change that countersign. If you are happy with your current password, enter N for "no" at the prompt:

          Using existing password for root.  Estimated strength of the password:            100            Alter the countersign for root ? ((Press y|Y for Aye, whatsoever other central for No) :            n                  

For the rest of the questions, press Y and striking the ENTER fundamental at each prompt. This volition remove some bearding users and the test database, disable remote root logins, and load these new rules and so that MySQL immediately respects the changes you have made.

When y'all're finished, test if you're able to log in to the MySQL console by typing:

                      
  1. sudo mysql

This will connect to the MySQL server as the authoritative database user root, which is inferred by the use of sudo when running this command. Y'all should see output like this:

                      

Output

Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is v Server version: 5.7.34-0ubuntu0.18.04.1 (Ubuntu) Copyright (c) 2000, 2022, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may exist trademarks of their respective owners. Type 'assist;' or '\h' for help. Type '\c' to articulate the current input argument. mysql>

To exit the MySQL console, blazon:

                      
  1. leave

Notice that you didn't demand to provide a password to connect every bit the root user, even though yous have divers 1 when running the mysql_secure_installation script. That is because the default hallmark method for the administrative MySQL user is unix_socket instead of password. Even though this might look similar a security business at first, it makes the database server more secure because the but users allowed to log in as the root MySQL user are the organization users with sudo privileges connecting from the panel or through an application running with the same privileges. In practical terms, that ways you won't be able to use the authoritative database root user to connect from your PHP awarding. Setting a password for the root MySQL business relationship works as a safeguard, in instance the default authentication method is changed from unix_socket to password.

For increased security, information technology's best to accept dedicated user accounts with less expansive privileges prepare up for every database, especially if you plan on having multiple databases hosted on your server. Delight refer to our guide on How To Create a New User and Grant Permissions on MySQL for detailed instructions on how to create MySQL users and configure database access rights.

Your MySQL server is now installed and secured. Next, we'll install PHP, the last component in the LAMP stack.

Step 3 — Installing PHP

PHP is the component of your setup that will process lawmaking to display dynamic content. Information technology tin run scripts, connect to your MySQL databases to get information, and hand the processed content over to your web server so that it tin display the results to your visitors.

One time again, leverage the apt system to install PHP. In addition to the php package, you'll also need libapache2-mod-php to integrate PHP into Apache, and the php-mysql package to permit PHP to connect to MySQL databases. Run the following command to install all three packages and their dependencies:

                      
  1. sudo apt install php libapache2-modern-php php-mysql

This should install PHP without whatsoever problems. We'll test this in a moment.

Changing Apache's Directory Alphabetize (Optional)

In some cases, you'll want to modify the fashion that Apache serves files when a directory is requested. Currently, if a user requests a directory from the server, Apache volition first look for a file called index.html. We want to tell the web server to prefer PHP files over others, to brand 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.

To brand this modify, open up the dir.conf configuration file in a text editor of your pick. Here, nosotros'll employ nano:

                      
  1. sudo nano /etc/apache2/mods-enabled/dir.conf

It will look like this:

/etc/apache2/mods-enabled/dir.conf

          <IfModule mod_dir.c>     DirectoryIndex index.html alphabetize.cgi index.pl            index.php            alphabetize.xhtml index.htm </IfModule>                  

Movement the PHP index file (highlighted above) to the start position after the DirectoryIndex specification, like this:

/etc/apache2/mods-enabled/dir.conf

          <IfModule mod_dir.c>     DirectoryIndex            index.php            alphabetize.html index.cgi alphabetize.pl alphabetize.xhtml index.htm </IfModule>                  

When you are finished, save and close the file by pressing CTRL+X. Ostend the save past typing Y and and then striking ENTER to verify the file save location.

After this, restart the Apache web server in order for your changes to exist recognized. You can do that with the following command:

                      
  1. sudo systemctl restart apache2

Y'all can besides cheque on the status of the apache2 service using systemctl:

                      
  1. sudo systemctl status apache2
                      

Sample Output

● apache2.service - The Apache HTTP Server Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled) Drop-In: /lib/systemd/organisation/apache2.service.d └─apache2-systemd.conf Active: active (running) since Thu 2022-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 outset ├─3721 /usr/sbin/apache2 -g get-go └─3722 /usr/sbin/apache2 -one thousand start Jul xv 09:22:59 ubuntu1804 systemd[ane]: Starting The Apache HTTP Server... Jul 15 09:22:59 ubuntu1804 apachectl[3694]: AH00558: apache2: Could not reliably make up one's mind the server'due south fully qualified domain name, using 127.0.ane.ane. Set up the 'ServerName' di Jul xv 09:22:59 ubuntu1804 systemd[1]: Started The Apache HTTP Server.

Printing Q to leave this status output.

Installing PHP Extensions (Optional)

To extend the functionality of PHP, you have the option to install some additional modules. To meet the available options for PHP modules and libraries, pipe the results of apt search into less, a pager which lets y'all scroll through the output of other commands:

                      
  1. apt search php- | less

Use the arrow keys to scroll up and downward, and press Q to quit.

The results are all optional components that you can install. It volition give you a short description for each:

          bandwidthd-pgsql/bionic ii.0.1+cvs20090917-10ubuntu1 amd64   Tracks usage of TCP/IP and builds html files with graphs  bluefish/bionic 2.2.10-1 amd64   advanced Gtk+ text editor for web and software development  cacti/bionic 1.ane.38+ds1-1 all   web interface for graphing of monitoring systems  ganglia-webfrontend/bionic iii.half-dozen.one-iii all   cluster monitoring toolkit - web front-end  golang-github-unknwon-cae-dev/bionic 0.0~git20160715.0.c6aac99-four all   PHP-similar Compression and Archive Extensions in Become  haserl/bionic 0.9.35-2 amd64   CGI scripting plan for embedded environments  kdevelop-php-docs/bionic 5.2.i-1ubuntu2 all   transitional bundle for kdevelop-php  kdevelop-php-docs-l10n/bionic 5.2.1-1ubuntu2 all   transitional packet for kdevelop-php-l10n … :                  

To larn more about what each module does, you could search the internet for more information most them. Alternatively, look at the long description of the package by typing:

                      
  1. apt evidence package_name

There volition exist a lot of output, with 1 field chosen Description which will have a longer explanation of the functionality that the module provides.

For instance, to detect out what the php-cli module does, you could type this:

                      
  1. apt show php-cli

Along with a large amount of other data, you lot'll find something that looks like this:

                      

Output

… Description: command-line interpreter for the PHP scripting linguistic communication (default) This bundle provides the /usr/bin/php command interpreter, useful for testing PHP scripts from a shell or performing full general shell scripting tasks. . PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and tin exist embedded into HTML. . This package is a dependency package, which depends on Ubuntu'due south default PHP version (currently 7.2). …

If, afterwards researching, you decide yous would like to install a package, y'all tin can do and so by using the apt install control like you have been doing for the other software.

If you lot decided that php-cli is something that you lot need, y'all could blazon:

                      
  1. sudo apt install php-cli

If you desire to install more than ane module, you lot tin can do that by list each one, separated by a infinite, post-obit the apt install command, like this:

                      
  1. sudo apt install package1 package2 ...

At this point, your LAMP stack is installed and configured. Before you practise anything else, we recommend that you prepare up an Apache virtual host where you tin can store your server's configuration details.

When using the Apache web server, you lot tin utilise virtual hosts (similar to server blocks in Nginx) to encapsulate configuration details and host more than than 1 domain from a single server. We volition fix a domain called your_domain, merely you lot should replace this with your own domain name. To larn more than about setting up a domain name with DigitalOcean, come across our Introduction to DigitalOcean DNS.

Apache on Ubuntu xviii.04 has one server block enabled past default that is configured to serve documents from the /var/world wide web/html directory. While this works well for a unmarried site, it can go unwieldy if you lot are hosting multiple sites. Instead of modifying /var/www/html, allow'south create a directory structure within /var/www for your_domain site, leaving /var/www/html in place as the default directory to exist served if a client request doesn't friction match whatever other sites.

Create the directory for your_domain as follows:

                      
  1. sudo mkdir /var/www/your_domain

Side by side, assign ownership of the directory with the $USER environment variable, which references the electric current logged user:

                      
  1. sudo chown -R $USER : $USER /var/www/your_domain

The permissions of your web root directory should be correct if you lot haven't modified its umask value, but you tin make certain by typing:

                      
  1. sudo chmod -R 755 /var/world wide web/your_domain

Next, create a sample alphabetize.html page using nano or your favorite editor:

                      
  1. nano /var/www/your_domain/alphabetize.html

Inside, add together the following sample HTML:

/var/world wide web/your_domain/index.html

          <html>     <caput>         <title>Welcome to            Your_domain!</title>     </head>     <body>         <h1>Success!  The            your_domain            server cake is working!</h1>     </body> </html>                  

Save and close the file when you are finished.

In gild for Apache to serve this content, it's necessary to create a virtual host file with the right directives. Instead of modifying the default configuration file located at /etc/apache2/sites-available/000-default.conf straight, let'southward make a new one at /etc/apache2/sites-available/your_domain.conf:

                      
  1. sudo nano /etc/apache2/sites-available/your_domain.conf

Paste in the following configuration cake, which is similar to the default, but updated for our new directory and domain proper name:

/etc/apache2/sites-available/your_domain.conf

          <VirtualHost *:80>     ServerAdmin webmaster@localhost     ServerName            your_domain            ServerAlias            www.your_domain            DocumentRoot /var/www/your_domain            ErrorLog ${APACHE_LOG_DIR}/error.log     CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>                  

Observe that we've updated the DocumentRoot to our new directory and ServerAdmin to an email that the your_domain site ambassador tin access. We've also added two directives: ServerName, which establishes the base domain that should friction match for this virtual host definition, and ServerAlias, which defines further names that should match as if they were the base name.

Salvage and shut the file when you lot are finished.

Let's enable the file with the a2ensite tool:

                      
  1. sudo a2ensite your_domain.conf

Disable the default site defined in 000-default.conf:

                      
  1. sudo a2dissite 000-default.conf

Side by side, let's exam for configuration errors:

                      
  1. sudo apache2ctl configtest

Y'all should see the following output:

                      

Output

Syntax OK

Restart Apache to implement your changes:

                      
  1. sudo systemctl restart apache2

Apache should at present exist serving your domain name. You can test this by navigating to http://your_domain , where yous should see something like this:

Apache virtual host example

With that, your virtual host is fully set up. Before making any more changes or deploying an application, though, it would be helpful to proactively test out your PHP configuration in case at that place are any issues that should be addressed.

Stride 5 — Testing PHP Processing on your Web Server

In gild to test that your system is properly configured for PHP, create a PHP script chosen info.php. In order for Apache to observe this file and serve it correctly, information technology must be saved to your web root directory.

Create the file at the web root you created in the previous step by running:

                      
  1. sudo nano /var/www/your_domain/info.php

This will open up a blank file. Add together the following text, which is valid PHP code, inside the file:

/var/world wide web/your_domain/info.php

          <?php phpinfo();                  

When you are finished, salvage and close the file.

Now you tin test whether your spider web server is able to correctly display content generated by this PHP script. To try this out, visit this page in your web browser. You lot'll need your server's public IP address or domain name again.

The address you volition want to visit is:

          http://your_domain/info.php                  

The page that you come to should await something similar this:

Ubuntu 18.04 default PHP info

This page provides some basic information about your server from the perspective of PHP. It is useful for debugging and to ensure that your settings are existence applied correctly.

If you tin see this page in your browser, then your PHP is working equally expected.

You probably desire to remove this file after this test because information technology could actually give information about your server to unauthorized users. To do this, run the post-obit command:

                      
  1. sudo rm /var/www/your_domain/info.php

You tin ever recreate this page if you lot need to access the information again afterwards.

Conclusion

Now that you take a LAMP stack installed, y'all have many choices for what to do next. You've installed a platform that volition allow you lot to install about kinds of websites and web software on your server.

As an immediate next step, y'all should ensure that connections to your web server are secured, by serving them via HTTPS. Follow our guide on how to secure Apache with Let's Encrypt to secure your site with a gratis TLS/SSL certificate.

Another popular options are:

  • Install Wordpress the virtually pop content management system on the net.
  • Gear up PHPMyAdmin to assistance manage your MySQL databases from a web browser.
  • Learn how to employ SFTP to transfer files to and from your server.

Source: https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-ubuntu-18-04

Posted by: foxandef1993.blogspot.com

0 Response to "How To Install Lamp In Linux"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel