How do I install Magento on a live server?

Install Magento on a Live Server

Installing Magento on a live server involves several steps, including preparing the server environment, downloading and configuring Magento, and setting up your domain and database. Here is a detailed guide to help you through the process:

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

Prerequisites

  • Server Requirements:
    • 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.
  • Server Access:
    • SSH access to your server.
    • Root or sudo privileges.

Step 1: Prepare Your Server Environment

Update and Upgrade Your Server:

sudo apt update
sudo apt upgrade

Install Apache/Nginx:

For Apache:

sudo apt install apache2

For Nginx:

sudo apt install nginx

Install MySQL/MariaDB:

For MySQL:

sudo apt install mysql-server

For MariaDB:

sudo apt install mariadb-server

Install PHP and Required 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@yourdomain.com
    DocumentRoot /var/www/html/magento
    ServerName yourdomain.com

    <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 yourdomain.com;
    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

Navigate to the Web Root Directory:

cd /var/www/html

Download Magento Using Composer:

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://yourdomain.com \
--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: Set Up Domain and SSL

Update DNS Records:

Point your domain name to the server's IP address.

Obtain an SSL Certificate:

Use Let's Encrypt to obtain a free SSL certificate:

sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d yourdomain.com

For Nginx:

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com

Step 6: Access Magento Admin

Open Your Browser:

Navigate to http://yourdomain.com/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 live Magento installation running on your server. This setup allows you to manage and customize your Magento store for your business needs. Ensure you regularly back up your installation and keep your server and Magento installation updated for optimal performance and security.