Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

How to Install LEMP Stack on Ubuntu Linux for Web Hosting

How to Install LEMP Stack on Ubuntu Linux for Web Hosting

Setting up a LEMP stack on Ubuntu Linux is an excellent way to host your websites locally or on a server. LEMP stands for Linux, Nginx, MySQL (or MariaDB), and PHP — a powerful combination for serving dynamic websites efficiently. This guide walks you through each step, ensuring you can deploy a robust web server environment. Before starting, ensure your Ubuntu system is up to date and has a stable internet connection. You might also want to have sudo privileges on your server to install and configure software seamlessly.

What You’ll Need

  • An Ubuntu Linux server or machine (Ubuntu 20.04 LTS or newer recommended)
  • Root or sudo privileges
  • Basic knowledge of terminal commands
  • Optional: a domain name pointed to your server’s IP address

Step-by-Step

  1. Update your system packages: Open your terminal and run:
    sudo apt update && sudo apt upgrade -y

    This ensures all your packages are current and reduces compatibility issues.

  2. Install Nginx: Nginx is a high-performance web server. Install it with:
    sudo apt install nginx -y

    After installation, verify it’s running with:

    sudo systemctl status nginx

    If active and running, proceed to the next step.

  3. Install MySQL or MariaDB: Choose your preferred database. For MySQL:
    sudo apt install mysql-server -y

    For MariaDB:

    sudo apt install mariadb-server -y

    Secure your database server with:

    sudo mysql_secure_installation

    Follow the prompts to set a root password and secure your database.

  4. Install PHP and necessary modules: PHP processes dynamic content. Install PHP-FPM (FastCGI Process Manager) along with common modules:
    sudo apt install php-fpm php-mysql php-cli php-common php-curl php-gd php-mbstring php-xml php-zip -y

    Verify PHP-FPM is running:

    sudo systemctl status php*-fpm
  5. Configure Nginx to use PHP: Create a new server block or modify the default to support PHP. For example:
    sudo nano /etc/nginx/sites-available/your_site

    Add the following configuration:

    server {
        listen 80;
        server_name your_domain.com;
        root /var/www/html;
    
        index index.php index.html index.htm;
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php-fpm.sock;
        }
    
        location ~ /\.ht {
            deny all;
        }
    }
    

    Save and link this configuration:

    sudo ln -s /etc/nginx/sites-available/your_site /etc/nginx/sites-enabled/

    Test the configuration for syntax errors:

    sudo nginx -t

    If successful, reload Nginx:

    sudo systemctl reload nginx
  6. 6. Test PHP Processing: Create a PHP info file:

    sudo nano /var/www/html/info.php

    Add:

    <?php phpinfo(); ?>

    Access http://your_server_ip/info.php in your browser. If PHP info displays, your server is configured correctly.

    7. Secure your server: Adjust firewall rules to allow HTTP and HTTPS traffic:

    sudo ufw allow 'Nginx Full'

    Optionally, set up SSL certificates using Let’s Encrypt for HTTPS support.

Common Mistakes

  • Not updating your system before starting — can cause dependency issues.
  • Forgetting to reload or restart Nginx after configuration changes.
  • Using incorrect PHP socket paths — ensure your PHP version matches the socket in your config.
  • Not setting proper permissions for your web root directory.
  • Failing to secure your database or server with firewalls or SSL.

Tips

  • Use domain names rather than IP addresses for easier management.
  • Regularly update your server packages to patch vulnerabilities.
  • Configure automatic backups of your database and website files.
  • Disable unnecessary services to improve security and performance.
  • Monitor your server’s resource usage with tools like htop or top.

Troubleshooting

If your server isn’t working as expected, follow these steps:

  1. Check service statuses: Ensure Nginx, PHP-FPM, and your database are running:
    sudo systemctl status nginx php*-fpm mysql
  2. Review logs: Nginx logs are at /var/log/nginx/error.log, PHP errors at /var/log/php-fpm.log, and MySQL errors at /var/log/mysql/error.log.
    Use tail or less to examine recent logs:

    sudo tail -f /var/log/nginx/error.log
  3. Verify configuration syntax: Run:
    sudo nginx -t

    and fix any errors reported.

  4. Test network connectivity: Ensure your server is reachable via ping or SSH.
  5. Firewall issues: Confirm UFW or other firewalls aren’t blocking HTTP/HTTPS ports.

If issues persist, consider reinstalling components or consulting community forums for specific error messages.

Frequently Asked Questions

1. Can I install LEMP on Ubuntu Desktop?

Yes, but Ubuntu Desktop isn’t optimized for server use. It’s better to use Ubuntu Server for production hosting.

2. How do I secure my LEMP stack?

Use firewalls, disable root login, set strong passwords, and consider installing SSL certificates with Let’s Encrypt.

3. Is Nginx faster than Apache?

Generally, yes. Nginx is designed for high concurrency and low resource consumption, making it faster for serving static and dynamic content.

4. How do I upgrade PHP on Ubuntu?

Use the ondrej PPA repository to install newer PHP versions and switch between them as needed.

5. Can I run multiple websites on one server?

Yes, by configuring separate server blocks in Nginx for each domain or subdomain.

6. How do I enable HTTPS on my server?

Install Certbot and obtain free SSL certificates from Let’s Encrypt, then configure your Nginx server to use them.

Following this comprehensive guide, you should have a fully functional LEMP stack on your Ubuntu Linux machine, ready to host your websites securely and efficiently. Regular maintenance and security practices will keep your server running smoothly for years to come.

Related

How to Install LEMP Stack on Ubuntu Linux for Web Hosting
Scroll to top