Do what I did! Setting up a LAMP stack (Linux, Apache, MySQL/MariaDB, PHP) on a server is a common task for hosting websites and web applications. Below are the steps to set up a LAMP stack on a server running Linux Ubuntu
Step 1: Update the Server
First, update the package index and upgrade your server packages to the latest versions.
sudo apt update
sudo apt upgrade
Step 2: Install Apache
Apache is the web server that will serve your website.
sudo apt install apache2
- After installation, you can check if Apache is running by visiting your server’s IP address in a web browser. You should see the default Apache welcome page.
- Ensure Apache starts on boot:
sudo systemctl enable apache2
Step 3: Install MySQL/MariaDB
MySQL or MariaDB is the database management system that will store your website’s data.
sudo apt install mysql-server
- Secure the MySQL installation:
sudo mysql_secure_installation
- Follow the prompts to secure your installation (set the root password, remove anonymous users, disallow root login remotely, etc.).
Step 4: Install PHP
PHP is the server-side scripting language that will process the dynamic content on your website.
sudo apt install php libapache2-mod-php php-mysql
- Restart Apache to load PHP:
sudo systemctl restart apache2
Step 5: Test PHP Processing
Create a PHP file to test if PHP is working correctly with Apache.
sudo nano /var/www/html/info.php
- Add the following content to the file:
<?php
phpinfo();
?>
- Save the file and exit the editor.
- Visit
http://your_server_ip/info.php
in your web browser. You should see the PHP information page, which indicates that PHP is correctly installed and running.
Step 6: Adjust Firewall Settings (Optional)
If you have a firewall enabled, you need to allow HTTP and HTTPS traffic.
sudo ufw allow in "Apache Full"
Step 7: Set Up Virtual Hosts (Optional)
If you want to host multiple websites on the same server, you need to set up virtual hosts.
- Create a directory for your website:
sudo mkdir /var/www/your_domain
- Set permissions:
sudo chown -R $USER:$USER /var/www/your_domain
sudo chmod -R 755 /var/www/your_domain
- Create a new virtual host configuration file:
sudo nano /etc/apache2/sites-available/your_domain.conf
- Add the following content, replacing
your_domain
with your actual domain name:
<VirtualHost *:80>
ServerAdmin webmaster@your_domain
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>
- Enable the new virtual host:
sudo a2ensite your_domain.conf
sudo systemctl reload apache2
Step 8: Enable PHP Modules (Optional)
You may need additional PHP modules for your application. You can search for and install them as needed:
sudo apt search php-
sudo apt install php-module_name
- Replace
module_name
with the name of the module you need.
Step 9: Remove Test Page (Optional)
For security reasons, you may want to delete the info.php
test page.
sudo rm /var/www/html/info.php
Step 10: Access MySQL via the Command Line
To manage your databases, log in to the MySQL shell:
sudo mysql -u root -p
- Enter your MySQL root password when prompted.
Step 11: Restart All Services
Finally, restart all services to ensure everything is running correctly:
sudo systemctl restart apache2
sudo systemctl restart mysql
Step 12: Secure Your Server (Optional)
For production environments, consider securing your server by disabling unnecessary services, setting up SSH keys, using a strong firewall, and installing security updates regularly.
With these steps, your LAMP stack should be fully operational and ready to host websites or applications.