How do I install Magento on a local server?

Install Magento on a Local Server

Installing Magento on a local server involves several steps, including setting up the required software, downloading Magento, and configuring the installation. Below is a detailed guide to help you through the process:

Step-by-Step Guide to Install Magento on a Local Server

Prerequisites

  • Operating System: Linux distributions (Ubuntu, CentOS, Debian), Windows, or macOS.
  • Web Server: Apache 2.4 or Nginx 1.x.
  • Database: MySQL 8.0 or MariaDB 10.4/10.3.
  • PHP: PHP 7.4, 8.1, 8.2 with required extensions.
  • Composer: A dependency manager for PHP.

Step 1: Install Required Software

Install Apache/Nginx:

For Apache:

sudo apt update
sudo apt install apache2

For Nginx:

sudo apt update
sudo apt install nginx

Install MySQL/MariaDB:

For MySQL:

sudo apt install mysql-server

For MariaDB:

sudo apt install mariadb-server

Install PHP and Extensions:

sudo apt install php libapache2-mod-php php-common php-mysql php-xml php-curl php-gd php-mbstring php-intl php-bcmath php-json php-soap php-zip php-cli php-xmlrpc

Install Composer:

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

Step 2: Configure Apache/Nginx

For Apache:

Create a virtual host configuration file:

sudo nano /etc/apache2/sites-available/magento.conf

Add the following configuration:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/magento
    ServerName magento.local

    <Directory /var/www/html/magento/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable the new site and rewrite module:

sudo a2ensite magento.conf
sudo a2enmod rewrite
sudo systemctl restart apache2

For Nginx:

Create a server block configuration file:

sudo nano /etc/nginx/sites-available/magento

Add the following configuration:

server {
    listen 80;
    server_name magento.local;
    set $MAGE_ROOT /var/www/html/magento;
    include $MAGE_ROOT/nginx.conf.sample;
}

Enable the server block:

sudo ln -s /etc/nginx/sites-available/magento /etc/nginx/sites-enabled/
sudo systemctl restart nginx

Step 3: Set Up Database

Create a Database:

sudo mysql -u root -p

In the MySQL shell:

CREATE DATABASE magento;
CREATE USER 'magento_user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON magento.* TO 'magento_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 4: Download and Install Magento

Download Magento Using Composer:

cd /var/www/html
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.7 magento

Set Permissions:

sudo find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
sudo find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +
sudo chmod u+x bin/magento

Install Magento:

cd /var/www/html/magento
bin/magento setup:install --base-url=http://magento.local \
--db-host=localhost --db-name=magento --db-user=magento_user --db-password=password \
--admin-firstname=Admin --admin-lastname=User --admin-email=user@example.com \
--admin-user=admin --admin-password=admin123 --language=en_US --currency=USD \
--timezone=America/Chicago --cleanup-database --use-rewrites=1

Step 5: Configure Hosts File

Edit the Hosts File:

sudo nano /etc/hosts

Add the following line:

127.0.0.1 magento.local

Step 6: Access Magento Admin

Open Your Browser:

Navigate to http://magento.local/admin to access the Magento admin panel. Use the admin credentials set during installation to log in.

Conclusion

By following these steps, you will have a local Magento installation running on your server. This setup allows you to develop, test, and customize your Magento store before deploying it to a live environment. Ensure you regularly back up your installation and keep your server and Magento installation updated for optimal performance and security.