How to install nginx in linux (follow the steps to install correctly)

1. Install epel first

sudo yum install yum-utils

2. After the installation is complete, check the installed epel package

yum install epel-release

3. Start installing nginx

yum install nginx

4. Check the nginx version

nginx -v

5. Set the boot to start automatically

systemctl enable nginx

6. Start nginx

systemctl start nginx

7. To check if Nginx is running, use the following command:

sudo systemctl status nginx

Or the page can directly access the IP

8. Some other usages:

sudo systemctl start nginx   # 启动 Nginx
sudo systemctl stop nginx    # 停止 Nginx
sudo systemctl restart nginx # 重启 Nginx
sudo systemctl reload nginx  # 重新加载 Nginx 配置文件

To see where Nginx is installed, you can use the following command:

which nginx

This command will output the full path of the Nginx executable, for example: /usr/sbin/nginx

Nginx binaries are usually located in the /usr/sbin directory, while configuration files and other files are usually located in the /etc/nginx directory.

If you wish to view Nginx's configuration file directory, run the following command:

nginx -t

This command will check Nginx's configuration file syntax and output the full path to the configuration file. For example:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

In the above example, the Nginx configuration file is located at /etc/nginx/nginx.conf .

9. If you want to map Nginx to port 8468 of the local machine and provide different pages on this port, you can add multiple location blocks in the Nginx configuration file .

Here is an example configuration with two location blocks that map requests to different pages:

server {
    listen 80;
    server_name your_domain.com;

    location / {
        proxy_pass http://127.0.0.1:8468;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    location /page1 {
        proxy_pass http://127.0.0.1:8468/page1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    location /page2 {
        proxy_pass http://127.0.0.1:8468/page2;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

In the example above, the first location block forwards all requests to local port 8468. The second and third location blocks forward the request to the /page1 and /page2 pages on the local port 8468, corresponding to different pages of your application.

After saving the configuration file, reload the Nginx configuration for the changes to take effect:

sudo systemctl reload nginx

Now, you can access the local port 8468 by visiting http://your_domain.com , or visit http://your_domain.com/page1 and http://your_domain.com/page2 to visit different page.

10. How to uninstall nginx

Following are the steps to uninstall Nginx on CentOS cloud server:

1. Stop the Nginx service and execute the following command in the terminal:

sudo systemctl stop nginx

2. To delete the Nginx package, execute the following command in the terminal:

sudo yum remove nginx

3. Delete Nginx configuration files and log files Execute the following command in the terminal:

sudo rm -rf /etc/nginx
sudo rm -rf /var/log/nginx

4. finish

After the above steps are completed, Nginx on the server will be completely removed. If you need to reinstall Nginx, you can perform the installation steps again

Guess you like

Origin blog.csdn.net/m0_63270506/article/details/129782230