How to Set Cookie Expiration in Apache

set cookie expiration in apache

Website cookies allow you to easily manage dynamic websites and even personalize user experience. However, a cookie lasts for only a single session. So you might need to dynamically extend its expiry to keep the session going. Let us take a look at how to set cookie expiration in Apache.


How to Set Cookie Expiration in Apache

Here are the steps to set cookie expiration in Apache. We will use the CookieExpires directive to update the expiry date of cookies.


Please ensure you have enabled mod_headers in Apache for this purpose. Here are the steps.


1. Open Apache Config file

Open Apache config file in a text editor. You will find it at one of the following locations, depending on the type of installation and your linux distribution.

  • /etc/apache2/httpd.conf
  • /etc/httpd/httpd.conf
  • /etc/httpd/conf/httpd.conf


$ sudo vim /etc/apache2/httpd.conf


2. Set Cookie Expiration in Apache

Add the following line to change the expiry date of all your cookies

CookieExpires 2 weeks 3 days 7 hours

You can also specify expiry in minutes and seconds. The above code will update expiry date for all your cookies.


3. Restart Apache web server

Restart Apache web server to apply changes

$ sudo /etc/init.d/apache2 start [Debian or Ubuntu]
# sudo apachectl restart [RHEL, CentOS or Fedora]


Similarly, you can not only set cookie expiration but other values as well.


About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!