Home > Operation and Maintenance > Linux Operation and Maintenance > How to use Nginx server in Linux

How to use Nginx server in Linux

PHPz
Release: 2023-06-18 10:12:09
Original
4352 people have browsed it

Nginx is a high-performance web server and reverse proxy server software. Its emergence provides a more stable and efficient Web service solution under Linux systems. In this article, we will introduce how to use Nginx server in Linux.

1. Install Nginx

Installing Nginx in Linux is very simple, just execute the following command:

sudo apt-get update
sudo apt-get install nginx
Copy after login

2. Start Nginx

Installation Once done, we need to start Nginx. Execute the following command:

sudo systemctl start nginx
Copy after login

If everything is normal, then the Nginx server has been started successfully.

3. Configure Nginx

The Nginx configuration file is located in /etc/nginx/nginx.conf. We can edit this file to configure Nginx.

First, we need to configure the default page of Nginx. By default, the Nginx server displays a welcome page. We can replace it with our own page.

We create a new configuration file in the /etc/nginx/sites-available directory. In this file, we need to include the following content:

server {
    listen 80 default_server;

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        try_files $uri $uri/ =404;
    }
}
Copy after login

In this configuration file, we specify that the Nginx server listens to port 80, and sets the default html root directory and default index page. We also specified server_name with an underscore, which means this server will handle all requests.

We save this file as default, and then create a symbolic link to the /etc/nginx/sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/
Copy after login

Now, we need to restart the Nginx server to make the new configuration Take effect. Execute the following command:

sudo systemctl restart nginx
Copy after login

4. Manage Nginx

In Linux systems, we can use the systemctl command to manage services. For example, we can use the following commands to start, stop, and restart the Nginx service:

sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
Copy after login

We can also use the status command to view the status of the Nginx service:

sudo systemctl status nginx
Copy after login

If everything is normal, we can use the browser Access the IP address of the Nginx server to see the default page we set.

Summary

Nginx is a powerful web server and reverse proxy server software. It is very convenient to use Nginx in a Linux system. It only requires a few simple steps to complete the installation, configuration and management. Through the introduction of this article, I believe readers can easily use Nginx in Linux systems.

The above is the detailed content of How to use Nginx server in Linux. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template