Skip to content

Instantly share code, notes, and snippets.

@zhangzhhz
Last active June 23, 2020 17:24
Show Gist options
  • Save zhangzhhz/b1462a2787991e10296bfc24703bf2e1 to your computer and use it in GitHub Desktop.
Save zhangzhhz/b1462a2787991e10296bfc24703bf2e1 to your computer and use it in GitHub Desktop.
Install WordPress v5.3.2 and self-host it over https using Nginx.

Install WordPress v5.3.2 and self-host it over https using Nginx.

Prerequisites

  1. MariaDB 10.4.13 (Reference How To Install MariaDB 10.4 on Ubuntu 18.04 / Ubuntu 16.04)

  2. php 7.4.3 (Reference How To Install PHP 7.4 on Ubuntu 20.04/18.04/16.04)

    php installs Apache2 too. Disable it and remove it if you want to use Nginx.

    The following extensions are needed:

    1. php7.4-fpm (needed for Nginx to render php files)
    2. php7.4-mysql (needed for connecting to MariaDB)

    To test php, create a file info.php with the following content and view it in web browser.

    <?php
    phpinfo();
    

Installation

Reference: How to install WordPress

  1. Download WordPress pakcage and extract it.

  2. Creating database for WordPress using mysql client

    $ mysql -u adminusername -p
    Enter password:
    Welcome to the MySQL monitor. Commands end with ; or \g.
    Your MySQL connection id is 5340 to server version: 3.23.54
    
    Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
    
    mysql> CREATE DATABASE wordpress;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> GRANT ALL PRIVILEGES ON wordpress.* TO "wordpressusername"@"localhost"
    -> IDENTIFIED BY "password";
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> FLUSH PRIVILEGES;
    Query OK, 0 rows affected (0.01 sec) 
    
    mysql> EXIT
    Bye
    $ 
    
  3. Set up wp-config.php

    I let WordPress do this in step 5 below.

  4. Decide where WordPress site shows in your domain

    I used blog.example.com

  5. Run install script

    Run http://blog.example.com/wp-admin/install.php and follow the prompts.

Common Installation Problems

Run WordPress over https behind Nginx

  1. Set up a Nginx site to serve WordPress at a cutom port, for example, 8088.

    server {
      listen 127.0.0.1:8088;
      listen [::1]:8088;
    
      root /root_directory
    
      # Add index.php to the list if you are using PHP
      index index.php index.html index.htm;
    
      server_name blog.example.com;
    
      charset UTF-8;
    
      location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
      }
    
            location ~ \.php$ {
                    include snippets/fastcgi-php.conf;
            #
            #       # With php-fpm (or other unix sockets):
                    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
            #       # With php-cgi (or other tcp sockets):
            #       fastcgi_pass 127.0.0.1:9000;
            }
    }
    
  2. Set up a Nginx site to proxy to http://127.0.0.1:8088.

    Note: This listens at port 80 without TLS connection because Nginx is also behind another server which is served at port 443 with TLS in my case here. Change to 443 and add SSL related configurations if the blog site is served at port 443 directly.
    server {
        listen         80;
        listen         [::]:80;
        server_name    blog.example.com;
        return         301 https://$host$request_uri;
    }
    
    server {
      listen 127.0.0.1:80;
      listen [::1]:80;
    
      root /dev/null;
    
      # Add index.php to the list if you are using PHP
      index index.php index.html index.htm;
    
      server_name blog.example.com;
    
      charset UTF-8;
    
      location / {
                proxy_pass http://127.0.0.1:8088;
                # set any custom header for WordPress to determine if it shoud serve contents over https
                proxy_set_header X-HTTPS 1;
                # proxy_set_header X-Forwarded-Proto https;
                # proxy_set_header X-Forwarded-Port 443;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Host $http_host;
                proxy_redirect off;
                proxy_buffering off;
                # proxy_set_header Upgrade $http_upgrade;
                # proxy_set_header Connection $connection_upgrade;
                proxy_read_timeout 86400;
      }
    }
    
  3. Update wp-config.php to check for the custom header set in Nginx

    ...
    
    /* That's all, stop editing! Happy publishing. */
    
    /** Absolute path to the WordPress directory. */
    if ( ! defined( 'ABSPATH' ) ) {
      define( 'ABSPATH', dirname( __FILE__ ) . '/' );
    }
    
    // Check for custom header for serving admin panel through https.
    //if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false) {
    //    $_SERVER['HTTPS']='on';
    //}
    if ($_SERVER['HTTP_X_HTTPS'] == 1) {
        $_SERVER['HTTPS']='on';
    }
    // The above must be before calling wp-settings.php, 
    // otherwise admin page is not accessible.
    
    /** Sets up WordPress vars and included files. */
    require_once( ABSPATH . 'wp-settings.php' );
    
    /** Force all logins and all admin sessions to happen over SSL */
    //define('FORCE_SSL_ADMIN', true);
    // The above, if not commented out, will give error: Constant FORCE_SSL_ADMIN already defined in ...
    // wp-settings.php calls wp_ssl_constants() which defines FORCE_SSL_ADMIN 
    // based onthe protocol in `siteurl`.
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment