LAMP stack is nothing but a combination of open source softwares to make a webserver. LAMP refers to the first letters of Linux, Apache, Mysql and PHP.
Apache
Install and Configure
Apache 2 is available as an ubuntu package, for install the package execute below command.
# sudo apt-get install apache2
This command will install the apache2 and its dependencies
After the installation,
Add the following line “ServerName localhost” to the /etc/apache2/apache2.conf file.
ServerName localhost
Restart apache service
# sudo /etc/init.d/apache2 restart
MySQL
Install the mysql-server package and choose a secure password when prompted:
# sudo apt-get install mysql-server
Create a MySQL Database
Log into MySQL:
# mysql -u root -p
Enter MySQL’s root password, and you’ll be presented with a MySQL prompt.
If no password was entered in the previous section, or if you want to change the root password, enter the following command. Replace password with a new root password:
# ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' BY 'password';
Create a database and a user with permissions for it. In this example, the database is called webdata, the user webuser, and password password:
CREATE DATABASE webdata;
GRANT ALL ON webdata.* TO 'webuser' IDENTIFIED BY 'password';
Exit MySQL:
quit
PHP
Install PHP, the PHP Extension and Application Repository, Apache support, and MySQL support:
# sudo apt-get install php7.0 php-pear libapache2-mod-php7.0 php7.0-mysql
Optionally, install additional cURL, JSON, and CGI support:
# sudo apt-get install php7.0-curl php7.0-json php7.0-cgi
Once PHP7.0 is installed, edit the configuration file located in /etc/php/7.0/apache2/php.ini to enable more descriptive errors, logging, and better performance. The following modifications provide a good starting point:
/etc/php/7.0/apache2/php.ini
max_input_time = 30
error_reporting = E_COMPILE_ERROR | E_RECOVERABLE_ERROR | E_ERROR | E_CORE_ERROR
error_log = /var/log/php/error.log
The beginning of the php.ini file contains examples commented out with a semicolon (;), which disables these directives. Ensure that the lines you modify in this step are after the examples section and are uncommented.
Create the log directory for PHP and give ownership to the Apache system user:
# sudo mkdir /var/log/php
# sudo chown www-data /var/log/php
Restart Apache:
# sudo systemctl restart apache2
Test
we’ll create a test page that shows whether Apache can render PHP and connect to the MySQL database. This can be helpful in locating the source of an error if one of the elements of your LAMP stack is not communicating with the others.
Paste the following code into a new file, phptest.php, in the public_html directory. Modify webuser and password to match the information entered in the Create a MySQL Database section above:
/var/www/html/example.com/public_html/phptest.php
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>';
// In the variables section below, replace user and password with your own MySQL credentials as created on your server
$servername = "localhost";
$username = "webuser";
$password = "password";
// Create MySQL connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection - if it fails, output will include the error message
if (!$conn) {
die('<p>Connection failed: <p>' . mysqli_connect_error());
}
echo '<p>Connected successfully</p>';
?>
</body>
</html>
Navigate to example.com/phptest.php from your local machine. If the components of your LAMP stack are working correctly, the browser will display a “Connected successfully” message.