20 min read

Improve the Quality of Healthcare: Install OpenEMR on Ubuntu 22.04


OpenEMR is the most popular and powerful open source electronic health records and medical practice management solution. It offers a wide range of features, including integrated patient health records, appointments,patient portal, electronic billing, multi-language support, and many more.

Website: OpenEMR

Prerequisites

Before you begin the installation process, make sure you have the following:

  • A server running Ubuntu 22.04
  • A non-root sudo user or root
  • Domain/Subdomain name like - example.com or openemr.example.com

Ensure that your system is up to date by running the following commands:

$ sudo apt update
$ sudo apt upgrade

You’ll also need to install some necessary packages. Run the following command to install the required packages:

$ sudo apt install wget curl nano ufw software-properties-common dirmngr apt-transport-https gnupg2 ca-certificates lsb-release ubuntu-keyring unzip -y

Step 1 - Install Nginx

Ubuntu 22.04 ships with an older version of Nginx. To install the latest version, you’ll need to add the official Nginx repository. Follow these steps to install Nginx:

#Import Nginx’s signing key
$ curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null   

# Add the Nginx repository for the stable version
$ echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg arch=amd64] http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list   

# Update the system repositories
$ sudo apt update                       

# Install Nginx
$ sudo apt install nginx                

# Verify the installation
$ nginx -v                              

# Start the Nginx server
$ sudo systemctl start nginx            

Step 2 - Install Configure MySQL

Ubuntu 22.04 comes with the latest version of MySQL. Use the following command to install it:

$ sudo apt install mysql-server

# To check the version of MySQL, run:
$ mysql --version
# This step is essential for MySQL versions 8.0.28 and above. Enter the MySQL Shell:
$ sudo mysql

# Set the password for the root user. Make sure to use a strong password with a mix of numbers, uppercase and lowercase letters, and special characters:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'StrongPassword14!';

# Exit the shell:
mysql> exit

Run the MySQL secure install script to further secure your MySQL installation and Follow the prompts to set the desired security options for your MySQL server.

$ sudo mysql_secure_installation

Step 3 - Install PHP and Its Extensions

Ubuntu 22.04 includes PHP 8.1.2 by default, but we’ll install the latest version, PHP 8.3, using Ondrej’s PHP repository. Follow these steps to install PHP and the required extensions for OpenEMR:

Installation

# Add Ondrej’s PHP repository:
$ sudo add-apt-repository ppa:ondrej/php

# Update the system repositories:
$ sudo apt update

# Install PHP and its extensions:
$ sudo apt install php8.3-fpm php8.3-mysql php8.3-bcmath php8.3-xml php8.3-zip php8.3-curl php8.3-mbstring php8.3-gd php8.3-tidy php8.3-intl php8.3-cli php8.3x-soap imagemagick libtiff-tools php8.2-ldap

# Verify the installation:
$ php --version

Configure PHP-FPM

Open the PHP-FPM configuration file for editing:

$ sudo nano /etc/php/8.3/fpm/pool.d/www.conf

Find the lines user = www-data and group = www-data and change them to user = nginx and group = nginx respectively.

Find the lines listen.owner = www-data and listen.group = www-data and change them to listen.owner = nginx and listen.group = nginx respectively.

Save the file and exit the editor.

# Increase the execution time for PHP-FPM and PHP-CLI to 60 seconds:
$ sudo sed -i 's/max_execution_time = 30/max_execution_time = 60/' /etc/php/8.3/fpm/php.ini

# Set the value of max_input_time to -1 to disable the time limit:
$ sudo sed -i 's/max_input_time = 60/max_input_time = -1/' /etc/php/8.3/fpm/php.ini

# Increase the memory limit for PHP-FPM:
$ sudo sed -i 's/memory_limit = 128M/memory_limit = 512M/' /etc/php/8.3/fpm/php.ini

# Increase the file upload size:
$ sudo sed -i 's/post_max_size = 8M/post_max_size = 30M/' /etc/php/8.3/fpm/php.ini
$ sudo sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 30M/' /etc/php/8.3/fpm/php.ini

