Categories
Knowledge Base Server Administration

How to Configure Timezone Properly in A LAMP Stack

This article covers:

  1. How to Setup Time Zone on Your Web Server based on LAMP Stack

Audiences of this article:

  • LAMP Stack Admin

Change Linux Timezone

To check timezone in a Linux OS such as Ubuntu:

timedatectl

The output looks like this:

Local time: Fri 2020-04-10 15:10:28 AEST
Universal time: Fri 2020-04-10 05:10:28 UTC
RTC time: Fri 2020-04-10 05:10:29
Time zone: Australia/Perth (AEST, +1000)
System clock synchronized: yes
systemd-timesyncd.service active: yes
RTC in local TZ: no

For example, we need to change OS’s time zone from Australia/Perth to Australia/Sydney. Make sure the new timezone are supported by both OS and PHP. Check PHP supported Australia timezone here.

To find out a list of supported timezone in Ubuntu:

timedatectl list-timezones | grep -i australia

# Output timezones

Australia/Adelaide
Australia/Brisbane
Australia/Broken_Hill
Australia/Currie
Australia/Darwin
Australia/Eucla
Australia/Hobart
Australia/Lindeman
Australia/Lord_Howe
Australia/Melbourne
Australia/Perth
Australia/Sydney

To Change Ubuntu timezone:

# Firstly unlink current timezone

sudo unlink /etc/localtime

# Link new timezone with local time

sudo ln -s /usr/share/zoneinfo/Australia/Sydney /etc/localtime

Change PHP Timezone

Firstly, find out the current PHP version:

php -v

In our case it’s 7.1, then edit php.ini file used by Apache

pico /etc/php/7.1/apache2/php.ini

Un-comment line “;date.timezone =” change it to:

date.timezone = Australia/Sydney

Reload Apache 2

sudo systemctl reload apache2

Have a look at your access log to see if log time is the same as your local time, below is the default apache access log.

tail -f /var/log/apache2/access.log

It’s important to set up both PHP timezone and server OS timezone to your local time to ensure your website is running in the correct timezone. Also keep it in mind that, some third party modules can ignore your timezone settings and use UTC instead.

By Ethan

To many, a business is a lifetime commitment. It's easy to start one yet difficult to make it successful. Attitude, skills, experiences and dedication help hone the craft along the way, but it's often the great vision and resilience to remain focused wins the game. Read more about me here