2

I'm trying to add a wikimedia site on my server, which already has another site (which doesn't use PHP) on it. I want to put it on a different folder like www.hostname.com/wiki but it would be wonderful to have two different conf files to avoid mixing stuff. At the moment I'm trying to put this wikimedia site on a different port, the 81, and having some issues.

My nginx.conf looks like this:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /run/nginx.pid;

events {
    worker_connections  1024;
}

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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    index   index.html index.htm index.php;
    include /etc/nginx/conf.d/*.conf;

}

And on the conf.d folder I have two files:

site.conf which looks like this:

server {
    listen 80;

    server_name hostname.com;

    auth_basic "Restricted Access";
    auth_basic_user_file /etc/nginx/htpasswd.users;

    location /kibanaadmin/ {
        proxy_pass http://localhost:5601/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    root /usr/share/nginx/html/pruebas/site;
    index index.php index.html index.htm;

    try_files $uri $uri/ /index.php?$args;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

And wiki.conf file:

server {

        listen 81;

        server_name hostname.com;

        root /usr/share/nginx/html/tests/mediawiki;

        client_max_body_size 5m;
        client_body_timeout 60;

        index index.html index.htm index.php;

        location / {
                try_files $uri $uri/ @rewrite;
                autoindex on;
                index index.html index.html index.php;
        }

        location @rewrite {
                rewrite ^/(.*)$ /index.php?title=$1&$args;
        }

        location ^~ /maintenance/ {
                return 403;
        }

        location ~ \.php$ {
                autoindex on;
                include fastcgi_params;
                fastcgi_pass unix:/tmp/phpfpm.sock;
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                try_files $uri /index.php;
                expires max;
                log_not_found off;
        }

        location = /_.gif {
                expires max;
                empty_gif;
        }

        location ^~ /cache/ {
                deny all;
        }

        location /dumps {
                root /usr/share/nginx/html/tests/mediawiki/local;
                autoindex on;
        }
}

I noticed two different problems:

The first one is that when I try to get a php file I get a 502 Bad Gateway and on the error.log I get this message:

2015/07/15 10:56:57 [crit] 16306#0: *6 connect() to unix:/var/run/php-fpm/php-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 111.222.333.444, server: hostname.com, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php-fpm/php-fpm.sock:", host: "hostname.com"

I checked my php config and I'm think is correct, I don't know if it's a nginx config problem or a PHP.

The second problem is that when I go to hostname.com:81 I get and 403 Forbidden (my other site in hostname.com:80 works with no issues) and this log message:

2015/07/15 10:59:15 [error] 16306#0: *9 directory index of

"/usr/share/nginx/html/pruebas/" is forbidden, client: 111.222.333.444, server: hostname.com, request: "GET / HTTP/1.1", host: "hostname.com:81"

My main question is: How can I manage to have the wikimedia site on the port 80 as well as my site but using two conf files of nginx to do it (If is not possible, how can I do it with just one file) and properly config the PHP stuff in nginx?

Note: I think is not a permissions problem because I gave the same to the wikimedia site as the other site has. I'm using Centos 7.1. Thank you in advance.

5
  • Have you checked that /var/run/php-fpm/php-fpm.sock socket exists? Jul 15, 2015 at 10:02
  • This is related with the 1st problem. Php-fpm.sock doesn't exists, there is a php-fpm.pid. What does this mean and how can I create it? I'm not used to PHP in centos. Jul 15, 2015 at 10:08
  • You need to show how your PHP files are going on with your filesystems. Jul 15, 2015 at 10:21
  • Do you mean the php files in the wikimedia folder? Jul 15, 2015 at 16:06
  • Sorry, I wrote a bit invalid text. What I meant is that you need to show your PHP-FPM pool configurations. Jul 15, 2015 at 16:51

2 Answers 2

1

One option to have the configurations in separate files is to use include directive. Something like this:

server {
    listen 80;

    server_name hostname.com;


    auth_basic "Restricted Access";
    auth_basic_user_file /etc/nginx/htpasswd.users;

    location /kibanaadmin/ {
        proxy_pass http://localhost:5601/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    root /usr/share/nginx/html/pruebas/site;
    index index.php index.html index.htm;

    try_files $uri $uri/ /index.php?$args;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    include '/path/to/wiki.conf';
}

And your wiki.conf would look like this:

location /wiki {
    alias /usr/share/nginx/html/tests/mediawiki;
    try_files $uri $uri/ @rewrite;
    autoindex on;
    index index.html index.html index.php;
}

location @rewrite {
    rewrite ^/(.*)$ /index.php?title=$1&$args;
}

location ^~ /maintenance/ {
    return 403;
}

location ~ \.php$ {
    autoindex on;
    include fastcgi_params;
    fastcgi_pass unix:/tmp/phpfpm.sock;
}

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    try_files $uri /index.php;
    expires max;
    log_not_found off;
}

location = /_.gif {
    expires max;
    empty_gif;
    }

    location ^~ /cache/ {
            deny all;
    }

    location /dumps {
            root /usr/share/nginx/html/tests/mediawiki/local;
            autoindex on;
    }
}
1
  • I tried your solution but it didn't work. I posted the right solution. The PHP problem was solved by reinstalling and using the proper configuration and the second problem was solved also with the new cfg file. Thanks ! Jul 20, 2015 at 15:08
0

Thanks for your answers. I solved both problems. The first problem was the 502 Bad Gateway and I solved by reinstalling the PHP modules and writing the proper configuration on the nginx files (check out the location ~* .php$ part in the wiki location block).

I finally made use of just one configuration file, writing a new location block and also writing the PHP configuration in the right way.

The second problem (403) was solved too using this file.

server {
    listen 80;

    server_name hostname;

    location /admin/ {

        auth_basic "Restricted Access";
        auth_basic_user_file /etc/nginx/htpasswd.users;

        root /usr/share/nginx/html/pruebas/admin;
        index index.php index.html index.htm;

        proxy_pass http://localhost:5601/;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
    }


    # the following line is responsible for clean URLs
    try_files $uri $uri/ /index.php?$args;

    location /site {
        alias /usr/share/nginx/html/site;
        auth_basic "Restricted Access";
        auth_basic_user_file /etc/nginx/htpasswd.users;
    }

    location /wiki {

        auth_basic "Restricted Access";
        auth_basic_user_file /etc/nginx/wiki/htpasswd;

        alias /usr/share/nginx/html/mediawiki;

        location ~* \.php$ {
          fastcgi_pass    127.0.0.1:9000;
          include         fastcgi.conf;
          fastcgi_param   SCRIPT_FILENAME $request_filename;
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|ico|xml)$ {
            expires 1y;
                access_log off;
                log_not_found off;
        }
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .