How to change WordPress port 80 to 8080

Overview

WordPress automatically uses the hostname or IP address used to access it during the installation process, as well as the port number, if not port 80. In this tutorial, you will learn how to change your WordPress port from 80 to 8080, or any other port number you desire.

Web Server Port Change

The first step would be to change the port number your web server is hosting WordPress on.

WordPress Nginx Port 8080

For those using NGINX to serve your WordPress site, the following instructions should be followed.

The NGINX server context use listen to set the TCP port number of a virtual host. To move WordPress from port 80 to 8080, the NGINX listen property needs to be set to 8080.

Open your NGINX configuration file for your WordPress site and change the listen property to 8080.

server {
    listen 8080;
    servername 127.0.0.1;
    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;
    location / {
        tryfiles $uri $uri/ =404;
    }

    errorpage 404 /404.html;
    errorpage 500 502 503 504 /50x.html;

    location = /50x.html {
        root /var/www/html;
    }

    location ~ .php$ {
        tryfiles $uri =404;
        fastcgipass unix:/var/run/php5-fpm.sock;
        fastcgiindex index.php;
        fastcgiparam SCRIPTFILENAME $documentroot$fastcgiscriptname;
        include fastcgi_params;
    }
}

Saves your changes and reload NGINX.

sudo systemctl restart nginx

Configuring WordPress to use Port 8080

During the WordPress installation process WordPress captures the hostname and port used to access the newly installed CMS. This information is stored in the backend MySQL database, and is used to force redirect requests to the correct hostname and port.

In order to bypass the saved hostname and port, you can define the WPSITEURL and WPHOME fields from the wp-config.php file.

Open your wp-config.php file and add the following lines to it.

define(‘WPSITEURL’,’http://localhost:8080/’);
define('WPHOME’,’http://localhost:8080/’);

Save your changes and all new requests will ignore the values of the two fields in your database, and instead use the values defined in your WordPress configuration.

The change is considered temporary, as it does not update your database. To update your database to permantently change your WordPress site’s port number, follow the instuctions below.

Update WordPress Port in MySQL

Updating WordPress fields in MySQL in considered a permanent change.

The WPSITEURL and WPHOME properties are stored as options in the MySQL database. In order to update their values you must run MySQL SQL commands against them.

To update the WPSITEURL WordPress option in MySQL, run the following command.

update wp_options set option_value=’http://localhost:8080’ where option_name='siteurl’;

And to update the WPHOME WordPress option in MySQL, run the following command.

update wp_options set option_value=’http://localhost:8080’ where option_name='home’;