How to configure the time zone on your Linux server

timezone-linux

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:

root@timezone:~# timedatectl status | grep "Time zone"
                       Time zone: Europe/Paris (CEST, +0200)
root@timezone:~#

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:

timezone

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:

root@timezone:~# 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.
root@timezone:~# timedatectl status | grep "Time zone"
                       Time zone: America/Chicago (CDT, -0500)
root@timezone:~#


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:


nano /etc/php/7.2/apache2/php.ini

Type the [Ctrl]+W key combination, type timezone, and hit Enter. You should then see the line: 

date.timezone =

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.

  • linux, time zone
  • 3 användare blev hjälpta av detta svar
Hjälpte svaret dig?

Relaterade artiklar

ownCloud Auto Install Script on Debian 7

Here is an easy way to install ownCloud on Debian 7 with a script, but first, for those who...

NGINX – Allow access only to certain IPs

Nginx has a nice module that not many people know about, it basically enables us...

How to configure NTP client in CentOS

What's NTP? NTP stands for Network Time Protocol, and it is an Internet protocol used to...

Initial Server Setup on CentOS 6

Here are some recommendations to setup your VPS or server when you first get it, some of these...

How To Install nginx on CentOS

What is NGINX? Nginx (pronounced "engine-x") is an open source reverse proxy server for HTTP,...