arrow_back 返回

在 CentOS 上安装并使用 Nginx

最后更新于

前言

安装

下载源码

解压

tar -zxvf nginx-x-xx.tar.gz

安装

安装依赖

yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

安装nginx

cd nginx
./configure
make && make install

启动

启动、停止、重启

要执行这些操作,最根本的是找到nginx的核心(仅是编译安装的情况下)。 若是直接包安装,使用systemctl stop/start/restart nginx就可以了。

nginx 核心在哪里

三指令

启动,配置文件可以不指定,要指定就是nginx.conf

nginx -c 你的配置文件

停止

nginx -s stop
nginx -s quit

重启

nginx -s reload

ATTENTION: 如果说找不到指令nginx

多个网站不同php版本

在配置多个网站之前,我们先来看看一个网站的配置是怎样的吧

nginx的配置

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       81;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        

        location / {
            root   /home/nginx_www/typecho;
            index  index.html index.htm index.php;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~ .*\.php(\/.*)*$ {
            root           /home/nginx_www/typecho;
            fastcgi_pass   127.0.0.1:9002;
            fastcgi_index  index.php;
            fastcgi_split_path_info ^(.+?.php)(/.*)$;
            set $path_info "";
            set $real_script_name $fastcgi_script_name;
            if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)${body}quot;) {
                set $real_script_name $1;
                set $path_info $2;
            }
            fastcgi_param  SCRIPT_NAME $real_script_name;
            fastcgi_param  PATH_INFO $path_info;
            fastcgi_param  SCRIPT_FILENAME /home/nginx_www/typecho$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

好了,没有必要去仔细地解析这个配置,让我来告诉你就好

配置不同php版本

我们的目的就在于「多个网站」「不同php」而已

可以发现,在http下的server其实就是一个网站的配置

server {
    listen       8000;
    server_name  somename;
    
    location / {
        root   /home/www/;
        index  index.html index.htm index.php;
    }
    
    location ~ \.php$ {
            root           /home/www;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME   /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }
}
字段 说明
server_name 一个名称,你喜欢就好
listen 网站的监听端口;使用https协议加上ssl: 8000 ssl
location 路由设置
root 每个路由对应的工作文件夹
index 地址栏进入后加载的文件
fastcgi_pass php的fastcgi的地址
fastcgi_index 被访问时fastcgi加载的文件

配置多个网站

server {
    listen 80; # 监听端口 可以这样:listen 443 ssl; 来使用https协议
    server_name 一个有意义的名字;

    location / {  # 指定网站访问根目录的时候的事情
        root 网站根目录的绝对路径;
        index 初始化脚本; # 通常是index.html index.htm index.php
    
    }
}