Setup Debian 11 server to host Magento
1.1 Update and upgrade the system
First, update your package lists and upgrade the system:
sudo apt update
sudo apt upgrade -y
1.2 Install necessary packages
Install the necessary packages including Apache, MySQL, PHP and their extensions:
sudo apt install apache2 mariadb-server libapache2-mod-php
sudo apt install php7.4 php7.4-mysql php7.4-dom php7.4-simplexml php7.4-ssh2 php7.4-xml php7.4-xmlreader php7.4-curl php7.4-exif php7.4-ftp php7.4-gd php7.4-iconv php7.4-imagick php7.4-json php7.4-mbstring php7.4-posix php7.4-sockets php7.4-tokenizer
1.3 Configure MySQL (MariaDB)
Next, secure your MariaDB installation:
sudo mysql_secure_installation
Answer the prompts as appropriate for your setup.
Now, log in to the MariaDB shell and create a database and user for Magento:
sudo mariadb
Inside the MariaDB shell, run:
CREATE DATABASE magento;
GRANT ALL ON magento.* TO 'magento'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
EXIT;
Replace 'password'
with a strong password.
1.4 Download and install Composer
Composer is a tool for dependency management in PHP. Magento 2 uses Composer for package management. Install it by running:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
1.5 Install Magento
You can install Magento via Composer. But before doing that, you need to get authentication keys from the Magento Marketplace. Go to the Magento Marketplace, log in and create a new access key. You will get a public key (username) and a private key (password).
Now, navigate to the directory where you want to install Magento and use Composer to install it:
cd /var/www/
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition magento
During the installation process, Composer will ask for the authentication keys. Enter the keys you obtained from the Magento Marketplace.
After installation, set the appropriate file and folder permissions:
sudo chown -R www-data:www-data /var/www/magento
sudo find /var/www/magento -type d -exec chmod 770 {} \;
sudo find /var/www/magento -type f -exec chmod 660 {} \;
sudo chmod u+x /var/www/magento/bin/magento
1.6 Configure Apache
Next, create an Apache configuration file for Magento:
sudo nano /etc/apache2/sites-available/magento.conf
In the file, add:
<VirtualHost *:80>
DocumentRoot /var/www/magento/
<Directory /var/www/magento/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/magento_error.log
CustomLog ${APACHE_LOG_DIR}/magento_access.log combined
</VirtualHost>
Save the file and exit the editor. Enable the new site and the Apache rewrite module:
sudo a2ensite magento.conf
sudo a2enmod rewrite
Restart Apache to apply the changes:
sudo systemctl restart apache2
Now, you should have a working Magento installation.