CMS

Installing the MODX content managment system

published on

MODX is another content managment system (CMS) similar in many ways to Wordpress, Joomla and Drupal.

Whereas Wordpress is aimed more at the everyday user (perhaps they just want a homepage and a blog), MODX is aimed more at professionals and is more suited to someone who is already able to create websites.

More information about MODX can be found at https://modx.com/.

Whilst the MODX team do give instructions on how to install MODX on Apache and NGinx they do state that it is best to use NGinx. For this reason I will not include instructions on how to install with Apache.

See Installing the ModX content management system on OpenResty if you prefer to use OpenResty.

The initial setup

sudo apt install lsb-release ca-certificates apt-transport-https software-properties-common language-pack-en-base unzip zip curl
sudo locale-gen en_US.UTF-8
sudo add-apt-repository ppa:ondrej/php
sudo add-apt-repository ppa:ondrej/nginx-mainline

Now run sudo nano /etc/apt/sources.list.d/ondrej-ubuntu-nginx-mainline-*.list and uncomment the deb-src line.

Finally, run sudo apt-get update to update the package lists.

Install NGinx

sudo apt install nginx-core nginx-common nginx nginx-full libgeoip-dev libpcre3-dev

Install PHP 7.4

sudo apt install openssl php7.4-{common, cli, bz2, zip, curl, intl, mysql, snmp, memcached, imagick, gd, fileinfo, imap, ldap, soap, tidy, xml, gd, gmp, pspell, mbstring, opcache, fpm, ssh2, imap, redis, apcu, mcrypt}

Now you need to edit two configuration files. The first can be done by running sudo nano /etc/php/7.4/fpm/pool.d/www.conf and making sure the following lines read as below.

listen = /run/php/php7.4-fpm.sock
listen.owner = www-data
listen.mode = 0660 

The second configuration file can be edited by running sudo nano /etc/php/7.4/fpm/php.ini and adding, or editing, the following lines to read as so:

register_globals = Off (or 0)
output_buffering = Off
expose_php = off
max_execution_time = 120
max_input_time = 60
max_input_vars = 1000
memory_limit = 512M       
display_errors = Off
display_startup_errors = Off
log_errors = On
html_errors = Off
post_max_size = 128M
upload_max_filesize = 128M
allow_url_fopen = Off
date.timezone = Europe/London
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
opcache.revalidate_freq=200
cgi.fix_pathinfo=0
extension=curl.dll
extension=pdo_mysql
zlib.output_compression On
zlib.output_compression_level 5

Finally, run sudo systemctl restart php7.4-fpm to restart PHP.

Now install MariaDB

sudo apt install mariadb-server
sudo mysql_secure_installation

Then create a new database by running sudo mysql -u root -p and entering the following lines.

CREATE DATABASE [database_name] charset=utf8mb4;
CREATE USER '[user_name]'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON [database_name].* TO '[user_name]'@'localhost';
FLUSH PRIVILEGES;
exit

Add the server block

Run sudo nano /etc/nginx/sites-available/modx.confand add the following lines

server {
        listen 80;
        server_name example.com www.example.com;
        root /var/www/example.com;
        index index.php; 

        client_max_body_size 30M;

        access_log /var/log/nginx/[domain].access.log;
        error_log /var/log/nginx/[domain].error.log;

        charset utf-8;
        source_charset utf-8;

        ## Do not accept DELETE, SEARCH and other methods ##
        if ($request_method !~ ^(GET|HEAD|POST)$ ) {
          return 444;
        }

        # Deny all attempts to access hidden files
        # such as .htaccess, .htpasswd, .DS_Store (Mac), .git, .etc...
        location ~ /\. {
          deny all;
        }

        error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 420 422 423 424 426 428 429 431 444 449 450 451 500 501 502 503 504 505 506 507 508 509 510 511 /error.html;
          location /error.html {
         internal;
        }

        limit_conn connlimit 1000; # Simultaneous Connections

        # protect parts of MODX
        location ~ ^/(\.(?!well_known)|_build|_gitify|_backup|core|config.core.php) {
          rewrite ^/(\.(?!well_known)|_build|_gitify|_backup|core|config.core.php) /index.php?q=doesnotexist;    
        }

        # the MODX part
        location @modx-rewrite {
            rewrite ^/(.*)$ /index.php?q=$1&$args last;
        }

        location / {
            absolute_redirect off;
            try_files $uri $uri/ @modx-rewrite;
        }

        location ~ \.php$ {
                include fastcgi_params;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                fastcgi_pass   unix:/run/php/php7.4-fpm.sock;
        }

}

And finally, run the following lines to activate and test your new server block.

sudo ln -s /etc/nginx/sites-available/modx.conf /etc/nginx/sites-enabled/modx.conf
sudo nginx -t
nginx -s reload

Secure your site with a SSL / TLS certificate

sudo apt install snapd
sudo snap install core; sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo certbot -d [domain] -d www.[domain] --nginx

Now install MODX

cd ~
wget https://modx.com/download/direct/modx-3.0.1-pl.zip
unzip modx-3.0.1-pl.zip
sudo mkdir /var/www/[domain]
cd modx-3.0.1-pl
sudo cp -r * /var/www/[domain]
cd ..
rm -rf modx-3.0.1-pl modx-3.0.1-pl.zip
sudo touch /var/www/[domain]/error.html
sudo touch /var/www/[domain]/core/config/config.inc.php
sudo chown -R www-data:www-data /var/www/[domain]
sudo chmod -R 755 /var/www/[domain]

Finishing off setup

In order to finish off the setup of MODX on your domain you need to visit https://[domain]/setup in your browser and complete the setup wizard.

Make sure that you select the option to delete the setup directory once the installation has completed. If you don't it is possible that somebody else could run this and destroy your new website.

Final thoughts

As always you may want to add extra commands to the NGinx server block to further refine and secure your setup.

However, this guide gives you the knowledge to set up a MODX installation. I suggest you play around with it in order to learn more about how to use it and get your own site up and running.

You can find a Quick Start Video series at https://support.modx.com/hc/en-us/articles/216187898-Quick-Start-Video-Series which may help you find your feet.