Skip to main content

Apache - Setup Magento2

# Install MySQL
sudo dnf install -y mariadb-server mariadb

# Install PHP 8.3 via Remi with Magento-required extensions
sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm
sudo dnf module reset php -y
sudo dnf module enable php:remi-8.3 -y

# Core stack + almost all required extensions
sudo dnf install -y \
  httpd mod_ssl php php-cli php-fpm php-mysqlnd php-zip php-gd php-mbstring \
  php-curl php-xml php-intl php-bcmath php-soap php-opcache php-sodium \
  php-pear php-devel php-gmp php-imagick php-redis php-memcached \
  php-mysqli

# Install Elasticsearch (required by Magento 2.4+)
sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
sudo tee /etc/yum.repos.d/elasticsearch.repo << 'REPO'
[elasticsearch]
name=Elasticsearch repository
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
REPO

sudo dnf install -y elasticsearch

# Configure MySQL
sudo systemctl enable --now mariadb

sudo mysql 'SQL'
CREATE DATABASE magento CHARACTER SET utf8mb4;
CREATE USER 'magento'@'localhost' IDENTIFIED BY 'MagentoPass456!';
CREATE USER 'magento'@'%' IDENTIFIED BY 'MagentoPass456!';
GRANT ALL PRIVILEGES ON magento.* TO 'magento'@'localhost';
GRANT ALL PRIVILEGES ON magento.* TO 'magento'@'%';
FLUSH PRIVILEGES;
SQL

# Configure Elasticsearch
sudo systemctl enable --now elasticsearch

# Verify Elasticsearch is running
curl -s http://localhost:9200

# Tune PHP for Magento
sudo tee /etc/php.d/99-magento.ini << 'PHP'
memory_limit = 4G
max_execution_time = 180
max_input_time = 180
post_max_size = 128M
upload_max_filesize = 128M
date.timezone = America/New_York
opcache.enable=1
opcache.memory_consumption=512
opcache.interned_strings_buffer=24
opcache.max_accelerated_files=20000
opcache.revalidate_freq=0
PHP

# Install Composer if not already present
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php --install-dir=/usr/local/bin --filename=composer

# Create the Magento project
# You will need Magento authentication keys from marketplace.magento.com
cd /var/www/html
sudo composer create-project --repository-url=https://repo.magento.com/ \
  magento/project-community-edition magento2

# Set file permissions
sudo chown -R apache:apache /var/www/html/magento2
find /var/www/html/magento2 -type d -exec chmod 755 {} \;
find /var/www/html/magento2 -type f -exec chmod 644 {} \;
sudo chmod -R 775 /var/www/html/magento2/{var,generated,pub/static,pub/media,app/etc}
sudo chmod 755 /var/www/html/magento

# Install Magento 2
cd /var/www/html/magento2
bin/magento setup:install \
  --base-url=http://www.houseestatesales.com/ \
  --db-host=localhost \
  --db-name=magento \
  --db-user=magento \
  --db-password='MagentoPass456!' \
  --admin-firstname=Admin \
  --admin-lastname=User \
  --admin-email=steve.ling@sflservicesllc.com \
  --admin-user=admin \
  --admin-password='Admin456!' \
  --language=en_US \
  --currency=USD \
  --timezone=America/New_York \
  --use-rewrites=1 \
  --search-engine=elasticsearch8

# After install → set production mode & compile
bin/magento deploy:mode:set production
bin/magento setup:di:compile
bin/magento setup:static-content:deploy -f
bin/magento cache:clean

# Configure Apache Site
sudo tee /etc/httpd/conf.d/magento.conf << 'APACHE'
<VirtualHost *:80>
    ServerName www.houseestatesales.com
    ServerAlias houseestatesales.com

    DocumentRoot /var/www/html/magento2

    <Directory "/var/www/html/magento2">
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    # Magento performance recommendations
    <IfModule mod_expires.c>
        ExpiresActive On
        ExpiresDefault "access plus 1 year"
    </IfModule>

    ErrorLog /var/log/httpd/magento_error.log
    CustomLog /var/log/httpd/magento_access.log combined
</VirtualHost>
APACHE

# Enable-Start Appache
sudo systemctl enable --now httpd
sudo systemctl status httpd