CIGALE is a powerful multiwavelength spectral energy distribution (SED) fitting code for extragalactic studies. However, the current version of CIGALE is not able to fit X-ray data, which often provide unique insights into AGN intrinsic power. We develop a new X-ray module for CIGALE, allowing it to fit SEDs from the X-ray to infrared (IR). We also improve the AGN fitting of CIGALE from UV-to-IR wavelengths. We implement a modern clumpy two-phase torus model, SKIRTOR. To account for moderately extincted type 1 AGNs, we implement polar-dust extinction. We publicly release the source code (named "X-CIGALE"). We test X-CIGALE with X-ray detected AGNs in SDSS, COSMOS, and AKARI-NEP. The fitting quality (as indicated by reduced χ2) is good in general, indicating that X-CIGALE is capable of modelling the observed SED from X-ray to IR. We discuss constrainability and degeneracy of model parameters in the fitting of AKARI-NEP, for which excellent mid-IR photometric coverage is available. We also test fitting a sample of AKARI-NEP galaxies for which only X-ray upper limits are available from Chandra observations, and find that the upper limit can effectively constrain the AGN SED contribution for some systems. Finally, using X-CIGALE, we assess the ability of Athena to constrain the AGN activity in future extragalactic studies.