Table of Contents
Install Nginx Webserver on Linux Ubuntu Server 20.04 LTS (Changing from Apache2 already installed)
It would be necessary to switch from Apache to Nginx if, for example, you want to install NextCloud and OnlyOffice Document Server on the same Ubuntu server and integrate them to work together in order to provide document creation, editing, collaboration, and cloud storage. A prerequisite for OnlyOffice Document Server is that it must run on Nginx webserver. NextCloud can run on either Apache or Nginx, but Nginx is the common denominator to run both NextCloud and Only/Office Document Server on the same box.
sudo apt update sudo apt upgrade
Stop apache2 if that webserver is also installed and running. It may also be necessary to change the http Port bind to Apache2 before starting Nginx after it is installed.
sudo systemctl stop apache2.service
See below if you need to change http port 80 that is bind to Apache2.
Installing Nginx Server
sudo apt install nginx
After the installation is completed, start the Nginx service and enable it to launch every time at system boot. Check the status of Nginx.
sudo systemctl start nginx sudo systemctl enable nginx sudo systemctl status nginx
NGinx Could not Bind to http port 80 because Apache is already Bind to that Port
Jun 10 02:33:16 xsvr nginx[77940]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Jun 10 02:33:16 xsvr nginx[77940]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Jun 10 02:33:17 xsvr nginx[77940]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Jun 10 02:33:17 xsvr nginx[77940]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Jun 10 02:33:17 xsvr nginx[77940]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Jun 10 02:33:17 xsvr nginx[77940]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Jun 10 02:33:18 xsvr nginx[77940]: nginx: [emerg] still could not bind() Jun 10 02:33:18 xsvr systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE
Jun 10 02:33:18 xsvr systemd[1]: nginx.service: Failed with result 'exit-code'.
Jun 10 02:33:18 xsvr systemd[1]: Failed to start A high performance web server and a reverse proxy server.
Change Apache2 Default Port to Custom Port on Ubuntu / Debian
Change Apache port on Debian/Ubuntu. Edit /etc/apache2/ports.conf file, $ sudo vi /etc/apache2/ports.conf. Find the following line: Listen 80. ..
sudo vim /etc/apache2/ports.conf
Find the following line:
Listen 80
Change it to a random port of you choice, for example 8090.
Listen 8090
This entry make the server to accept connections on port 8090 on all interfaces. To make the server accept connections on port 8090 for a specific interface, just include the corresponding network interface’s IP address as shown below.
Listen 192.168.1.101:8090
This will be helpful if your server has multiple IP addresses or network interfaces.
Save and close the file.
If the ports.conf file also has the following, may need to change ssl port 443 to port 444 or something else.
<IfModule ssl_module> Listen 443 </IfModule> <IfModule mod_gnutls.c> Listen 443 </IfModule>
Additionally, in Ubuntu and Debian, you also have to change the port number in /etc/apache2/sites-enabled/000-default.conf file too.
$ sudo vi /etc/apache2/sites-enabled/000-default.conf
Find the following line and change the port number.
<VirtualHost *:8090>
Save and close the file.
Then, restart Apache service to take effect the changes.
$ sudo systemctl restart apache2
Now let us verify the port settings:
$ sudo netstat -tulpn | grep :8090
Sample Output:
tcp6 0 0:::880 :::* LISTEN 4066/apache2
Return (above) to Start Nginx, Enable it to start at start-up, and check its status
sudo systemctl start nginx sudo systemctl enable nginx sudo systemctl status nginx
Open the UFW firewall for Nginx port 80 and 443
If you have enabled the uncomplicated firewall (ufw) on Ubuntu, then you will need to type the following ufw command to open TCP port number 80 and 443 for everyone. If the firewall is not enabled because you are behind a NAT router (or whatever reason), then skip these steps to open ports in the firewall.
sudo ufw allow 80/tcp comment 'accept HTTP Nginx' sudo ufw allow 443/tcp comment 'accept HTTPS/TLS Nginx connections'
Verify port status it:
sudo ufw status
Configure Nginx to Use the PHP Processor
Configure PhpMyAdmin within Nginx Server default sites-available virtual host file
Now go to the '/etc/nginx' configuration directory, and edit the default virtual host file.
cd /etc/nginx/sites-available/ sudo cp default default-original sudo vim default
Paste the following Nginx configuration for PHPMyAdmin inside the 'server {…}' bracket.
location /phpmyadmin { root /usr/share/; index index.php; try_files $uri $uri/ =404; location ~ ^/phpmyadmin/(doc|sql|setup)/ { deny all; } location ~ /phpmyadmin/(.+\.php)$ { fastcgi_pass unix:/run/php/php7.4-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; include snippets/fastcgi-php.conf; } }
Save and exit.
Test the nginx configuration and make sure there is no error, then restart the nginx service.
sudo nginx -t sudo systemctl reload nginx
And we've added the Nginx configuration for our phpMyAdmin installation, it's accessible through the path URL as 'http://server-ip/phpmyadmin'.