Setting up Magento 2 requires a structured process. Here’s a step-by-step guide to installing Magento 2, typically on a Linux-based system (Ubuntu, CentOS, or similar). For this example, we’ll assume you’re setting it up on a LAMP stack (Linux, Apache, MySQL, PHP). However, you can adapt this for other environments like Nginx or use services like Docker, but we’ll focus on Apache.
Before starting, make sure your server meets the Magento 2 requirements:
Update Your Server:
sudo apt update
sudo apt upgrade
Install Apache:
sudo apt install apache2
sudo systemctl enable apache2
sudo systemctl start apache2
Install MySQL:
sudo apt install mysql-server
sudo systemctl start mysql
sudo mysql_secure_installation
If you want MariaDB instead:
sudo apt install software-properties-common dirmngr -y && apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'
sudo add-apt-repository 'deb [arch=amd64,i386,ppc64el] https://mirrors.ukfast.co.uk/sites/mariadb/repo/10.5/debian buster main'
sudo apt install mariadb-server mariadb-client
mysql -u root -p
CREATE DATABASE magento2;
CREATE USER 'magento_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON magento2.* TO 'magento_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Install PHP and Extensions:
Install PHP 7.4 or 8.x with the required extensions.
sudo apt install php libapache2-mod-php php-curl php-json php-mbstring php-intl php-xml php-zip php-soap php-bcmath php-iconv php-gd php-mysql
Verify PHP version:
php -v
Install Composer:
Magento 2 is installed via Composer.
sudo apt install composer
Enable Apache Rewrite Module:
sudo a2enmod rewrite
sudo systemctl restart apache2
Create a Directory for Magento:
sudo mkdir -p /var/www/html/magento2
sudo chown -R $USER:$USER /var/www/html/magento2
Use Composer to Download Magento:
Navigate to the Magento directory and use Composer to create the Magento project.
For Magento Open Source:
cd /var/www/html/magento2
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.5 .
(Replace 2.4.5 with the latest version or desired version).
You’ll be prompted for Magento authentication keys. You can get these keys from your Magento Marketplace account.
After Magento has been downloaded, set the necessary file and folder permissions:
Set Ownership:
sudo chown -R www-data:www-data /var/www/html/magento2
Set Directory Permissions:
find var generated vendor pub/static pub/media app/etc -type f -exec chmod u+w {} +
find var generated vendor pub/static pub/media app/etc -type d -exec chmod u+w {} +
chmod u+x bin/magento
Create a Virtual Host Configuration:
sudo nano /etc/apache2/sites-available/magento2.conf
Add the following configuration:
<VirtualHost *:80>
ServerName yourdomain.com
DocumentRoot /var/www/html/magento2
<Directory /var/www/html/magento2>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/magento_error.log
CustomLog ${APACHE_LOG_DIR}/magento_access.log combined
</VirtualHost>
Replace yourdomain.com with your actual domain or IP.
Enable the Virtual Host:
sudo a2ensite magento2.conf
sudo systemctl restart apache2
Run the Magento Installation Command:
cd /var/www/html/magento2
bin/magento setup:install \
--base-url=http://yourdomain.com \
--db-host=localhost \
--db-name=magento2 \
--db-user=magento_user \
--db-password=your_password \
--admin-firstname=Admin \
--admin-lastname=User \
--admin-email=admin@example.com \
--admin-user=admin \
--admin-password=admin123 \
--language=en_US \
--currency=USD \
--timezone=America/Chicago \
--use-rewrites=1
Replace the parameters as needed, such as your domain name, database credentials, admin user, and password.
Flush Cache and Reindex:
After the installation, you can clear the cache and reindex.
bin/magento cache:clean
bin/magento cache:flush
bin/magento indexer:reindex
Open your browser and visit:
http://yourdomain.comhttp://yourdomain.com/adminLog in to the admin panel with the credentials you set during installation.
Set Magento to Production Mode:
bin/magento deploy:mode:set production
Set Up Cron Jobs:
Magento relies on cron jobs for many automated tasks.
sudo crontab -u www-data -e
Add the following:
*/5 * * * * /usr/bin/php /var/www/html/magento2/bin/magento cron:run | grep -v "Ran jobs by schedule" >> /var/www/html/magento2/var/log/magento.cron.log