sodcalculator

10 Tech How-To

Install WordPress with Nginx on Ubuntu 20.04 LTS

Guide 10-8211 · Updated · Step by step

This guide walks you through installing WordPress on Ubuntu 20.04 LTS with Nginx as the web server. You’ll end up with a fully functional WordPress site served over HTTP, ready for HTTPS setup. Before starting, ensure you have a Ubuntu 20.04 server with a non-root sudo user, a domain name pointing to your server’s IP, and basic familiarity with the command line.

What You’ll Need

  • Ubuntu 20.04 LTS server with sudo access
  • Domain name (e.g., example.com) with DNS A record pointing to your server
  • Access to a terminal (SSH)
  • Internet connection to download packages

Step-by-Step

  1. Update system packages

    Log into your server and run:

    sudo apt update && sudo apt upgrade -y
    

    This ensures all existing software is up to date.

  2. Install Nginx

    sudo apt install nginx -y
    

    After installation, start and enable Nginx:

    sudo systemctl start nginx
    sudo systemctl enable nginx
    

    Verify it’s running with sudo systemctl status nginx. You should see “active (running)”.

  3. Install MariaDB (MySQL-compatible database)

    sudo apt install mariadb-server mariadb-client -y
    

    Secure the installation:

    sudo mysql_secure_installation
    

    Follow the prompts: set a root password (or use unix_socket), remove anonymous users, disallow root login remotely, remove test database, and reload privileges. For a local setup, answering ‘Y’ to most is fine.

  4. Create WordPress database and user

    Log into MariaDB as root:

    sudo mysql -u root -p
    

    Enter the root password you set. Then create a database and user (replace wpuser and strongpassword with your own):

    CREATE DATABASE wpdatabase;
    CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'strongpassword';
    GRANT ALL PRIVILEGES ON wpdatabase.* TO 'wpuser'@'localhost';
    FLUSH PRIVILEGES;
    EXIT;
    
  5. Install PHP and required extensions

    sudo apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip -y
    

    Check the PHP-FPM service name (usually php7.4-fpm on Ubuntu 20.04):

    sudo systemctl status php7.4-fpm
    

    Ensure it’s running and enabled.

  6. Download and configure WordPress

    Navigate to the web root and download WordPress:

    cd /var/www/html
    sudo wget https://wordpress.org/latest.tar.gz
    sudo tar -xzf latest.tar.gz
    sudo mv wordpress/* .
    sudo rm -rf wordpress latest.tar.gz
    

    Set correct ownership and permissions:

    sudo chown -R www-data:www-data /var/www/html
    sudo find /var/www/html -type d -exec chmod 755 {} \;
    sudo find /var/www/html -type f -exec chmod 644 {} \;
    
  7. Create Nginx server block

    Create a new configuration file:

    sudo nano /etc/nginx/sites-available/wordpress
    

    Paste the following (replace example.com with your domain):

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

    Save and exit (Ctrl+X, Y, Enter). Enable the site:

    sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
    sudo rm /etc/nginx/sites-enabled/default
    

    Test the configuration and reload Nginx:

    sudo nginx -t
    sudo systemctl reload nginx
    
  8. Run WordPress installation

    Open your browser and go to http://example.com. You’ll see the WordPress language selection. Choose your language, then fill in the database details:

    • Database Name: wpdatabase
    • Username: wpuser
    • Password: strongpassword
    • Database Host: localhost
    • Table Prefix: wp_ (default)

    Click Submit, then Run the installation. Enter your site title, admin username, password, and email, then install.

Common Mistakes

  • Wrong PHP-FPM socket path: The fastcgi_pass line must match your PHP version. Use php -v to check, and adjust the socket path (e.g., php8.1-fpm.sock if you have PHP 8.1).
  • File permissions too loose: Setting files to 777 can cause security issues. Stick to 644 for files and 755 for directories, with owner www-data.
  • Not removing default Nginx site: If you don’t remove the default site, it may conflict and serve the wrong content.
  • Forgetting to enable the site: Creating a symlink in sites-enabled is essential; otherwise Nginx won’t use your config.

Tips

  • Use a strong database password: Avoid simple passwords; use a mix of letters, numbers, and symbols.
  • Enable HTTPS: After installation, install Certbot and obtain a free SSL certificate with sudo apt install certbot python3-certbot-nginx and sudo certbot --nginx.
  • Keep everything updated: Regularly run sudo apt update && sudo apt upgrade to patch security issues.
  • Backup regularly: Use plugins or cron jobs to back up your database and files.

Troubleshooting

Nginx not starting: Check syntax with sudo nginx -t. Look at error logs: sudo tail -f /var/log/nginx/error.log. Common issues include port 80 already in use (stop Apache with sudo systemctl stop apache2) or syntax errors in your config.

WordPress shows “Error establishing a database connection”: Verify database credentials in wp-config.php. Ensure MariaDB is running (sudo systemctl status mariadb). Check that the user has correct privileges.

PHP files not executing (download instead): This usually means the PHP location block is missing or incorrect. Ensure the location ~ \.php$ block is present and the socket path is correct. Reload Nginx after changes.

403 Forbidden: Check file permissions and ownership. The web server needs read access to files and execute on directories. Run sudo chown -R www-data:www-data /var/www/html.

White screen of death: Often due to PHP errors. Enable debugging by editing wp-config.php and adding define('WP_DEBUG', true);. Check the PHP error log at /var/log/php7.4-fpm.log.

Frequently Asked Questions

Can I use Apache instead of Nginx?

Yes, but this guide is specifically for Nginx. If you prefer Apache, you’d need to install apache2 and use .htaccess rules, which differ from Nginx’s configuration.

How do I change my WordPress site URL after installation?

Go to Settings > General in the WordPress admin dashboard and update the WordPress Address and Site Address fields. Alternatively, edit wp-config.php with define('WP_HOME','http://example.com'); and define('WP_SITEURL','http://example.com');.

Why do I get a 404 on all pages except the homepage?

This is usually a permalink issue. Go to Settings > Permalinks and click “Save Changes” to flush rewrite rules. If that doesn’t work, ensure Nginx’s try_files directive is correct.

How do I enable HTTPS with a free SSL certificate?

Install Certbot and the Nginx plugin, then run sudo certbot --nginx. It will automatically modify your Nginx config and set up a cron job for renewal.

Can I install WordPress on a subdirectory like example.com/blog?

Yes, but you’d need to adjust the Nginx server block’s root and location directives accordingly. The process is similar but requires more careful path configuration.

What should I do if I forget my WordPress admin password?

Use the WordPress admin reset via email, or use phpMyAdmin to update the user_pass field with an MD5 hash of a new password. Alternatively, use WP-CLI: wp user update <user> --user_pass=newpassword.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →