In this tutorial, we will show the manual as well as bash script method to install Nginx on Ubuntu 22.04 LTS server. Nginx which is one the most powerful web server widely use in the IT industry. It can easily handle the thousands of simultaneous connections with the small memory usage.
Usage of Nginx:
- Web Server
- Caching Server
- Reverse Proxy,
- Load Balancer
- Mail Proxy
In many CDN Network the backend servers are mostly Nginx Server. So by this only you can imagine how dynamic and powerful it is.
Let’s start the installation for which you might be waiting for. We expect you have already installed the Ubuntu 22.04 LTS Server in your machines.
If you are DevOps aspirant then we will strongly suggest to automate each single bit of your task. For this you can use scripting like bash, python etc and tools like Ansible, Chef, Puppet etc.
We have also created a simple bash script for installing the Nginx its Github link is at the end of this post.
Table of Contents
- Install Nginx On Ubuntu 22.04 LTS (Manual Method)
- How To Manage Nginx Service
- Install Nginx On Ubuntu 22.04 LTS By Using Bash Script
Install Nginx On Ubuntu 22.04 LTS (Manual Method)
We have checked this method in Ubuntu 22.04 LTS, this should work with other Ubuntu versions also. You can share your practical result in comment sections and help others to know.
- Open the terminal and run the following commands.
- Install the dependencies
sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring -y
- Add the signing key
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \ | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
- Add the apt repo file
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \ http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \| sudo tee /etc/apt/sources.list.d/nginx.list
- Pin the preference
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \ | sudo tee /etc/apt/preferences.d/99nginx
- Install the Nginx after updating the apt repo
sudo apt update -y sudo apt install nginx -y
- Now restart the nginx service
sudo systemctl restart nginx
- Now open the Web Browser and type the URL or IP Address of the Nginx Server. Check you Linux Machine ip address by typing command
ip a. In web browser, it will open the Nginx default page.
How To Manage Nginx Service
Once you have installed the Nginx, you will also be interested to know how to stop/start/restart/enable/disable the Nginx Service. So here are the commands.
Stop Nginx Service
Follow the given below command to stop the nginx service.
sudo systemctl stop nginx
Start Nginx Service
To start the nginx service, here is the given below command.
sudo systemctl start nginx
Restart Nginx Service
To restart the nginx service use the following command.
sudo systemctl restart nginx
Enable Nginx Service
When you want the nginx service should be in running state every time after server get restarted/started, you have to ‘enable’ the nginx service with systemctl command.
sudo systemctl enable nginx
Disable Nginx Service
When you want to the nginx service should not start on every restart/start of the server then we should ‘disable’ the nginx service with systemctl command.
sudo systemctl disable nginx
Install Nginx On Ubuntu 22.04 LTS By Using Bash Script
The bash script we have kept in our Github repo from where you can pull and use it. You can find the Github Repo link here.
Download the bash script and give execute permission to the script.
chmod 750 install_nginx_ubuntu.sh
Now run the script
sh install_nginx_ubuntu.sh . It will install the Nginx.
Now open the web browser and give ip address or FQDN of the Server in address field. It will open the default page.