Run php7-fpm app with gitlab-ce's bundled nginx

If you install gitlab-ce in your Ubuntu 16.04 with the default configuration, your server will be set up to use the embedded version of nginx. If that's the case and you want to user nginx for other applications (e.g. php7-fpm) on the same server you can do either following ways:

1. Reconfigure gitlab to use external nginx (non-bundled): check here.

2. Add a new server block for the new application to the gitlab's bundled nginx

I chose (2) to run my php web app. Here are the steps:

1. Create a server block configuration in gitlab's nginx directory:

/var/opt/gitlab/nginx/conf/myapp.conf

server {
    listen 80;
    root /var/www/myapp;
    index index.php index.html index.htm;
    server_name myapp.com;

    error_log    /var/log/nginx/myapp_err.log;


    location / {
        try_files $uri $uri/ =404;
    }


    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;

        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;

        include /opt/gitlab/embedded/conf/fastcgi_params;
    }
}

Note: the myapp.com domain must be something that different from the one using for gitlab.

2. Modify gitlab's nginx configuration to include your server block:

/var/opt/gitlab/nginx/conf/nginx.conf

...
include /var/opt/gitlab/nginx/conf/myapp.conf;
...

3. Change the running user of php7-fpm to gitlab-www (because gitlab using it)

/etc/php/7.0/fpm/pool.d/www.conf

...
user = gitlab-www
group = gitlab-www
listen.owner = gitlab-www
listen.group = gitlab-www
...

4. Change the ownership of sock file to gitlab-www:

sudo chown gitlab-www:gitlab-www /run/php/php7.0-fpm.sock

5. Change the ownership of the php app directory to gitlab-www:

sudo chown gitlab-www:gitlab-www -R /var/www/myapp

6. Restart nginx:

sudo gitlab-ctl restart nginx

7. Restart php7-fpm

sudo systemctl restart php7.0-fpm

8. Enjoy

Comments