Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

How to Install Magento with Nginx on Ubuntu 24.04

How to Install Magento with Nginx on Ubuntu 24.04

Setting up Magento on Ubuntu 24.04 with Nginx allows you to create a high-performance, scalable e-commerce platform optimized for speed and reliability. This guide walks you through the process step-by-step, ensuring a smooth installation even if you’re new to server management. Before starting, ensure your server has a fresh Ubuntu 24.04 installation, a domain name pointed to your server’s IP, and root or sudo access.

What You’ll Need

  • Ubuntu 24.04 server (fresh install recommended)
  • Root or sudo privileges
  • Domain name pointing to your server’s IP address
  • Basic knowledge of Linux command line
  • At least 4GB RAM (preferably more for production)
  • SSL certificate (optional but recommended)

Step-by-Step

  1. Update your system packages: Begin by updating your server to ensure all packages are current.
  2. sudo apt update && sudo apt upgrade -y
  3. Install necessary dependencies: Install PHP, MariaDB, Nginx, and other required packages.
  4. sudo apt install nginx mariadb-server php php-fpm php-cli php-mysql php-zip php-xml php-gd php-curl php-mbstring php-bcmath php-intl php-soap php-bcmath unzip git -y
  5. Secure your MariaDB installation: Run the security script and set a root password.
  6. sudo mysql_secure_installation
  7. Create a Magento database and user: Log into MariaDB and set up database credentials.
  8. sudo mysql -u root -p
    CREATE DATABASE magento;
    CREATE USER 'magento_user'@'localhost' IDENTIFIED BY 'your_password';
    GRANT ALL PRIVILEGES ON magento.* TO 'magento_user'@'localhost';
    FLUSH PRIVILEGES;
    EXIT;
  9. Download Magento: Get the latest Magento Community Edition from the official repository.
  10. cd /var/www
    sudo git clone https://github.com/magento/magento2.git magento
  11. Set permissions: Adjust ownership and permissions for the Magento directory.
  12. sudo chown -R www-data:www-data /var/www/magento
    sudo find /var/www/magento -type f -exec chmod 644 {} \\;
    sudo find /var/www/magento -type d -exec chmod 755 {} \\;
    
  13. Configure Nginx: Create a server block for your domain.
  14. sudo nano /etc/nginx/sites-available/magento
    
    # Paste the following configuration:
    server {
        listen 80;
        server_name yourdomain.com www.yourdomain.com;
        root /var/www/magento;
    
        index index.php;
    
        location / {
            try_files $uri $uri/ /index.php$is_args$args;
        }
    
        location /pub/ {
            try_files $uri $uri/ /pub/index.php$is_args$args;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    
        location ~ /\.ht {
            deny all;
        }
    }
    
    # Enable the site:
    sudo ln -s /etc/nginx/sites-available/magento /etc/nginx/sites-enabled/
    
  15. Test and restart Nginx: Ensure your configuration is correct and reload Nginx.
  16. sudo nginx -t
    sudo systemctl reload nginx
  17. Install Composer: Magento requires Composer for dependency management.
  18. php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
    php composer-setup.php --install-dir=/usr/local/bin --filename=composer
    php -r "unlink('composer-setup.php');"
  19. Run Magento setup via Composer: Install Magento dependencies.
  20. cd /var/www/magento
    composer install
  21. Complete Magento installation: Access your server’s IP or domain in a browser to run the Magento setup wizard or use CLI commands to install Magento with your database details.
  22. php bin/magento setup:install \
    --base-url=http://yourdomain.com \
    --db-host=localhost \
    --db-name=magento \
    --db-user=magento_user \
    --db-password=your_password \
    --admin-firstname=Admin \
    --admin-lastname=User \
    --admin-email=admin@yourdomain.com \
    --admin-user=admin \
    --admin-password=StrongPassword123 \
    --language=en_US \
    --currency=USD \
    --timezone=America/Chicago \
    --use-rewrites=1
  23. Configure SSL (optional but recommended): Obtain an SSL certificate using Let’s Encrypt and configure Nginx for HTTPS.
  24. sudo apt install certbot python3-certbot-nginx
    sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
  25. Finalize permissions and cache: Set permissions and clean cache.
  26. sudo chown -R www-data:www-data /var/www/magento
    php bin/magento cache:flush

Common Mistakes

  • Incorrect permissions: Not setting proper ownership can cause Magento to malfunction. Always ensure www-data owns Magento files.
  • Missing PHP extensions: Forgetting to install necessary PHP modules will cause setup failures. Double-check the list of required extensions.
  • Wrong database credentials: Using incorrect database user or password during setup leads to connection errors. Verify credentials beforehand.
  • Misconfigured Nginx: Syntax errors or incorrect server block settings can prevent the site from loading. Always test configuration with nginx -t.
  • Not securing the site: Running Magento without SSL leaves data vulnerable. Always configure HTTPS especially for production.

Tips

  • Use a swap file: If your server has less than 8GB RAM, create a swap file to improve performance during setup.
  • Regular backups: Always backup your database and files before major updates or changes.
  • Automate SSL renewal: Set up a cron job for Certbot to automatically renew your SSL certificates.
  • Enable Magento caching: After installation, enable caching to improve performance.
  • Monitor server logs: Check Nginx and PHP logs regularly for errors during setup and after deployment.

Troubleshooting

If Magento does not load correctly or you encounter errors:

  • Check error logs: Review /var/log/nginx/error.log and PHP error logs for clues.
  • Verify database connection: Ensure database credentials are correct and the database is accessible.
  • Ensure PHP-FPM is running: Check with sudo systemctl status php8.2-fpm and restart if necessary.
  • Review permissions: Confirm files are owned by www-data and permissions are correct.
  • Test Nginx configuration: Run nginx -t and reload Nginx after any config changes.
  • Clear cache: Run php bin/magento cache:flush to clear any stale cache data.

Frequently Asked Questions

1. Do I need to install Apache instead of Nginx for Magento?

No, Magento can run efficiently on Nginx. This guide specifically covers Nginx installation.

2. How secure is Magento on Ubuntu 24.04?

Security depends on proper configuration, SSL setup, and regular updates. Always keep your server and Magento updated.

3. Can I install Magento on a shared hosting environment with Nginx?

Typically, shared hosting environments do not support custom server configurations like Nginx. A VPS or dedicated server is recommended.

4. How do I upgrade Magento after installation?

Use Composer to update Magento dependencies and follow the official upgrade procedures to ensure data integrity.

5. Is it necessary to use SSL for Magento?

Yes, SSL is essential for securing customer data, especially during checkout. It is highly recommended to enable HTTPS.

Related

How to Install Magento with Nginx on Ubuntu 24.04
Scroll to top