How to create a multiboot USB drive in Linux with bash
December 27, 2022How to automate tasks in Linux with cron
January 1, 2023Setting up a LAMP stack (Linux, Apache, MySQL, PHP) on Ubuntu
Setting up a LAMP stack (Linux, Apache, MySQL, PHP) on Ubuntu can seem intimidating at first, but with a little guidance, it’s actually quite straightforward. In this tutorial, we’ll walk you through the process step by step, and provide all the necessary code to get your LAMP stack up and running.
First, let’s start by installing Apache. Apache is the most popular web server software, and it’s included in the default repositories for Ubuntu, so installation is easy. Open up a terminal and run the following command:
sudo apt-get update sudo apt-get install apache2
This will install Apache and all the necessary dependencies. To test that Apache is working correctly, open up a web browser and go to http://localhost/. You should see the default Apache landing page.
Next, let’s install MySQL. MySQL is a popular open-source database management system that is commonly used with PHP. To install it, run the following command:
sudo apt-get install mysql-server
During the installation process, you’ll be asked to set a password for the MySQL root user. Make sure to choose a strong password and remember it, as you’ll need it later.
Once MySQL is installed, we can proceed to install PHP. PHP is a server-side scripting language that is commonly used for web development. To install it, run the following command:
sudo apt-get install php libapache2-mod-php
This will install PHP and the Apache module for PHP, which allows Apache to process PHP scripts.
Now that we have all the components of the LAMP stack installed, let’s proceed to configure them. First, we’ll need to create a MySQL database and user for our application. To do this, log in to the MySQL command line as the root user:
mysql -u root -p
Enter the password you chose earlier when prompted. Once you’re logged in, run the following commands to create a new database and user:
CREATE DATABASE myapp; CREATE USER 'myappuser'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON myapp.* TO 'myappuser'@'localhost'; FLUSH PRIVILEGES;
Replace “myapp” with the desired name for your database, and “myappuser” and “password” with your desired username and password.
With the database and user created, we can now configure Apache to use PHP. To do this, open the Apache configuration file in a text editor:
sudo nano /etc/apache2/apache2.conf
Add the following line to the file:
AddType application/x-httpd-php .php
This tells Apache to treat files with the .php extension as PHP scripts. Save the file and exit the text editor.
Next, we’ll need to create a PHP test script to make sure that everything is working correctly. To do this, create a new file in the Apache document root directory:
sudo nano /var/www/html/info.php
Add the following code to the file:
<?php phpinfo(); ?>
Save the file and exit the text editor. Then, restart Apache to apply the changes:
sudo service apache2 restart
Now, open up a web browser and go to http://localhost/info.php. You should see a page with information about your PHP installation.
That’s it! You now have a fully functional LAMP stack on Ubuntu. You can now start developing PHP applications and storing data in the MySQL database.
Here are a few additional tips to help you get started:
- If you want to install additional PHP modules, you can use the following command:
sudo apt-get install php-&amp;lt;module-name&amp;gt;
Replace “<module-name>” with the name of the module you want to install.
- If you want to allow remote access to your MySQL database, you’ll need to edit the MySQL configuration file. Open the file in a text editor:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Find the line that says “bind-address = 127.0.0.1” and change it to “bind-address = 0.0.0.0”. This will allow connections from any IP address. Save the file and exit the text editor, then restart MySQL:
sudo service mysql restart
- If you want to allow .htaccess files to override Apache configuration, you’ll need to enable the “AllowOverride” directive in the Apache configuration file. Open the file in a text editor:
sudo nano /etc/apache2/apache2.conf
FFind the section that says “” and add the following line:
AllowOverride All
Save the file and exit the text editor, then restart Apache:
sudo service apache2 restart
Configure virtual Hosts with Let’s Encrypt certificates
To configure the VirtualHosts (vhosts) for your domains using the SSL certificates obtained from Let’s Encrypt, you’ll need to follow these steps:
- Create a new Apache configuration file for each domain. For example, to create a configuration file for domain.com, run the following command:
sudo nano /etc/apache2/sites-available/domain.com.conf
- Add the following VirtualHost entry to the file, replacing “domain.com” with your actual domain name:
<VirtualHost *:80> ServerName domain.com Redirect permanent / https://domain.com/ </VirtualHost> <VirtualHost *:443> ServerName domain.com DocumentRoot /var/www/domain.com SSLEngine on SSLCertificateFile /etc/letsencrypt/live/domain.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/domain.com/privkey.pem </VirtualHost>
Make sure to replace “/var/www/domain.com” with the root directory of your website.
- Repeat the above steps for each additional domain, creating a separate configuration file and VirtualHost entry for each domain.
- Enable the vhosts by running the following commands:
sudo a2ensite domain.com.conf
Repeat the command for each domain, replacing “domain.com” with the actual domain name.
- Restart Apache to apply the changes:
sudo service apache2 restart
That’s it! With these steps, you should now have a fully functional LAMP stack on Ubuntu, your domains should now be configured to use SSL certificates from Let’s Encrypt, and your Apache vhosts should be properly configured, tehrefore you can start developing your PHP applications with confidence.
Happy coding!