本教程适用于以下操作系统版本
- Ubuntu 24.04(Noble Numbat)
- Ubuntu 22.04(Jammy Jellyfish)
本页内容
- 准备工作
- 第 1 步 - 配置防火墙
- 第 2 步 - 安装 Nginx
- 第 3 步 - 安装 MySQL
- 第 4 步 - 配置 MySQL
- 第 5 步 - 安装 PHP 及其扩展
- 第 6 步 - 安装 SSL
- 第 7 步 - 下载 OpenEMR
- 第 8 步 - 配置 PHP-FPM
- 第 9 步 - 配置 Nginx
- 第 10 步 - 安装 OpenEMR
- 结语
OpenEMR 是一款开源的电子健康档案与医疗诊所管理工具。它获得了美国国家卫生信息技术协调办公室(ONC)的认证,具备集成健康档案、诊所管理、排程、电子账单、国际化、免费支持等众多功能。它可以记录患者人口统计信息、安排患者预约、维护包含化验报告、用药和诊疗手续的极为详细的健康档案、跟踪处方、协助医疗账单处理、生成详细报告,并支持多语言。
在本教程中,您将学习如何在运行 Ubuntu 22.04 的服务器上安装 OpenEMR 软件。
准备工作
- 一台运行 Ubuntu 20.04 的服务器。
- 一个非 root 的 sudo 用户。
- 一个完整的域名(FQDN),例如 openemr.example.com。
- 确保系统已更新。 $ sudo apt update $ sudo apt upgrade
- 系统需要的一些软件包。 $ sudo apt install wget curl nano ufw software-properties-common dirmngr apt-transport-https gnupg2 ca-certificates lsb-release ubuntu-keyring unzip -y 其中部分软件包可能已在您的系统上安装。
一台运行 Ubuntu 20.04 的服务器。
一个非 root 的 sudo 用户。
一个完整的域名(FQDN),例如 openemr.example.com。
确保系统已更新。
$ sudo apt update
$ sudo apt upgrade
系统需要的一些软件包。
$ sudo apt install wget curl nano ufw software-properties-common dirmngr apt-transport-https gnupg2 ca-certificates lsb-release ubuntu-keyring unzip -y
其中部分软件包可能已在您的系统上安装。
第 1 步 - 配置防火墙
第一步是配置防火墙。Ubuntu 默认自带 ufw(Uncomplicated Firewall,简易防火墙)。
检查防火墙是否正在运行。
$ sudo ufw status
您将看到如下输出。
Status: inactive
允许 SSH 端口,以免启用防火墙时中断当前连接。
$ sudo ufw allow OpenSSH
同时允许 HTTP 和 HTTPS 端口。
$ sudo ufw allow http
$ sudo ufw allow https
启用防火墙
$ sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup
再次检查防火墙状态。
$ sudo ufw status
您应会看到类似的输出。
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
80/tcp ALLOW Anywhere
443 ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
80/tcp (v6) ALLOW Anywhere (v6)
443 (v6) ALLOW Anywhere (v6)
第 2 步 - 安装 Nginx
Ubuntu 自带的 Nginx 版本较旧。要安装最新版本,您需要下载官方的 Nginx 软件源。
导入 Nginx 的签名密钥。
$ curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
添加 Nginx 稳定版的软件源。
$ 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
更新系统软件源。
$ sudo apt update
安装 Nginx。
$ sudo apt install nginx
验证安装。
$ nginx -v
nginx version: nginx/1.24.0
启动 Nginx 服务。
$ sudo systemctl start nginx
第 3 步 - 安装 MySQL
Ubuntu 22.04 自带最新版本的 MySQL。您可以使用一条命令安装它。
$ sudo apt install mysql-server
检查 MySQL 的版本。
$ mysql --version
mysql Ver 8.0.33-0ubuntu0.22.04.2 for Linux on x86_64 ((Ubuntu))
此步骤适用于 MySQL 8.0.28 及以上版本。进入 MySQL Shell。
$ sudo mysql
运行以下命令为 root 用户设置密码。请确保密码包含数字、大写字母、小写字母和特殊字符的组合。
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourPassword12!';
退出 shell。
mysql> exit
运行 MySQL 安全安装脚本。
$ sudo mysql_secure_installation
首先,系统会要求您输入 root 密码,请输入。接下来会询问是否安装 Validate Password Component(密码校验组件),它会检查 MySQL 中密码的强度。按 Y 进行安装。随后会要求您设置密码校验策略的强度级别,请选择 2,因为它是强度最高的级别。
Securing the MySQL server deployment.
Enter password for user root:
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: Y
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Using existing password for root.
Estimated strength of the password: 100
接着,输入 N 拒绝修改 root 密码。同时,输入 Y 以删除匿名用户、禁止 root 远程登录、删除测试数据库并重新加载权限表。
Change the password for root ? ((Press y|Y for Yes, any other key for No) : N
... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
Success.
All done!
第 4 步 - 配置 MySQL
登录 MySQL shell。根据提示输入 root 密码。
$ sudo mysql -u root -p
创建一个示例数据库。
mysql> CREATE DATABASE openemr;
创建一个 SQL 用户账户。
mysql> CREATE USER 'openemruser'@'localhost' IDENTIFIED BY 'Your_password2';
将该数据库的所有权限授予该用户。
mysql> GRANT ALL PRIVILEGES ON openemr.* TO 'openemruser'@'localhost';
刷新用户权限。
mysql> FLUSH PRIVILEGES;
退出 shell。
mysql> exit
第 5 步 - 安装 PHP 及其扩展
Ubuntu 22.04 自带的 PHP 8.1.2 版本稍显陈旧。我们将通过 Ondrej 的 PHP 软件源安装最新的 PHP 8.2 版本。
$ sudo add-apt-repository ppa:ondrej/php
接下来,安装 OpenEMR 所需的 PHP 及其扩展。
$ sudo apt install php8.2-fpm php8.2-mysql php8.2-bcmath php8.2-xml php8.2-zip php8.2-curl php8.2-mbstring php8.2-gd php8.2-tidy php8.2-intl php8.2-cli php8.2-soap imagemagick libtiff-tools php8.2-ldap
验证安装。
$ php --version
PHP 8.2.7 (cli) (built: Jun 8 2023 15:27:40) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.7, Copyright (c) Zend Technologies
with Zend OPcache v8.2.7, Copyright (c), by Zend Technologies
第 6 步 - 安装 SSL
我们需要安装 Certbot 来生成 SSL 证书。您可以通过 Ubuntu 的软件源安装 Certbot,也可以通过 Snapd 工具获取最新版本。我们将使用 Snapd 版本。
Ubuntu 22.04 默认已安装 Snapd。运行以下命令以确保您的 Snapd 版本是最新的。
$ sudo snap install core && sudo snap refresh core
安装 Certbot。
$ sudo snap install --classic certbot
使用以下命令创建指向 /usr/bin 目录的符号链接,以确保 Certbot 命令可以运行。
$ sudo ln -s /snap/bin/certbot /usr/bin/certbot
运行以下命令生成 SSL 证书。
$ sudo certbot certonly --nginx --agree-tos --no-eff-email --staple-ocsp --preferred-challenges http -m
[email protected]
-d openemr.example.com
上述命令会将证书下载到您服务器上的 /etc/letsencrypt/live/openemr.example.com 目录。
生成 Diffie-Hellman 组证书。
$ sudo openssl dhparam -dsaparam -out /etc/ssl/certs/dhparam.pem 4096
检查 Certbot 续期调度服务。
$ sudo systemctl list-timers
您会发现 snap.certbot.renew.service 是已安排在运行的服务之一。
NEXT LEFT LAST PASSED UNIT ACTIVATES
.....
Sun 2023-02-26 06:32:00 UTC 9h left Sat 2023-02-25 18:04:05 UTC 2h 59min ago snap.certbot.renew.timer snap.certbot.renew.service
Sun 2023-02-26 06:43:20 UTC 9h left Sat 2023-02-25 10:49:23 UTC 10h ago apt-daily-upgrade.timer apt-daily-upgrade.service
Sun 2023-02-26 09:00:06 UTC 11h left Sat 2023-02-25 20:58:06 UTC 5min ago apt-daily.timer apt-daily.service
对流程进行一次预演(dry run),以检查 SSL 续期是否正常工作。
$ sudo certbot renew --dry-run
如果没有看到错误,说明一切就绪。您的证书将自动续期。
第 7 步 - 下载 OpenEMR
访问 OpenEMR 下载页面并获取最新版本 OpenEMR 的链接。将 OpenEMR 下载到服务器。
$ wget https://sourceforge.net/projects/openemr/files/OpenEMR%20Current/7.0.1/openemr-7.0.1.tar.gz
解压文件。
$ tar -pxzf openemr-7.0.1.tar.gz
创建 /var/www/html 目录。
$ sudo mkdir /var/wwww/html -p
将解压后的文件移动到 Web 目录。
$ sudo mv openemr-7.0.1 /var/www/html/openemr
将 Web 根目录的权限授予 Nginx 用户。
$ sudo chown -R nginx:nginx /var/www/html/openemr
第 8 步 - 配置 PHP-FPM
打开文件 /etc/php/8.2/fpm/pool.d/www.conf。
$ sudo nano /etc/php/8.2/fpm/pool.d/www.conf
我们需要将 PHP 进程的 Unix 用户/组设置为 nginx。在文件中找到 user=www-data 和 group=www-data 两行,并将其修改为 nginx。
...
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
; RPM: apache user chosen to provide access to the same directories as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
...
在文件中找到 listen.owner = www-data 和 listen.group = www-data 两行,并将其修改为 nginx。
; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions. The owner
; and group can be specified either by name or by their numeric IDs.
; Default Values: user and group are set as the running user
; mode is set to 0660
listen.owner = nginx
listen.group = nginx
按 Ctrl + X 保存文件,并在提示时输入 Y。
将 PHP-FPM 和 PHP-CLI 的执行时间增加到 60 秒。
$ sudo sed -i 's/max_execution_time = 30/max_execution_time = 60/' /etc/php/8.2/fpm/php.ini
将变量 max_input_time 的值设置为 1。
$ sudo sed -i 's/max_input_time = 60/max_input_time = -1/' /etc/php/8.2/fpm/php.ini
将 PHP-FPM 的内存限制从 128MB 提高到 512MB。
$ sudo sed -i 's/memory_limit = 128M/memory_limit = 512M/' /etc/php/8.2/fpm/php.ini
将文件上传大小提高到 30MB。
$ sudo sed -i 's/post_max_size = 8M/post_max_size = 30M/' /etc/php/8.2/fpm/php.ini
$ sudo sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 30M/' /etc/php/8.2/fpm/php.ini
将最大输入变量数增加到 3000。
$ sudo sed -i 's/;max_input_vars = 1000/max_input_vars = 3000/g' /etc/php/8.2/fpm/php.ini
允许从 PHP 的角度使用 LOAD DATA 语句访问本地文件。
$ sudo sed -i 's/;mysqli.allow_local_infile = On/mysqli.allow_local_infile = On/g' /etc/php/8.2/fpm/php.ini
重启 PHP-FPM 服务。
$ sudo systemctl restart php8.2-fpm
将 PHP sessions 目录的所属组改为 Nginx。
$ sudo chgrp -R nginx /var/lib/php/sessions
第 9 步 - 配置 Nginx
创建并打开文件 /etc/nginx/conf.d/openemr.conf 进行编辑。
$ sudo nano /etc/nginx/conf.d/openemr.conf
将以下代码粘贴到其中。
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name openemr.example.com;
access_log /var/log/nginx/openemr.access.log;
error_log /var/log/nginx/openemr.error.log;
# SSL
ssl_certificate /etc/letsencrypt/live/openemr.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/openemr.example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/openemr.example.com/chain.pem;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
ssl_ecdh_curve X25519:prime256v1:secp384r1:secp521r1;
ssl_stapling on;
ssl_stapling_verify on;
ssl_dhparam /etc/ssl/certs/dhparam.pem;
# use https://blog.cloudflare.com/announcing-1111 Cloudfare+Apnic labs, It is free and secure
resolver 1.1.1.1 1.0.0.1 [2606:4700:4700::1111] [2606:4700:4700::1001] valid=300s;
root /var/www/html/openemr;
index index.php;
location / {
try_files $uri $uri/ /index.php;
}
# 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;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
log_not_found off;
access_log off;
}
location ~ /\. {
deny all;
}
}
# enforce HTTPS
server {
listen 80;
listen [::]:80;
server_name openemr.example.com;
return 301 https://$host$request_uri;
}
注意,Nginx 配置中要使用的根目录是 /var/www/html/wallabag/public/。
完成后按 Ctrl + X 保存文件,并在提示时输入 Y。
打开文件 /etc/nginx/nginx.conf 进行编辑。
$ sudo nano /etc/nginx/nginx.conf
在 include /etc/nginx/conf.d/*.conf; 这一行之前添加以下行。
server_names_hash_bucket_size 64;
按 Ctrl + X 保存文件,并在提示时输入 Y。
验证 Nginx 配置文件的语法。
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重启 Nginx 服务。
$ sudo systemctl restart nginx
第 10 步 - 安装 OpenEMR
在浏览器中打开网址 https://openemr.example.com,您将看到以下安装向导界面。

在此页面它会检查文件权限,并确认您是否可以进入第 1 步。如果您看到绿色的 ready 字样,说明可以继续。点击蓝色按钮进入第 1 步。

在下一页,系统会询问您是希望安装程序创建数据库,还是使用已创建的数据库。选择 I have already created the database(我已创建好数据库)选项,然后点击按钮进入第 2 步。

在下一页,填写您之前在第 4 步中配置的数据库凭据,并输入您的管理员账户凭据。请确保用户名长度为 12 个字符或以上,否则会出现错误。您可以在此处启用双因素认证(2FA),但建议在安装完成后再配置。点击按钮创建数据库和用户账户。

下一页将显示安装状态,并向您展示用户名和密码。点击按钮进入第 4 步。

下一页将列出 php.ini 文件中建议的值和当前值。请确保当前值满足要求。出于某种原因,即使您已正确设置,安装程序显示的 max_input_time 和 max_execution_time 变量值仍可能有误。您可以忽略这些错误。您可以使用以下命令验证当前值。

确认无误后,点击按钮进入第 5 步。

下一步列出的是 Apache 服务器设置,由于我们使用的是 Nginx 服务器,因此将其忽略。点击按钮进入下一页。

此处系统会要求您为管理面板选择一个主题。选择 Keep Current(保持当前)选项并点击按钮继续。您可以在管理面板中稍后更改主题,但那样将无法预览其外观。如果是在安装程序页面中选择,您还可以在选定前查看其外观效果。我们将保留默认主题。

最后一页列出了一些关于软件和账户凭据的结束语。点击 Start(开始)按钮打开登录页面。

您会看到一个 OpenEMR 注册弹窗,用于接收其官网的公告。您可以忽略它,直接输入您的凭据并点击 Login(登录)按钮进入仪表盘。

如果您在安装过程中没有配置双因素认证,可以点击右上角的头像图标并选择 MFA Management(MFA 管理)选项进行设置。

在下一页,从下拉菜单中选择 Authentication method(认证方式)并开始配置。

从现在起,您就可以开始使用 OpenEMR 来管理您的医疗业务了。
结语
至此,我们关于在 Ubuntu 22.04 服务器上安装 OpenEMR 的教程就结束了。如果您有任何疑问,请在下方评论区留言。