# Increase the number of maximum input variables:
$ sudo sed -i 's/;max_input_vars = 1000/max_input_vars = 3000/g' /etc/php/8.2/fpm/php.ini

# Allow accessing local files with LOAD DATA statements:
$ sudo sed -i 's/;mysqli.allow_local_infile = On/mysqli.allow_local_infile = On/g' /etc/php/8.3/fpm/php.ini

# Restart the PHP-FPM service:
$ sudo systemctl restart php8.3-fpm

# Change the group of the PHP sessions directory to Nginx:
$ sudo chgrp -R nginx /var/lib/php/sessions

Step 4 - Configure Firewall

The first step is to configure the firewall on your Ubuntu server. By default, Ubuntu comes with ufw (Uncomplicated Firewall) installed.

#  To check if the firewall is running, use the following command:
$ sudo ufw status

# If the firewall is inactive, you’ll need to enable it. Start by allowing SSH, HTTP, and HTTPS ports:
$ sudo ufw allow OpenSSH
$ sudo ufw allow http
$ sudo ufw allow https

# Once you’ve allowed the necessary ports, enable the firewall:
$ sudo ufw enable

# Verify the status of the firewall to ensure it’s active:
$ sudo ufw status

Step 5 – Download OpenEMR

Visit the OpenEMR download page and grab the link for the latest version[Current: 7.0.2]. Use the wget command to Download OpenEMR to your server:

$ wget https://sourceforge.net/projects/openemr/files/OpenEMR%20Current/7.0.2/openemr-7.0.2.tar.gz

# Extract the downloaded files:
$ tar -pxzf openemr-7.0.1.tar.gz

# Move the extracted files to the web directory:
sudo mv openemr-7.0.1 /var/www/openemr

# Change the ownership of the OpenEMR directory to the Nginx user:
$ sudo chown -R nginx:nginx /var/www/openemr

Step 6 – Domain Configure on Nginx

Create and open the Nginx configuration file for OpenEMR:

$ sudo nano /etc/nginx/conf.d/openemr.conf
server {
    listen 80;
    server_name openemr.example.com;
    root /var/www/openemr;
    index index.php;
    location / {
        try_files $uri $uri/ =404;
    }
}

# Open the main Nginx configuration file for editing:
$ sudo nano /etc/nginx/nginx.conf

# Add the following line before the line include /etc/nginx/conf.d/*.conf;:
server_names_hash_bucket_size 64;

# Test your configuration for syntax errors:
$ sudo nginx -t

# If you see “syntax is okay” and “test is successful”, restart NGINX to apply the changes:
$ sudo systemctl restart nginx

Step 7 – Install SSL

# Ensure your version of Snapd is up to date Run the following commands :
$ sudo snap install core && sudo snap refresh core

# Install Certbot:
$ apt install certbot python3-certbot-nginx -y

# Create a symbolic link to the /usr/bin directory:
$ sudo ln -s /snap/bin/certbot /usr/bin/certbot

# Generate an SSL certificate using Certbot and apply:
sudo certbot --nginx --agree-tos --no-eff-email --staple-ocsp --preferred-challenges http --email [email protected] -d openemr.example.com

# Response

# You have an existing certificate that has exactly the same domains or certificate name you # requested and isn't close to expiry.
# (ref: /etc/letsencrypt/renewal/openemr.satpg.com.conf)
# 
# What would you like to do?
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 1: Attempt to reinstall this existing certificate
# 2: Renew & replace the certificate (may be subject to CA rate limits)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 

Enter:  1

#Deploying certificate
#Successfully deployed certificate for openemr.satpg.com to /etc/nginx/conf.d/openemr.conf
#Congratulations! You have successfully enabled HTTPS on https://openemr.example.com

After Install certificate need to some more changes in /etc/nginx/conf.d/openemr.conf file. Here is the complete File , no need to change Certbot settings

