我如何在Xampp中设置nginx网站

时间:2013-11-06 20:18:13

标签: php yii nginx

我正在尝试从XAMPP迁移到Nginx网络服务器。我曾经有多个网站与Xampp一起运行,每个网络文件夹都位于xampp / htdocs文件夹下。例如,我将访问网站(yii网站)作为[代码] http:// myserver / site1 [代码]和[代码] http:// myserver / site2 [代码],他们将从site1和htdocs下的site2文件夹。但是我在nginx中设置它时遇到了麻烦。我已将根文件夹的默认配置设置为/ etc / share / nginx / www(并且site1和site2文件夹位于www下),当我使用nginx以相同方式访问时,网页会显示错误消息“未指定输入文件”。我知道在nginx中设置了多个站点,它们具有不同的域名和不同的根文件夹,但是是否可以使用类似xampp的配置?因为我在我的本地网络上测试它,我不想为此设置多个域名。

3 个答案:

答案 0 :(得分:1)

最新nginx版本的配置非常简单。首先,您应该修改nginx/conf/nginx.conf,并确保在include vhosts/*.conf;部分中有http {之类的内容。这将使nginx在vhosts下查找额外的配置。

同样,最好在http {中声明以下内容,不要为每个单独的配置重复此操作:

gzip  on;
charset utf-8;
index index.php index.htm index.html;

然后在nginx/conf/vhosts/mydomain.com.conf

server {
    listen       80;
    server_name  mydomain.com;
    root    /var/www/mydomain.com/www;

    location / {
        try_files   $uri $uri/ /index.php?$args; # Redirect everything that isn't real file to index.php including arguments.
    }

    location ~ \.php$ {
        include fastcgi.conf; # that's if you have one of latest versions of nginx. If not, see below
        fastcgi_pass   127.0.0.1:9000; # or pass through socket if it's how you've configured php-fpm
    }

    location ~ /\.(ht|svn) {
        deny all;
    }
}

如果您没有fastcgi.conf,请执行以下操作:

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

现在唯一剩下的就是将mydomain.com添加到您的主机文件中,以便在本地识别。

答案 1 :(得分:1)

这很简单。

删除你的数据库,压缩它们,再次导入它们或者使用 mysql -u root -p BASENAME

然后只需使用 chmod 0777 -R /the/path 并使用 chown www-data:www-data -R /the/path 移动您的默认目录,如 /var/www/html 并设置权限为 0777 或更少然后重启你的系统。

答案 2 :(得分:0)

嗯,经过努力,我终于找到了理想的配置。我知道这不是推荐的设置,但这是给我的问题(nginx有多个站点作为子目录),并且必须找到它的解决方案。

location /Site1/ {
            root /usr/share/nginx/www/Site1;
           try_files $uri $uri/ /index.php?$query_string;
    }

    # the images need a seperate entry as we dont want to concatenate that with index.php      
    location ~ /Site1/.+\.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
            root /usr/share/nginx/www/Site1;
    }
    # pass the PHP scripts to FastCGI server
    location ~ /Site1/.+\.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            allow 127.0.0.1;
    #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    #       # With php5-fpm:
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_index index.php;
    }
location /Site3/ {
            root    /usr/share/nginx/www/Site3;
    }

    # pass the PHP scripts to FastCGI server
    location ~ /Site3/.+\.php$ {
            allow 127.0.0.1;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            include fastcgi_params;
            #we are directly using the $request_filename as its a single php script
            fastcgi_param SCRIPT_FILENAME $request_filename;
    }

此博客的更多详情,http://programmersjunk.blogspot.com/2013/11/nginx-multiple-sites-in-subdirectories.html