LAMP Installation on Ubuntu
Connect with your server using SSH
Example :
SSH -i private_key.pem username@your_server_IP
Step 1 — Installing Apache on Ubuntu
Install Apache using Ubuntu’s package manager, apt:
sudo su
sudo apt update
sudo apt install apache2
Press Y and hit ENTER to continue, and the installation will proceed.
Step 2 — Adjust the Firewall to Allow Web Traffic
Run the following command to check all available profile
sudo ufw app list
Run the following command to allow incoming HTTP and HTTPS traffic for this profile:
sudo ufw allow in "Apache Full"
Step 2 — Installing MySQL on Ubuntu
sudo apt install mysql-server
First, open up the MySQL prompt:
sudo mysql
Next, execute the subsequent ALTER USER command to modify the authentication method of the root user to use a password. In the following example, it changes the authentication method to ‘mysql_native_password’:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
After making this change, exit the MySQL prompt:
Exit
Run the security script with sudo:
sudo mysql_secure_installation
Press enter for all questions asked during installation of the mysql.
Step 3 — Installing PHP on Ubuntu
Once again, leverage the apt system to install PHP
sudo apt install php libapache2-mod-php php-mysql
If it asks for additional data will be used always Y and hit enter
Setup Index page
To accomplish this, enter the following command to open the ‘dir.conf’ file in a text editor with root privileges:
sudo nano /etc/apache2/mods-enabled/dir.conf
sudo systemctl restart apache2
Step 4 — Install PHP 8.1 Modules
sudo apt-get install php8.1-xml php8.1-curl php8.1-dev php8.1-gd php8.1-mbstring php8.1-zip php8.1-mysql php8.1-xml php8.1-bcmath php8.1-intl php8.1-soap
After this, restart the Apache web server in order for your changes to be recognized. Do this by typing this:
sudo systemctl restart apache2
Step 5 — Check Apache2 status
Go to server url and check apache2 is running or not
Step 6 — Enable Apache Mod_Rewrite
You can enable any Apache module using the a2enmod command. So run the command below on your Ubuntu server:
sudo a2enmod rewrite
sudo nano /etc/apache2/sites-available/000-default.conf
By default, Apache does not allow the use of ‘.htaccess’ file so you will need to edit the configuration of each website’s virtual host file by adding the following code:
<Directory /var/www/html> Options Indexes FollowSymLinks MultiViews AllowOverride All Require all granted </Directory>
For instance, you can edit the default virtual hosts that ships with Apache using a nano editor by typing the command below:
Then copy paste the above text just before the ‘</VirtualHost>’ closing tag. Remember to save the file by pressing CTRL + X then Y and Enter
Then, restart Apache for the changes to take effect:
sudo systemctl restart apache2
Magento 2.4.X Installation On Ubuntu
Step 7 — Installing Composer
Run following command to install composer
sudo apt update
First, make sure you’re in your home directory:
cd ~
Then, retrieve the installer using curl:
curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
Then, retrieve the installer using curl:
HASH=`curl -sS https://composer.github.io/installer.sig`
To output the obtained value, run:
echo $HASH
Now execute the following PHP code, as provided in the Composer download page, to verify that the installation script is safe to run:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
You’ll see the following output:
Installer verified
To install composer globally, use the following command to download and install Composer as a system-wide command named composer under /usr/local/bin:
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
Step 8 — Download Magento 2.4.6
Change root directory using following command
cd /var/www/html/
The following command can be run from the prompt
sudo chmod -R 777 /var/www/html/
Delete index.html available at the folder
Run following composer command to download Magento2 package.
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition:2.4.6 /var/www/html/
It will asked for username and password as per screenshot, you Obtain authentication keys for the Magento code repository.
sudo chmod -R 777 var/ pub/ generated/
sudo chmod -R 777 app/etc
Step 9 — Installing Elasticsearch
curl -fsSL https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list
Next, update your package lists so APT will read the new Elastic source:
sudo apt update
Then install Elasticsearch with this command:
sudo apt install elasticsearch
Step 11 — Configuring Elasticsearch
To configure Elasticsearch, navigate to its primary configuration file named ‘elasticsearch.yml’. This file holds a majority of configuration options and is typically located in the ‘/etc/elasticsearch’ directory.
Use your preferred text editor to edit Elasticsearch’s configuration file. Here, we’ll use nano:
sudo nano /etc/elasticsearch/elasticsearch.yml
/etc/elasticsearch/elasticsearch.yml . . . network.host: localhost
We’ve specified ‘localhost’ in order for Elasticsearch to listen on all interfaces and bound IPs. If you wish for it to exclusively listen on a particular interface, you can specify its IP address in place of ‘localhost’. Save and close the ‘elasticsearch.yml’ file. If you’re using ‘nano’, you can achieve this by pressing CTRL+X, followed by ‘Y’, and then hitting ENTER.
These are the minimum settings you can start with in order to use Elasticsearch. Now you can start Elasticsearch for the first time.
Start the Elasticsearch service with systemctl. Give Elasticsearch a few moments to start up. Otherwise, you may get errors about not being able to connect.
sudo systemctl start elasticsearch
Next, run the following command to enable Elasticsearch to start up every time your server boots:
sudo systemctl enable elasticsearch
sudo systemctl restart elasticsearch
Step 12 — Installing Magento 2.4.6
Connect to database using mysql and create new database
mysql -u root -p
Enter the password in when asked as shown in screenshot
Type exit to exit from mysql and press enter
Now run the following command to install Magento 2.
php bin/magento setup:install --base-url="http://your-domain-name.com/" --db-host="localhost" --db-name="magento2" --db-user="your-sql-username" --db-password="mysql-password" --admin-firstname="Admin" --admin-lastname="W3ctrl" --admin-email="admin@w3ctrl.com" --admin-user="magento2-admin" --admin-password="Admin#404$" --language="en_US" --currency="USD" --timezone="America/Chicago" --use-rewrites="1"
You can change the mentioned param value as per your URL,database and other settings.
Once it is completed, you will get an admin URL that will be used to login into the back-end.
Now run the following command in the Magento 2 root directory
php bin/magento cache:clean && php bin/magento cache:flush chmod -R 777 var pub/static/ generated/ chmod -R 777 var pub/static/ generated/
Now you can access the front-end with the mentioned URL and also back-end with back-end URL, use username and password that you mentioned during install command.