How to Install CraftCMS with Apache and Let's Encrypt SSL on Ubuntu 22.04 LTS
Craft is an open-source, flexible, and feature-rich content management system for developers and content authors. It offers all the customization features necessary for building a powerful website. It allows you to manage the content of various sites from a single dashboard. It is an alternative solution to WordPress and Drupal for building bespoke digital experiences. It has hundreds of free and paid plugins allowing you to add functionality.
This post will explain how to install Craft CMS with Apache and Let's Encrypt SSL on Ubuntu 22.04.
Prerequisites
- A server running Ubuntu 22.04.
- Valid domain name pointed with your server IP.
- A root password is configured on the server.
Install LAMP Server
Craft CMS runs on the web server, is written in PHP, and uses MariaDB as a database backend. So you will need to install all those packages to your server.
You can execute the following command to install all those packages:
apt-get install apache2 mariadb-server php php-cli libapache2-mod-php php-common php-json php-curl php-gd php-imagick php-json php-mbstring php-mysql php-pgsql php-zip php-intl php-xml -y
After installing all the packages, edit the PHP configuration file and change the default settings:
nano /etc/php/8.1/php.ini
Change the following settings:
memory_limit = 512M post_max_size = 32M upload_max_filesize = 32M max_execution_time = 360
Save and close the file then restart the Apache service to apply the changes:
systemctl restart apache2
Create a Database for CraftCMS
Next, you will need to create a database and user for Fork CMS. First, log in to the MariaDB shell with the following command:
mysql
Once you are login, create a database and user with the following command:
MariaDB [(none)]> CREATE DATABASE craftcms;
MariaDB [(none)]> GRANT ALL ON craftcms.* TO 'craftuser' IDENTIFIED BY 'password';
Next, flush the privileges and exit from the MariaDB shell with the following command:
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;
At this point, the MariaDB database is created for Craft CMS. You can now proceed to the next step.
Install Craft CMS Using Compose
Next, you will need to install the Composer to download the latest version of Craft CMS. You can install it using the following command:
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
Once the Composer is installed, navigate to the Apache web root directory and create a Craft CMS project using the following command:
cd /var/www/html
composer create-project craftcms/craft craftcms
You will be asked to provide your database settings, admin username, password, site URL as shown below:
Which database driver are you using? (mysql or pgsql) [mysql] Database server name or IP address: [127.0.0.1] Database port: [3306] Database username: [root] craftuser Database password: Database name: craft Database table prefix: Testing database credentials ... success! Saving database credentials to your .env file ... done Install Craft now? (yes|no) [yes]:yes Username: [admin] admin Email: [email protected] Password: Confirm: Site name: CraftCMS Site Site URL: http://craftcms.example.com Site language: [en-US] > add foreign key fk_rlbmgnhpxsljkaunjwnsezfrnrkhwzpthfsq: {{%widgets}} (userId) references {{%users}} (id) ... done (time: 0.035s) > populating the info table ... done > saving default site data ... done > saving the first user ... done *** installed Craft successfully (time: 5.449s)
Next, set proper permission and ownership to the Craft CMS directory:
chown -R www-data:www-data /var/www/html/craftcms/
chmod -R 755 /var/www/html/craftcms/
Once you are finished, you can proceed to the next step.
Configure Apache for Craft CMS
Next, you will need to create an Apache virtual host configuration file for Craft CMS. You can create it with the following command:
nano /etc/apache2/sites-available/craftcms.conf
Add the following lines:
<VirtualHost *:80> ServerAdmin [email protected] DocumentRoot /var/www/html/craftcms/web ServerName craftcms.example.com <Directory /var/www/html/craftcms/web/> Options FollowSymlinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined <Directory /var/www/html/craftcms/web/> RewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*) index.php [PT,L] </Directory> </VirtualHost>
Save and close the file then activate the Apache virtual host and rewrite module using the following command:
a2ensite craftcms.conf
a2enmod rewrite
Next, restart the Apache service to apply the changes:
systemctl restart apache2
You can also check the Apache status with the following command:
systemctl status apache2
You will get the following output:
? apache2.service - The Apache HTTP Server Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2022-06-17 15:48:11 UTC; 31min ago Docs: https://httpd.apache.org/docs/2.4/ Process: 37935 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS) Process: 40916 ExecReload=/usr/sbin/apachectl graceful (code=exited, status=0/SUCCESS) Main PID: 37939 (apache2) Tasks: 6 (limit: 2292) Memory: 53.0M CPU: 28.718s CGroup: /system.slice/apache2.service ??37939 /usr/sbin/apache2 -k start ??40920 /usr/sbin/apache2 -k start ??40921 /usr/sbin/apache2 -k start ??40922 /usr/sbin/apache2 -k start ??40923 /usr/sbin/apache2 -k start ??40924 /usr/sbin/apache2 -k start Jun 17 15:48:11 ubuntu2204 systemd[1]: Starting The Apache HTTP Server...
Once your Apache web server is configured, you can proceed to the next step.
Access Craft CMS Web Interface
Now, open your web browser and type the URL http://craftcms.example.com to access the Craft CMS web interface. You should see the following page:
Click on the go to your control panel. You will be redirected to the Craft CMS login page:
Provide your admin username, password and click on the login button. You should see the Craft CMS dashboard on the following page:
Secure Craft CMS with Let's Encrypt SSL
Next, it is a good idea to secure your website with Let's Encrypt SSL. First, install the Certbot client package with the following command:
apt-get install python3-certbot-apache -y
After the successful installation, run the following command to secure your website with Let's Encrypt SSL:
certbot --apache -d craftcms.example.com
You will be asked to provide your email and accept the term of service as shown below:
Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator standalone, Installer None Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): [email protected] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must agree in order to register with the ACME server at https://acme-v02.api.letsencrypt.org/directory - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (A)gree/(C)ancel: A - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Would you be willing to share your email address with the Electronic Frontier Foundation, a founding partner of the Let's Encrypt project and the non-profit organization that develops Certbot? We'd like to send you email about our work encrypting the web, EFF news, campaigns, and ways to support digital freedom. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: Y Plugins selected: Authenticator apache, Installer apache Obtaining a new certificate Performing the following challenges: http-01 challenge for craftcms.example.com Enabled Apache rewrite module Waiting for verification... Cleaning up challenges Created an SSL vhost at /etc/apache2/sites-available/craftcms-le-ssl.conf Enabled Apache socache_shmcb module Enabled Apache ssl module Deploying Certificate to VirtualHost /etc/apache2/sites-available/craftcms-le-ssl.conf Enabling available site: /etc/apache2/sites-available/craftcms-le-ssl.conf
Next, select whether or not to redirect HTTP traffic to HTTPS as shown below:
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: No redirect - Make no further changes to the webserver configuration. 2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for new sites, or if you're confident your site works on HTTPS. You can undo this change by editing your web server's configuration. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Type 2 and hit Enter to install the Let's Encrypt SSL for your website:
Enabled Apache rewrite module Redirecting vhost in /etc/apache2/sites-enabled/craftcms.conf to ssl vhost in /etc/apache2/sites-available/craftcms-le-ssl.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations! You have successfully enabled https://craftcms.example.com You should test your configuration at: https://www.ssllabs.com/ssltest/analyze.html?d=craftcms.example.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/craftcms.example.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/craftcms.example.com/privkey.pem Your cert will expire on 2022-09-17. To obtain a new or tweaked version of this certificate in the future, simply run certbot again with the "certonly" option. To non-interactively renew *all* of your certificates, run "certbot renew" - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le
Conclusion
Congratulations! you have successfully installed Craft CMS with Apache and Let's Encrypt SSL on Ubuntu 22.04. You can now explore the CraftCMS features and start creating a powerful website using the Craft CMS. Feel free to ask me if you have any questions.