How to Install Nginx on CentOS: Step-by-Step Guide
To install
nginx on CentOS, first update your system with sudo yum update. Then install nginx using sudo yum install nginx and start it with sudo systemctl start nginx. Enable it to run on boot with sudo systemctl enable nginx.Syntax
Here are the main commands to install and manage nginx on CentOS:
sudo yum update: Updates system packages.sudo yum install nginx: Installs nginx web server.sudo systemctl start nginx: Starts the nginx service.sudo systemctl enable nginx: Enables nginx to start on boot.sudo systemctl status nginx: Checks nginx service status.
bash
sudo yum update sudo yum install nginx sudo systemctl start nginx sudo systemctl enable nginx sudo systemctl status nginx
Example
This example shows the full process to install and start nginx on CentOS. It updates the system, installs nginx, starts the service, enables it on boot, and checks its status.
bash
sudo yum update -y sudo yum install nginx -y sudo systemctl start nginx sudo systemctl enable nginx sudo systemctl status nginx
Output
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2024-06-20 12:00:00 UTC; 1min ago
Main PID: 1234 (nginx)
CGroup: /system.slice/nginx.service
├─1234 nginx: master process /usr/sbin/nginx
└─1235 nginx: worker process
Common Pitfalls
Common mistakes when installing nginx on CentOS include:
- Not running
yum updatefirst, which can cause package conflicts. - Forgetting to start the nginx service after installation.
- Not enabling nginx to start on boot, so it stops after reboot.
- Firewall blocking HTTP traffic; remember to allow port 80 if needed.
To allow HTTP traffic through the firewall, run:
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --reload
bash
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --reload
Output
success
success
Quick Reference
| Command | Description |
|---|---|
| sudo yum update -y | Update all system packages |
| sudo yum install nginx -y | Install nginx web server |
| sudo systemctl start nginx | Start nginx service |
| sudo systemctl enable nginx | Enable nginx to start on boot |
| sudo systemctl status nginx | Check nginx service status |
| sudo firewall-cmd --permanent --add-service=http | Allow HTTP traffic through firewall |
| sudo firewall-cmd --reload | Reload firewall to apply changes |
Key Takeaways
Always update your CentOS system before installing nginx to avoid conflicts.
Use yum to install nginx and systemctl to start and enable the service.
Enable nginx to start automatically on system boot with systemctl enable.
Check nginx status to confirm it is running properly after installation.
Remember to allow HTTP traffic through the firewall for nginx to serve web pages.