已完成PHP、Apache、MySQL部署,phpMyAdmin会话Cookie设置失败如何解决?
Hey there, let's fix that Failed to set session cookie. Maybe you are using HTTP instead of HTTPS error you're hitting with phpMyAdmin. I’ve dealt with this exact issue a handful of times, so here are the go-to solutions that usually resolve it:
First, locate your config.inc.php file—this is typically in /usr/share/phpmyadmin or the phpMyAdmin folder within your web root. Open it up and adjust these settings:
- Set a valid session save path: Make sure phpMyAdmin can write to a dedicated session directory. Add or update this line:
Then ensure the directory has the right permissions for your web server user (usually$cfg['SessionSavePath'] = '/var/lib/php/sessions';www-data):chown www-data:www-data /var/lib/php/sessions chmod 700 /var/lib/php/sessions - Disable forced HTTPS (if you're not using it yet): If you're still on HTTP, turn off the SSL enforcement:
$cfg['ForceSSL'] = false; - Adjust cookie settings: Make sure these cookie-related configs match your setup:
$cfg['CookieSameSite'] = 'Lax'; $cfg['CookieSecure'] = false; // Set to true once you switch to HTTPS $cfg['CookieHttpOnly'] = true;
Open your php.ini file (the path varies by PHP version and server—common locations are /etc/php/7.x/apache2/php.ini or /etc/php.ini). Check these parameters:
session.save_path: Confirm it points to the same writable directory you set in phpMyAdminsession.cookie_secure: Set toOffif you're using HTTP; switch toOnonce you enable HTTPSsession.cookie_samesite: Set toLax(this is the safest default for most setups)
After making changes, restart Apache to apply them:
sudo systemctl restart apache2
If your Apache virtual host config has rules that force the Secure attribute on all cookies, that'll break session cookies over HTTP. Look for lines like this in your Apache config file:
Header edit Set-Cookie ^(.*)$ $1;Secure
If you find it, either comment it out, or wrap it in an HTTPS-only condition so it only applies when you're using secure connections:
<If "%{HTTPS} == 'on'"> Header edit Set-Cookie ^(.*)$ $1;Secure </If>
Restart Apache after updating the config.
Sometimes stale browser data can cause cookie issues. Try clearing the cookies for your phpMyAdmin domain, or open phpMyAdmin in incognito/private browsing mode to see if that fixes it.
If you're running a system with SELinux enabled (like CentOS), it might be blocking Apache from writing to the session directory. Test this by temporarily disabling SELinux:
sudo setenforce 0
If the error goes away, you'll need to add a SELinux rule to allow Apache access to the session directory permanently:
sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/lib/php/sessions(/.*)?" sudo restorecon -Rv /var/lib/php/sessions
内容的提问来源于stack exchange,提问作者Lancer

