Installing Nginx on a Linux server is a straightforward process, and this article will guide you through the steps with command examples.
Before you begin the installation, make sure your Linux server is up to date. Use the following commands to update the package list and upgrade installed packages:
sudo apt update
sudo apt upgrade
Step 1: Install Nginx
Use the package manager of your Linux distribution to install Nginx. For example, on Ubuntu/Debian-based systems, use:
sudo apt install nginx
On CentOS/RHEL-based systems, use:
sudo yum install nginx
Step 2: Start Nginx
After the installation is complete, start the Nginx service with the following command:
sudo systemctl start nginx
To ensure that Nginx starts automatically at boot, enable it with:
sudo systemctl enable nginx
Step 3: Verify Installation
Open a web browser and navigate to your server’s IP address or domain. You should see the default Nginx welcome page, confirming a successful installation.
Step 4: Configuring Nginx
The main configuration file for Nginx is usually located at /etc/nginx/nginx.conf
. You can edit this file using a text editor, such as nano
or vim
.
sudo nano /etc/nginx/nginx.conf
Make sure to make backups before making changes.
Step 5: Managing Sites
Nginx configurations for individual sites are stored in the /etc/nginx/sites-available/
directory. To create a new site, create a configuration file in this directory and then create a symbolic link to sites-enabled
:
sudo nano /etc/nginx/sites-available/mywebsite
sudo ln -s /etc/nginx/sites-available/mywebsite /etc/nginx/sites-enabled/
Don’t forget to test the configuration before restarting Nginx:
sudo nginx -t
If the test is successful, reload Nginx to apply the changes:
sudo systemctl reload nginx
Conclusion
You have successfully installed and configured Nginx on your Linux server. This powerful web server is now ready to serve your websites and applications efficiently. Feel free to explore advanced configurations and optimizations based on your specific requirements.