Introduction
This documentation will guide you through how to set up a LAMP stack (Linux, Apache, MySQL, PHP) on your Ubuntu machine. A LAMP stack is a popular and powerful web development environment that allows you to create dynamic websites and applications.
We will guide you on how to install and configure each component of the LAMP stack step by step. You will also learn how to test your LAMP stack and troubleshoot common issues. By the end, you will have a fully functional LAMP stack ready for your web projects. Let’s get started!
Installing Apache
Apache is a widely used web server software that allows your Ubuntu machine to serve web content to users accessing it through their web browsers. To install Apache on your Linux Ubuntu machine, follow these steps:
- Open a terminal window.
- Run the following command to update the packages and make sure you have the latest:
- Then, install the Apache package by running:
- Once the installation is complete, Apache should be up and running automatically. You can verify this by entering your server's IP address or hostname into a web browser. If Apache is installed correctly, you should see the default Apache2 Ubuntu Default Page.
sudo apt update
sudo apt install apache2
Installing PHP
PHP is a server-side scripting language commonly used for web development. To install PHP on your Ubuntu machine, follow these steps:
- Open a terminal window.
- Install PHP along with the necessary modules by running:
- After the installation is complete, restart the Apache web server to apply the changes:
sudo apt install php8.2 libapache2-mod-php8.2 php8.2-mysql
sudo systemctl restart apache2
Installing MySQL
MySQL is a popular open-source relational database management system used to store and manage data. To install MySQL on your Ubuntu machine, follow these steps:
- Open a terminal window.
- Install the MySQL server package by running:
- Once the installation is complete, MySQL should start automatically. You can verify this by checking the MySQL service status:
sudo apt install mysql-server
sudo systemctl status mysql
Adding a new user
To add a new user to MySQL, follow these steps:
- Log in to the MySQL root account using the following command:
- Enter your MySQL root password when prompted.
- Once logged in, create a new MySQL user with the desired username and password:
- Replace 'newuser' with the desired username and 'password' with the desired password.
- Grant appropriate privileges to the new user. For example, to grant all privileges on all databases:
- Flush the privileges to apply the changes and exit the MySQL shell:
sudo mysql -u root -p
CREATE USER 'newuser'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
exit;
Adding a new database
To add a new database to MySQL, follow these steps:
- Log in to the MySQL root account if necessary:
- Enter your MySQL root password when prompted.
- Once logged in, create a new database.
- Replace 'dbname' with the desired database name.
sudo mysql -u root -p
CREATE DATABASE dbname;
Assigning permissions
To assign your user permissions to our newly created database, follow these steps:
- Log in to the MySQL root account if necessary:
- Enter your MySQL root password when prompted.
- Grant the user permission to access and manipulate the new database:
- Replace 'dbname' with the database name and 'username' with the username you created earlier.
- Flush the privileges to apply the changes and exit the MySQL shell:
sudo mysql -u root -p
GRANT ALL ON dbname.* TO 'username'@'%';
FLUSH PRIVILEGES;
exit;
Configuring a vhost
A virtual host (vhost) allows Apache to serve multiple websites from the same IP address. To configure a vhost in Apache on your Ubuntu machine, follow these steps:
- Navigate to the Apache sites-available directory:
- Create a new configuration file for your virtual host. You can copy the default configuration file as a template:
- Edit the newly created configuration file with a text editor of your choice:
- Configure the virtual host by specifying the server name, document root, and any other necessary settings. Here's an example configuration:
- Save the changes and exit the text editor.
- Enable the virtual host by creating a symbolic link to the sites-enabled directory:
- Reload Apache to apply the changes:
cd /etc/apache2/sites-available;
sudo cp 000-default.conf yourdomain.conf
sudo nano yourdomain.conf
<VirtualHost *:80>
ServerName yourdomain.com
DocumentRoot /var/www/yourdomain.com/public_html
ErrorLog ${APACHE_LOG_DIR}/yourdomain_error.log
CustomLog ${APACHE_LOG_DIR}/yourdomain_access.log combined
</VirtualHost>
sudo a2ensite yourdomain.conf
sudo systemctl reload apache2
Testing the LAMP stack
To test that your LAMP (Linux, Apache, MySQL, PHP) stack is working correctly, follow these steps:
- Create a PHP info file in the Apache document root directory:
- Add the following PHP code to the file:
- Save the changes and exit the text editor.
- Open a web browser and navigate to http://your_server_ip/info.php (replace 'your_server_ip' with your server's IP address).
- If PHP is installed and configured correctly, you should see a page displaying detailed information about your PHP configuration.
sudo nano /var/www/html/info.php
<?php
phpinfo();
?>
This concludes the installation and configuration of Apache, PHP, MySQL, and setting up a virtual host on your Linux Ubuntu machine. You now have a fully functional LAMP stack ready for web development and hosting.