server {

    root /var/www/openemr;
    index index.html index.htm index.php;

    server_name openemr.example.com;

	access_log  /var/log/nginx/openemr.access.log;
    error_log   /var/log/nginx/openemr.error.log;

    location / {
       	try_files $uri $uri/ /index.php;
    }
    
    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/openemr.satpg.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/openemr.satpg.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    ## START section :: NEED TO ADD THIS
        # Pass PHP Scripts To FastCGI Server
        location ~* \.php$ {
            try_files $uri =404;
            fastcgi_index  index.php;
            fastcgi_pass unix:/run/php/php8.2-fpm.sock; # Depends On The PHP Version
            fastcgi_param SCRIPT_FILENAME  $realpath_root$fastcgi_script_name;
            fastcgi_param DOCUMENT_ROOT $realpath_root;
            include fastcgi_params;
        }

        

        # deny access to writable files/directories
        location ~* ^/sites/*/(documents|edi|era) {
            deny all;
            return 404;
        }

        # deny access to certain directories
        location ~* ^/(contrib|tests) {
            deny all;
            return 404;
        }

        # Alternatively all access to these files can be denied
        location ~* ^/(admin|setup|acl_setup|acl_upgrade|sl_convert|sql_upgrade|gacl/setup|ippf_upgrade|sql_patch)\.php {
            deny all;
            return 404;
        }
        
        #enable rewrite
        if (!-e $request_filename) {
                # Needed for zend to work
                rewrite ^(.*/zend_modules/public)(.*) $1/index.php?$is_args$args last;

                # Needed for patient portal to work
                rewrite ^(.*/portal/patient)(.*) $1/index.php?_REWRITE_COMMAND=$1$2 last;

                # Needed for REST API/FHIR to work
                rewrite ^(.*/apis/)(.*) $1/dispatch.php?_REWRITE_COMMAND=$2 last;

                # Needed for OAuth2 to work
                rewrite ^(.*/oauth2/)(.*) $1/authorize.php?_REWRITE_COMMAND=$2 last;
            }

        location =https://ddv1datqrr7r4.cloudfront.net/favicon.ico?x70162 {
            log_not_found off;
            access_log off;
        }

        location = /robots.txt  {
            log_not_found off;
            access_log off;
        }

        location ~ /\. {
            deny all;
        }

    ## End section :: NEED TO ADD THIS

}

server {
    listen       80;
    listen       [::]:80;
    server_name  openemr.example.com;
    return 301   https://$host$request_uri;
}

Step 8 – Install OpenEMR

Now it’s time to complete the OpenEMR installation. Follow these steps:

  1. Open the URL https://openemr.example.com in your browser.
  2. On the setup screen, check the file permissions and ensure they are ready to proceed. If you see the word “ready” in green, click the blue button to proceed to step 1.
  3. On the next page, select the option “I have already created the database” and click the button to proceed to step 2.
  4. Fill in the database credentials you configured in step 4, and enter your administrator account credentials. Make sure your username is at least 12 characters long. You can enable two-factor authentication (2FA) here, but it’s recommended to set it up later. Click the button to create the database and user account.
  5. The installation status and your username and password will be displayed on the next page. Click the button to proceed to step 4.
  6. On the next page, verify the recommended values for PHP settings. Ignore any incorrect values for max_input_time and max_execution_time. You can verify the current values using the following commands: $ php -i | grep”max_input_time” $ php -i | grep”max_execution_time”
  7. Once you’re satisfied, click the button to proceed to step 5.
  8. The next step lists Apache server settings, which you can ignore since you’re using Nginx. Click the button to proceed to the next page.
  9. Here, you’ll be asked to select a theme for the administration panel. Select the “Keep Current” option and click the button to proceed. You can change the theme later in the administration panel.
  10. The last page provides final notes about the software and your account credentials. Click the “Start” button to open the login page.
  11. Enter your credentials and click the “Login” button to access the OpenEMR dashboard.

If you didn’t configure two-factor authentication during the installation, you can do so by clicking the avatar icon at the top right and selecting the “MFA Management” option.

Congratulations! You have successfully installed OpenEMR on your Ubuntu 22.04 server with the assistance of Shape.host. You can now start utilizing OpenEMR to streamline your health business operations.

Support ❤️
If you have enjoyed my content and code, do support me by buying a couple of coffees. This will enable me to dedicate more time to research and create new content. Cheers!
Share this Article
Share this article with your network to help others!
What's your Feedback?
Do let me know your thoughts around this article.