Search code examples
phpbasic-authenticationfpm

Basic authentication in vhost in PHP FPM


I want to setup a basic authentication based vhost for where apache PHP handler is FastCGI(FPM). I found a solution here, but not sure it is same what I doing or not.

Server and SW details: CentOS 6.9 Apache 2.4 - compiled with source PHP 5.6.6 - compiled with source

I have configured PHP with FPM and running PHP as :

php-fpm -y /usr/local/php/php/fpm/php-fpm.conf -c /usr/local/lib/php.ini

I have some pools which:

listen = 127.0.0.1:9001
listen = 127.0.0.1:9002 .. 

and so on

and my vhost configured as :

<VirtualHost *:80>
    DocumentRoot "/var/www/html/webserver/web"
    ServerName spectrumenterprise.jnj.com
    <Directory /var/www/html/webserver/web>
        Options -Indexes +FollowSymLinks -ExecCGI +MultiViews    
        AllowOverride All    
        Order allow,deny
        allow from all
        AuthType Basic
        AuthName "Restricted Content"
        AuthUserFile /etc/httpd/.htpasswd
        Require valid-user    
        Require all granted     
        <IfModule mod_proxy_fcgi.c>
            RewriteEngine On
            RewriteBase /
            RewriteOptions InheritBefore
            RewriteCond %{REQUEST_FILENAME} -f
            RewriteRule ^([^\.]+\.php)$ fcgi://127.0.0.1:9007/var/www/html/webserver/web/$1 [L,P]
        </IfModule>
    </Directory>        
    ErrorLog "logs/spectrumenterprise.jnj.com-error_log"
    CustomLog "logs/spectrumenterprise.jnj.com-access_log" common
</VirtualHost>

If something I am doing wrong here?


Solution

  • I found a solution,

    I have an .htaccess at webroot, I added

    Order allow,deny
    allow from all
    AuthType Basic
    AuthName "Restricted Content"
    AuthUserFile /var/www/html/site1/web/.htpasswd
    Require valid-user
    Require all granted
    

    after

    <IfModule mod_rewrite.c>
        RewriteEngine On
    

    added the .htpasswd file at same location of .htaccess (in my case /var/www/html/site1/web/)

    restart pool and apache

    pkill -o php-fpm
    php-fpm -y /usr/local/php/etc/php-fpm.conf -c /usr/local/lib/php.ini
    /usr/local/apache2/bin/httpd -k restart