温馨提示×

如何在CentOS LNMP中添加新站点

小樊
89
2025-02-09 09:19:34
栏目: 智能运维

在 CentOS LNMP(Linux, Nginx, MySQL, PHP)环境中添加新站点,您需要按照以下步骤操作:

  1. 创建一个新的站点配置文件: 在 /etc/nginx/conf.d/ 目录下创建一个新的配置文件,例如 yourdomain.com.conf。使用文本编辑器打开文件,如 vinano

    sudo vi /etc/nginx/conf.d/yourdomain.com.conf
    
  2. 编辑站点配置文件: 在新创建的配置文件中,输入以下内容并根据您的实际情况进行修改:

    server {
        listen       80;
        server_name  yourdomain.com www.yourdomain.com;
    
        root   /var/www/yourdomain.com;
        index  index.php index.html index.htm;
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        error_page  404              /404.html;
        location = /404.html {
            internal;
        }
    
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    
        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;
        }
    
        location ~ /\.ht {
            deny  all;
        }
    }
    

    请将 yourdomain.com 替换为您的实际域名,将 /var/www/yourdomain.com 替换为您希望站点文件存放的目录。

  3. 创建站点根目录: 使用 mkdir 命令创建站点根目录:

    sudo mkdir -p /var/www/yourdomain.com
    
  4. 设置站点权限: 更改站点根目录的所有者和权限,以便 Nginx 可以访问文件:

    sudo chown -R nginx:nginx /var/www/yourdomain.com
    sudo chmod -R 755 /var/www/yourdomain.com
    
  5. 创建一个简单的 HTML 页面: 在站点根目录中创建一个名为 index.html 的文件,用于测试新站点是否正常工作:

    sudo vi /var/www/yourdomain.com/index.html
    

    输入以下内容:

    <!DOCTYPE html>
    <html>
    <head>
        <title>Welcome to Your Domain</title>
    </head>
    <body>
        <h1>Hello, this is your new site!</h1>
    </body>
    </html>
    
  6. 重启 Nginx 服务: 为了使新的站点配置生效,需要重启 Nginx 服务:

    sudo systemctl restart nginx
    

现在,您应该可以通过访问 http://yourdomain.com 在浏览器中查看新添加的 CentOS LNMP 站点。

0