How to change Ngnix config setup to another port 8080 in Linux.

Date posted:   28 /09/2018

In this article , we learn about how to change Ngnix port in Linux.

Nginx is an open source server having high traffic websites in internet today. Among  all the web services, Nginx web server can be act as a good web reverse proxy or as a POP and IMAP proxy server.

By default, Nginx HTTP server listens for incoming connection and binds on port 80, which represents the standard web port.In order to make Nginx  server to listen for incoming connections on another port such as 8080.

Importance of port 8080:

  • 8080 port is used to host an alternate web server and it act as a proxy and caching port.
  • It is a popular alternate port to 80 port. It used to convey HTTP  traffic.
  • Here , we have to change the listen port from 80 to 8080.

Step by step procedure to implement in our Linux Environment, are as follows:

If you are  already using Apache HTTP server as a default one , it automatically listens to a port 80 as default standard  web port.So that we have to launch a Nginx web server in a  another port 8080.

Step 1: Adding a Nginx repository.

# sudo yum install epel-release

Step 2: To install nginix repository, in our server.

# sudo yum install nginx

Step 3: To Start Nginx.

# sudo systemctl start ngnix
Failed to start nginix.service: unit not found.

Nginix setup will not get started as default  apache webserver is running on our server.So that we have to edit the nginx.config file.

Step 4: Modify the main configuration File :

  • In  RHEL and CentOS based distributions edit /etc/nginx/nginx.conf file.
  • In Ubuntu and Debian based system, we need to modify the /etc/nginx/sites-enabled/default file.
# nano /etc/nginx/sites-enabled/default 
# nano /etc/nginx/nginx.conf

To configure Nginx HTTP server to listen for incoming connections on port  8080. Search for the line that begins with Listen statement in server config file directory  and change the port from 80 to 8080,

Inside the # nano /etc/nginx/nginx.conf  file , the apache server will automatically listens to the 80 port as the default_server as shown in below figure as follows,

In to the # nano /etc/nginx/nginx.conf  file , the config file has to edit  listen  8080 port as the default_server as shown in below figure as follows,

Step 5:

After altering Listening nginx port, Restart the nginx web server to bind in our  Linux environment.

# Sudo systemctl restart nginx 

Step 6:

Verify the  network sockets table with netstat  and ss  command. Port 8080 should be displayed in your server local network table.

Step 7:

Add the rules required by SElinux for nginx to bind on the new port.

Security-Enhanced Linux (SELinux) is a Linux kernel security module that provides control security policies, including United States Department of Defense–style mandatory access controls (MAC).

SELinux has to be enabled (Enforcing) , if it is disabled.

This entry can contain one of three values:

Enforcing:

SELinux security policy is enforced.

Permissive:

SELinux logs warnings instead of enforcing the policy (i.e. the action is allowed to proceed).

Disabled:

No SELinux policy is loaded.

Note that this configures the global SELinux enforcement mode. It is still possible to have domains running in permissive mode and/or object managers running as disabled, permissive or enforcing, when the global mode is enforcing or permissive.

To alter the enforce, permissive and disable by using this config files.

Edit the config file /etc/selinux/config  file to change the  status to disabled  SElinux to enable(Enforcing) Mode.

# sudo nano /etc/selinux/config

Then, inside the /etc/selinux/config file change the status disabled to enable.

As you can see in the image above, the orange marked section says SELinux status is enabled. The green marked section says that the Current mode is enforcing. once it is changed, Reboot it using reboot command.

# reboot

Then check the status , Whether the alteration in SELinux from Disabled to Enabled from using sestatus command.

# sestatus

This command will show the status of the SELinux . Now it is in Enabled enforced mode.

Step 8: 

After the SELinux  is Enabled , Install a policycoreutils package.

# yum install policycoreutils

To  bind the SElinux for nginx in  new port 8080, thus install a semanage package.

# yum provides /usr/sbin/semanage

Once the Semanage package is installed , thus use this to add TCP to port with 8080. using this command and address it.

# semanage port -a -t http_port_t -p tcp 8080
# semanage port -m -t http_port_t -p tcp 8080

Step 9:

Finally restart the service. And check the status in the network table.

# systemctl restart nginx.service 
# netstat -tlpn| grep nginx
# ss -tlpn| grep nginx

Step 10:

ping the server .ip address of your  local machine with  port 8080.

http://sever.ip:8080

http://10.1.1.1:8080

 

We successfully configured nginx setup on a alternate port 8080.For more information related  to this update, leave the comment.

Leave a Reply