Welcome to the tutorial on getting started with Nginx! Nginx is a high-performance web server that is known for its stability, security, and efficiency. In this guide, we will cover the basics of installing, configuring, and using Nginx.
Prerequisites
Before you begin, make sure you have the following prerequisites:
- A Linux server (Ubuntu, CentOS, etc.)
- Root access to the server
- Basic knowledge of Linux commands
Installation
To install Nginx, you can use the package manager of your Linux distribution. Here's how you can do it on Ubuntu:
sudo apt update
sudo apt install nginx
On CentOS, you can use:
sudo yum install nginx
After the installation is complete, you can check if Nginx is running with:
sudo systemctl status nginx
Basic Configuration
Nginx uses a configuration file located at /etc/nginx/nginx.conf
. You can edit this file to customize the behavior of your web server.
Here's a basic configuration example:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
}
Testing the Configuration
After making changes to the configuration file, you need to test it to ensure there are no syntax errors. You can do this with the following command:
sudo nginx -t
If the test is successful, you can reload Nginx to apply the changes:
sudo systemctl reload nginx
Accessing Your Website
Now that Nginx is installed and configured, you can access your website by navigating to http://localhost
in your web browser.
For more detailed information on Nginx configuration and advanced features, check out our Nginx Configuration Guide.