Setting up a LAMP stack on Ubuntu 22.04 allows you to create a powerful web server environment for hosting websites and applications. LAMP stands for Linux, Apache, MySQL, and PHP, and it’s a widely used stack for web development.
In this guide, you’ll learn how to install and configure a LAMP stack on Ubuntu 22.04.
Before starting, ensure you have:
Start by updating your system packages to ensure you’re using the latest versions.
sudo apt update && sudo apt upgrade -y
Apache is the most popular web server software. Install it with the following command:
sudo apt install apache2 -y
sudo systemctl enable apache2
sudo systemctl start apache2
To check if Apache is running, type:
systemctl status apache2
You can also visit your server’s IP address in a browser:
http://your-server-ip
You should see the Apache default page.
MySQL is a relational database management system that stores website data.
sudo apt install mysql-server -y
Run the security script to remove insecure default settings:
sudo mysql_secure_installation
You’ll be asked to set a root password and remove test databases. Follow the prompts and choose secure options.
systemctl status mysql
To test the connection to MySQL, log in as root:
sudo mysql -u root -p
Enter your root password when prompted. Then, create a test database:
CREATE DATABASE test_db;
USE test_db;
CREATE TABLE test_table (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100));
INSERT INTO test_table (name) VALUES ('Test Entry');
SELECT * FROM test_table;
If you see the inserted entry, your MySQL setup is working correctly. Exit MySQL by typing:
EXIT;
For better security, create a separate MySQL user with privileges for a specific database:
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'securepassword';
GRANT ALL PRIVILEGES ON test_db.* TO 'newuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
This user can now access and manage test_db without using the root account.
PHP is a server-side scripting language used to generate dynamic web pages.
sudo apt install php libapache2-mod-php php-mysql -y
To ensure compatibility with various applications, install the following PHP extensions:
sudo apt install -y php-{common,mysql,xml,xmlrpc,curl,gd,imagick,cli,dev,imap,mbstring,opcache,soap,zip,intl}
php -v
By default, Apache serves index.html before index.php. Modify Apache’s configuration to prioritize PHP files.
sudo nano /etc/apache2/mods-enabled/dir.conf
Move index.php to the beginning of the list:
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
Save the file and restart Apache:
sudo systemctl restart apache2
To enable a site configuration in Apache, you need to create a new virtual host file. Run:
sudo nano /etc/apache2/sites-available/mywebsite.conf
Add the following configuration:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save the file and enable the site:
sudo a2ensite mywebsite.conf
Then restart Apache:
sudo systemctl restart apache2
You might have to edit /etc/apache2/apache2.conf to enable .htaccess overrides.
Open the configuration file:
sudo nano /etc/apache2/apache2.conf
Find the following section:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Change AllowOverride None to AllowOverride All:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Save and exit the file, then restart Apache:
sudo service apache2 restart
Enable the mod_rewrite module:
sudo a2enmod rewrite
Restart Apache again to apply changes:
sudo systemctl restart apache2
Create a PHP test file:
sudo nano /var/www/html/info.php
Add the following content:
<?php
phpinfo();
?>
Save and close the file, then visit:
http://your-server-ip/info.php
If PHP is installed correctly, you will see the PHP info page.
phpMyAdmin is a web-based interface for managing MySQL databases.
sudo apt install phpmyadmin -y
During the installation, select Apache as the web server and choose Yes when asked to configure a database for phpMyAdmin.
After installation, access phpMyAdmin by visiting:
http://your-server-ip/phpmyadmin
Log in using the MySQL credentials you set up earlier.
You have successfully set up a LAMP stack on Ubuntu 22.04. You can now deploy PHP applications and manage MySQL databases. To further optimize performance, consider configuring SSL certificates and additional security measures.
For more tutorials on web hosting and server management, stay tuned!
© 2023 All right reserved to sobiztrend.com
Leave a Reply