As much as time is important for your daily routine (or should be), so is time important for your server, as an IT admin or sysadmin making sure that the time zone is properly set on your server can make the difference on whether the applications it hosts run properly.
On this guide we're going to show you how to properly set the time zone on your Linux server, we're using an Ubuntu server for this guide, but this process should work fine on any other recent Linux distribution.
Let's first find out what the time zone currently is, we may not need to modify anything if it's properly set
timedatectl status | grep "Time zone"
This command should report the currently set time zone:
If your time zone is not properly set, no worries, let's use the following command to change it:
sudo dpkg-reconfigure tzdata
It should look like this:
Here you can locate the time zone you wish to apply to your server, in my case I'll go ahead and set the time zone to Chicago, and I can verify this afterwards by running the following command:
[email protected]:~# sudo dpkg-reconfigure tzdata
Current default time zone: 'America/Chicago'
Local time is now: Wed Sep 18 05:04:39 CDT 2019.
Universal Time is now: Wed Sep 18 10:04:39 UTC 2019.
[email protected]:~# timedatectl status | grep "Time zone"
Time zone: America/Chicago (CDT, -0500)
PHP Time Zone
You most likely also have PHP installed on your server as nowadays most way too many web services depend on it, so we'll also learn how to set the time zone on your php installation.
We'll assume that you're running php 7.2 on your server and that the path to your php.ini is the one shown below, so we'll go ahead modify the php.ini file as follows:
Type the [Ctrl]+W key combination, type timezone, and hit Enter. You should then see the line:
This line needs to be configured in the form:
date.timezone = GEOGRAPHIC_REGION/CITY
Where GEOGRAPHIC_REGION is your Country (or region) and CITY is the city in which you are located. In some instances, where you live in a city with the same name as cities in other states, you'll have to use the format GEOGRAPHIC_REGION/STATE/CITY. For example, if you're in Louisville, KY, the time zone line would be:
date.timezone = America/Kentucky/Louisville
Save and close that file.
PHP is now properly configured for your time zone. Restart Apache (so the change will take effect) with the command:
sudo systemctl restart apache2
This should be it, I'd recommend you go through all your servers and make sure that the time is properly set.