Nginx教程

Posted by Cooper on December 30, 2022

Introduction to NGINX

  • It can handle a higher number of concurrent requests.
  • It has faster static content delivery with low resource usage.

Install Nginx

1
vi /etc/yum.repos.d/nginx.repo

写入:

1
2
3
4
5
[nginx] 
name = nginx repo 
baseurl = https://nginx.org/packages/mainline/centos/7/$basearch/ 
gpgcheck = 0 
enabled = 1

wq保存退出

用``yum安装nginx`

1
yum install -y nginx

配置default.conf 文件

1
vim /etc/nginx/conf.d/default.conf

写入:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
server {
    listen       80;
    root   /usr/share/nginx/html;
    server_name  localhost;
    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
    #
    location / {
          index index.php index.html index.htm;
    }
    #error_page  404              /404.html;
    #redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
      root   /usr/share/nginx/html;
    }
    #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ .php$ {
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
      include        fastcgi_params;
    }
}

wq保存退出

启动nginx

1
systemctl start nginx

如果报错,可能是80端口已被占用,在上面文件中改其他端口,如81

查看端口:

1
lsof -i tcp:80

停止nginx

1
systemctl stop nginx

开机启动

1
systemctl enable nginx 

本地浏览器测试

1
http://服务器IP

默认端口为:80,如果改了81则为:

1
http://服务器IP:81	

Screenshot 2022-12-30 at 11.01.46

Configure Nginx

参考材料

Nginx极简教程