In this article, we are going to see how to change default Nginx port in Linux distributions. Nginx is a fast, open source multi-functional web server which was written by Igor Sysoev and released on October 4th, 2003. Apart from being a web server, It also acts as a reverse proxy and a load balancer allowing it to handle large numbers of concurrent HTTP connections and is mostly preferred to Apache in hosting enterprise websites which handle high traffic. It is used in high traffic websites such as Netflix and Dropbox to deliver their content in a quick, reliable and secure manner.
By default, Nginx listens to incoming HTTP connections on port 80 and port 443 for secure connections.
Installation of Nginx on CentOS and RHEL 7
Install epel release
yum install epel-release
Sample Output
Loading mirror speeds from cached hostfile * base: mirrors.linode.com * extras: mirrors.linode.com * updates: mirrors.linode.com Resolving Dependencies --> Running transaction check ---> Package epel-release.noarch 0:7-9 will be installed --> Finished Dependency Resolution Dependencies Resolved ======================================================================================================================================================================== Package Arch Version Repository Size ======================================================================================================================================================================== Installing: epel-release noarch 7-9 extras 14 k Transaction Summary ======================================================================================================================================================================== Install 1 Package Total download size: 14 k Installed size: 24 k Is this ok [y/d/N]:
Install nginx
yum install nginx
Sample Output
Dependencies Resolved ======================================================================================================================================================================== Package Arch Version Repository Size ======================================================================================================================================================================== Installing: nginx x86_64 1:1.12.2-1.el7 epel 529 k Installing for dependencies: fontconfig x86_64 2.10.95-11.el7 base 229 k fontpackages-filesystem noarch 1.44-8.el7 base 9.9 k gd x86_64 2.0.35-26.el7 base 146 k gperftools-libs x86_64 2.4-8.el7 base 272 k libX11 x86_64 1.6.5-1.el7 base 606 k libX11-common noarch 1.6.5-1.el7 base 164 k libXau x86_64 1.0.8-2.1.el7 base 29 k libXpm x86_64 3.5.12-1.el7 base 55 k libjpeg-turbo x86_64 1.2.90-5.el7 base 134 k libpng x86_64 2:1.5.13-7.el7_2 base 213 k libunwind x86_64 2:1.2-2.el7 base 57 k libxcb x86_64 1.12-1.el7 base 211 k libxslt x86_64 1.1.28-5.el7 base 242 k lyx-fonts noarch 2.2.3-1.el7 epel 159 k nginx-all-modules noarch 1:1.12.2-1.el7 epel 16 k nginx-filesystem noarch 1:1.12.2-1.el7 epel 17 k nginx-mod-http-geoip x86_64 1:1.12.2-1.el7 epel 23 k nginx-mod-http-image-filter x86_64 1:1.12.2-1.el7 epel 26 k nginx-mod-http-perl x86_64 1:1.12.2-1.el7 epel 35 k nginx-mod-http-xslt-filter x86_64 1:1.12.2-1.el7 epel 25 k nginx-mod-mail x86_64 1:1.12.2-1.el7 epel 53 k nginx-mod-stream x86_64 1:1.12.2-1.el7 epel 76 k Transaction Summary ======================================================================================================================================================================== Install 1 Package (+22 Dependent packages) Total download size: 3.3 M Installed size: 9.8 M Is this ok [y/d/N]: Install complete Installed: nginx.x86_64 1:1.12.2-1.el7 Dependency Installed: fontconfig.x86_64 0:2.10.95-11.el7 fontpackages-filesystem.noarch 0:1.44-8.el7 gd.x86_64 0:2.0.35-26.el7 gperftools-libs.x86_64 0:2.4-8.el7 libX11.x86_64 0:1.6.5-1.el7 libX11-common.noarch 0:1.6.5-1.el7 libXau.x86_64 0:1.0.8-2.1.el7 libXpm.x86_64 0:3.5.12-1.el7 libjpeg-turbo.x86_64 0:1.2.90-5.el7 libpng.x86_64 2:1.5.13-7.el7_2 libunwind.x86_64 2:1.2-2.el7 libxcb.x86_64 0:1.12-1.el7 libxslt.x86_64 0:1.1.28-5.el7 lyx-fonts.noarch 0:2.2.3-1.el7 nginx-all-modules.noarch 1:1.12.2-1.el7 nginx-filesystem.noarch 1:1.12.2-1.el7 nginx-mod-http-geoip.x86_64 1:1.12.2-1.el7 nginx-mod-http-image-filter.x86_64 1:1.12.2-1.el7 nginx-mod-http-perl.x86_64 1:1.12.2-1.el7 nginx-mod-http-xslt-filter.x86_64 1:1.12.2-1.el7 nginx-mod-mail.x86_64 1:1.12.2-1.el7 nginx-mod-stream.x86_64 1:1.12.2-1.el7 Complete!
Start Nginx
systemctl start nginx
Enable Nginx to start on boot
systemctl enable nginx
If you are behind a firewall, run the commands below to open ports 80 (http) and 443 (https)
sudo firewall-cmd --permanent --zone=public --add-port=80/tcp sudo firewall-cmd --permanent --zone=public --add-port=443/tcp sudo firewall-cmd --reload
Installation of Nginx on Ubuntu 16.04 and Debian 9
Update the system first
apt-get update
Install nginx
apt-get install nginx
Start nginx
systemctl start nginx
To verify everything went well, you should see nginx’s default page in a browser when you visit the IP of the server. In my case, the server’s IP is 45.33.10.146
Configuration of Nginx to listen on port 3200 (RHEL & CentOS 7)
To make the Nginx HTTP server listen to web connections from nonstandard ports, we must edit its main configuration file and make changes to the main configuration file.
For Centos & RHEL the configuration file is as shown below
/etc/nginx/nginx.conf
Locate the line that starts with listen statement in server directive and change the port from 80 to 3400,
Restart nginx
systemctl restart nginx.service
Run
netstat -pnltu
to verify the server is listening on port 3400
Next, install policycoreutils-python package
yum install policycoreutils-python
Configure SELinux
vim /etc/sysconfig/selinux
Set SELinux to permissive
Reboot nginx
Configuration of Nginx to listen on port 3200 (Ubuntu 16.04 & Debian 9)
The default config file is
/etc/nginx/sites-enabled/default
As before, locate the line that starts with listen and change the default port from 80 to 3400.
Restart Nginx and verify the server is listening on port 3400 using the netstat command.
Set SELinux to permissive and reboot.
Open your web browser and visit your server’s ip address followed by :3400
http://ip-address:3400
Wrapping up
Thank you for your time. Your comments and suggestions are welcome. Feel free to ask any questions.