概览
最近有些服务需要测试,想起近几年来腾讯云服务器还一直会有些优惠活动,所以就从腾讯云买了一台轻量应用服务器,系统选择搭载 CentOS 8.2 x64,以下即是在该基础上搭建 LNMP 环境的备忘录,方便自己后续以及有需要的同学用以参考。
当前使用 LNMP 组合是主流的 Web 环境之一,其中包括 Linux+Nginx+MariaDB+PHP,相对在此之前的 LAMP 组合,Apache 替换为 Nginx,MySQL 替换为 MariaDB。
当然现在 Apache 也优化的非常不错,只是何星星个人因为从一入手 Web 环境就是用的 LNMP 组合,所谓先入为主,我就一直没换过,经验也是以 Nginx 方案积累;而 MySQL 的原因大家都知道的,因为 Oracle 收购的缘故,原作者在 MySQL 的基础上再升级开发版本为 MariaDB,所以使用者可以从 MySQL 无缝切换为 MariaDB,以下即使何星星个人再一次完整实践 LNMP 的全过程,虽然前面有很多次的类似实践,但这次对于何星星个人来说,又是一次非常超越此前的提升。
更新 Linux
配置前先更新所有软件和系统版本以及内核版本:yum update
yum update
运行以上命令以更好的提升系统运行状态和支持新发布的特性。
安装 Nginx
根据 Nginx 官网的配置手册,CentOS 的 “安装先决条件” 配置如下:
1. 安装 utils;
yum install yum-utils
2. 设置 yum 存储库;
切换到以下目录:
cd /etc/yum.repos.d
创建 nginx.repo 文件:
vi nginx.repo
并在 nginx.repo 文件里粘贴以下内容:
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
注意:如果你当前的系统不是标准的 RHEL 发行版本,请手工改为当前实际发行版本号。
3. 执行安装 Nginx 命令;
yum install nginx
nginx stable repo 36 kB/s | 35 kB 00:00
Dependencies resolved.
====================================================================================================================
Package Architecture Version Repository Size
====================================================================================================================
Installing:
nginx x86_64 1:1.20.2-1.el8.ngx nginx-stable 820 k
Transaction Summary
====================================================================================================================
Install 1 Package
Total download size: 820 k
Installed size: 2.8 M
Is this ok [y/N]: y
Downloading Packages:
nginx-1.20.2-1.el8.ngx.x86_64.rpm 143 kB/s | 820 kB 00:05
--------------------------------------------------------------------------------------------------------------------
Total 143 kB/s | 820 kB 00:05
nginx stable repo 2.2 kB/s | 1.5 kB 00:00
Importing GPG key 0x7BD9BF62:
Userid : "nginx signing key <signing-key@nginx.com>"
Fingerprint: 573B FD6B 3D8F BC64 1079 A6AB ABF5 BD82 7BD9 BF62
From : https://nginx.org/keys/nginx_signing.key
Is this ok [y/N]: y
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Running scriptlet: nginx-1:1.20.2-1.el8.ngx.x86_64 1/1
Installing : nginx-1:1.20.2-1.el8.ngx.x86_64 1/1
Running scriptlet: nginx-1:1.20.2-1.el8.ngx.x86_64 1/1
----------------------------------------------------------------------
Thanks for using nginx!
Please find the official documentation for nginx here:
* https://nginx.org/en/docs/
Please subscribe to nginx-announce mailing list to get
the most important news about nginx:
* https://nginx.org/en/support.html
Commercial subscriptions for nginx are available on:
* https://nginx.com/products/
----------------------------------------------------------------------
Verifying : nginx-1:1.20.2-1.el8.ngx.x86_64 1/1
Installed:
nginx-1:1.20.2-1.el8.ngx.x86_64
Complete!
4. 查看 Nginx 版本;
nginx -v
nginx version: nginx/1.20.1
5. 启动 Nginx 服务;
service nginx start
Redirecting to /bin/systemctl start nginx.service
6.Nginx 的 WEB 服务文件在以下路径:
pwd
/usr/share/nginx/html
7. 访问服务器的 IP 或绑定的域名即可打开 Nginx 的欢迎页面;
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
8. 配置 Nginx 开机启动;
查询当前配置状态:
当前开机启动状态为禁用
systemctl list-unit-files | grep nginx
nginx-debug.service disabled
nginx.service disabled
设置 Nginx 及相关服务为开机启动:
systemctl enable nginx nginx-debug
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
Created symlink /etc/systemd/system/multi-user.target.wants/nginx-debug.service → /usr/lib/systemd/system/nginx-debug.service.
确认开启状态:
当前开机启动状态为启用
systemctl list-unit-files | grep nginx
nginx-debug.service enabled
nginx.service enabled
至此,即可通过访问服务器 IP 或域名来显示网站的默认页。
安装 MariaDB
1. 卸载前置程序;
卸载系统中可能已经存在的 MySQL 和其相关程序及依赖服务
yum remove mysql* -y
2. 安装 MariaDB 和 MariaDB 服务器;
yum install mariadb mariadb-server -y
3. 启动 MariaDB 数据库服务;
systemctl start mariadb.service
4. 将 MariaDB 数据库服务加入开机启动项;
systemctl enable mariadb
5. 设置数据库密码;
在 MariaDB 数据库是空密码的场景下
mysqladmin -uroot password "newpasswd01"
6. 重启 MariaDB 数据库服务以生效改动;
systemctl restart mariadb.service
7. 登录 MariaDB 数据库查看版本;
mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
关于 MariaDB 的相关详细实战请跳转至:https://hexingxing.cn/mariadb-mysql-database-combat。
安装 PHP
1. 配置 epel-release;
yum install epel-release
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
os | 3.6 kB 00:00:00
updates | 2.9 kB 00:00:00
Resolving Dependencies
There are unfinished transactions remaining. You might consider running yum-complete-transaction, or "yum-complete-transaction --cleanup-only" and "yum history redo last", first to finish them. If those don't work you'll have to try removing/installing packages by hand (maybe package-cleanup can help).
--> Running transaction check
---> Package epel-release.noarch 0:7-14 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==================================================================================================================
Package Arch Version Repository Size
==================================================================================================================
Installing:
epel-release noarch 7-14 epel 15 k
Transaction Summary
==================================================================================================================
Install 1 Package
Total download size: 15 k
Installed size: 25 k
Is this ok [y/d/N]: y
Downloading packages:
epel-release-7-14.noarch.rpm | 15 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
Installing : epel-release-7-14.noarch 1/1
Verifying : epel-release-7-14.noarch 1/1
Installed:
epel-release.noarch 0:7-14
Complete!
2. 安装 epel-release rpm;
rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
Retrieving https://mirror.webtatic.com/yum/el7/epel-release.rpm
warning: /var/tmp/rpm-tmp.zAzpRt: Header V4 RSA/SHA1 Signature, key ID 62e74ca5: NOKEY
Preparing... ################################# [100%]
package epel-release-7-14.noarch (which is newer than epel-release-7-5.noarch) is already installed
2. 安装包 webtatic-release rpm;
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
Retrieving https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
warning: /var/tmp/rpm-tmp.yv8wmd: Header V4 RSA/SHA1 Signature, key ID 62e74ca5: NOKEY
Preparing... ################################# [100%]
package webtatic-release-7-3.noarch is already installed
3. 安装 PHP7.2 及插件;
yum install php72w php72w-opcache php72w-xml php72w-gd php72w-devel php72w-mysqlnd php72w-intl php72w-mbstring php72w-pear php72w-pdo php72w-fpm
Loaded plugins: fastestmirror, langpacks
Repository epel is listed more than once in the configuration
Loading mirror speeds from cached hostfile
* webtatic: us-east.repo.webtatic.com
Resolving Dependencies
There are unfinished transactions remaining. You might consider running yum-complete-transaction, or "yum-complete-transaction --cleanup-only" and "yum history redo last", first to finish them. If those don't work you'll have to try removing/installing packages by hand (maybe package-cleanup can help).
--> Running transaction check
---> Package mod_php72w.x86_64 0:7.2.34-1.w7 will be installed
--> Processing Dependency: libargon2.so.0()(64bit) for package: mod_php72w-7.2.34-1.w7.x86_64
---> Package php72w-devel.x86_64 0:7.2.34-1.w7 will be installed
--> Processing Dependency: php72w-cli(x86-64) = 7.2.34-1.w7 for package: php72w-devel-7.2.34-1.w7.x86_64
---> Package php72w-fpm.x86_64 0:7.2.34-1.w7 will be installed
--> Processing Dependency: php72w-common(x86-64) = 7.2.34-1.w7 for package: php72w-fpm-7.2.34-1.w7.x86_64
---> Package php72w-gd.x86_64 0:7.2.34-1.w7 will be installed
--> Processing Dependency: libXpm.so.4()(64bit) for package: php72w-gd-7.2.34-1.w7.x86_64
---> Package php72w-intl.x86_64 0:7.2.34-1.w7 will be installed
---> Package php72w-mbstring.x86_64 0:7.2.34-1.w7 will be installed
---> Package php72w-mysqlnd.x86_64 0:7.2.34-1.w7 will be installed
---> Package php72w-opcache.x86_64 0:7.2.34-1.w7 will be installed
---> Package php72w-pdo.x86_64 0:7.2.34-1.w7 will be installed
---> Package php72w-pear.noarch 1:1.10.12-1.w7 will be installed
--> Processing Dependency: php72w-posix for package: 1:php72w-pear-1.10.12-1.w7.noarch
---> Package php72w-xml.x86_64 0:7.2.34-1.w7 will be installed
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.24)(64bit) for package: php72w-xml-7.2.34-1.w7.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.22)(64bit) for package: php72w-xml-7.2.34-1.w7.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.18)(64bit) for package: php72w-xml-7.2.34-1.w7.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.13)(64bit) for package: php72w-xml-7.2.34-1.w7.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.11)(64bit) for package: php72w-xml-7.2.34-1.w7.x86_64
--> Processing Dependency: libxslt.so.1()(64bit) for package: php72w-xml-7.2.34-1.w7.x86_64
--> Processing Dependency: libexslt.so.0()(64bit) for package: php72w-xml-7.2.34-1.w7.x86_64
--> Running transaction check
---> Package libXpm.x86_64 0:3.5.12-1.el7 will be installed
---> Package libargon2.x86_64 0:20161029-3.el7 will be installed
---> Package libxslt.x86_64 0:1.1.28-6.el7 will be installed
---> Package php72w-cli.x86_64 0:7.2.34-1.w7 will be installed
---> Package php72w-common.x86_64 0:7.2.34-1.w7 will be installed
---> Package php72w-process.x86_64 0:7.2.34-1.w7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==================================================================================================================
Package Arch Version Repository Size
==================================================================================================================
Installing:
mod_php72w x86_64 7.2.34-1.w7 webtatic 3.1 M
php72w-devel x86_64 7.2.34-1.w7 webtatic 2.8 M
php72w-fpm x86_64 7.2.34-1.w7 webtatic 1.6 M
php72w-gd x86_64 7.2.34-1.w7 webtatic 142 k
php72w-intl x86_64 7.2.34-1.w7 webtatic 184 k
php72w-mbstring x86_64 7.2.34-1.w7 webtatic 587 k
php72w-mysqlnd x86_64 7.2.34-1.w7 webtatic 198 k
php72w-opcache x86_64 7.2.34-1.w7 webtatic 247 k
php72w-pdo x86_64 7.2.34-1.w7 webtatic 90 k
php72w-pear noarch 1:1.10.12-1.w7 webtatic 343 k
php72w-xml x86_64 7.2.34-1.w7 webtatic 123 k
Installing for dependencies:
libXpm x86_64 3.5.12-1.el7 os 55 k
libargon2 x86_64 20161029-3.el7 epel 23 k
libxslt x86_64 1.1.28-6.el7 os 242 k
php72w-cli x86_64 7.2.34-1.w7 webtatic 3.1 M
php72w-common x86_64 7.2.34-1.w7 webtatic 1.3 M
php72w-process x86_64 7.2.34-1.w7 webtatic 40 k
Transaction Summary
==================================================================================================================
Install 11 Packages (+6 Dependent packages)
Total download size: 14 M
Installed size: 64 M
Is this ok [y/d/N]: y
Downloading packages:
(1/17): libXpm-3.5.12-1.el7.x86_64.rpm | 55 kB 00:00:00
(2/17): libargon2-20161029-3.el7.x86_64.rpm | 23 kB 00:00:00
(3/17): libxslt-1.1.28-6.el7.x86_64.rpm | 242 kB 00:00:00
warning: /var/cache/yum/x86_64/7/webtatic/packages/php72w-devel-7.2.34-1.w7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 62e74ca5: NOKEY
Public key for php72w-devel-7.2.34-1.w7.x86_64.rpm is not installed
(4/17): php72w-devel-7.2.34-1.w7.x86_64.rpm | 2.8 MB 00:00:36
(5/17): php72w-common-7.2.34-1.w7.x86_64.rpm | 1.3 MB 00:01:01
(6/17): php72w-gd-7.2.34-1.w7.x86_64.rpm | 142 kB 00:00:01
(7/17): php72w-intl-7.2.34-1.w7.x86_64.rpm | 184 kB 00:00:01
(8/17): php72w-mbstring-7.2.34-1.w7.x86_64.rpm | 587 kB 00:00:03
(9/17): php72w-mysqlnd-7.2.34-1.w7.x86_64.rpm | 198 kB 00:00:01
(10/17): php72w-opcache-7.2.34-1.w7.x86_64.rpm | 247 kB 00:00:01
(11/17): php72w-pdo-7.2.34-1.w7.x86_64.rpm | 90 kB 00:00:00
(12/17): php72w-pear-1.10.12-1.w7.noarch.rpm | 343 kB 00:00:01
(13/17): php72w-process-7.2.34-1.w7.x86_64.rpm | 40 kB 00:00:00
(14/17): php72w-xml-7.2.34-1.w7.x86_64.rpm | 123 kB 00:00:00
(15/17): php72w-cli-7.2.34-1.w7.x86_64.rpm | 3.1 MB 00:02:14
(16/17): mod_php72w-7.2.34-1.w7.x86_64.rpm | 3.1 MB 00:02:23
(17/17): php72w-fpm-7.2.34-1.w7.x86_64.rpm | 1.6 MB 00:01:25
------------------------------------------------------------------------------------------------------------------
Total 98 kB/s | 14 MB 00:02:27
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-webtatic-el7
Importing GPG key 0x62E74CA5:
Userid : "Webtatic EL7 <rpms@webtatic.com>"
Fingerprint: 830d b159 6d9b 9b01 99dc 24a3 e87f d236 62e7 4ca5
Package : webtatic-release-7-3.noarch (installed)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-webtatic-el7
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
Installing : php72w-common-7.2.34-1.w7.x86_64 1/17
Installing : libargon2-20161029-3.el7.x86_64 2/17
Installing : php72w-cli-7.2.34-1.w7.x86_64 3/17
Installing : php72w-process-7.2.34-1.w7.x86_64 4/17
Installing : php72w-pdo-7.2.34-1.w7.x86_64 5/17
Installing : libxslt-1.1.28-6.el7.x86_64 6/17
Installing : php72w-xml-7.2.34-1.w7.x86_64 7/17
Installing : libXpm-3.5.12-1.el7.x86_64 8/17
Installing : php72w-gd-7.2.34-1.w7.x86_64 9/17
Installing : 1:php72w-pear-1.10.12-1.w7.noarch 10/17
Installing : php72w-mysqlnd-7.2.34-1.w7.x86_64 11/17
Installing : php72w-devel-7.2.34-1.w7.x86_64 12/17
Installing : php72w-fpm-7.2.34-1.w7.x86_64 13/17
Installing : mod_php72w-7.2.34-1.w7.x86_64 14/17
Installing : php72w-opcache-7.2.34-1.w7.x86_64 15/17
Installing : php72w-mbstring-7.2.34-1.w7.x86_64 16/17
Installing : php72w-intl-7.2.34-1.w7.x86_64 17/17
Verifying : libXpm-3.5.12-1.el7.x86_64 1/17
Verifying : php72w-mysqlnd-7.2.34-1.w7.x86_64 2/17
Verifying : php72w-fpm-7.2.34-1.w7.x86_64 3/17
Verifying : libxslt-1.1.28-6.el7.x86_64 4/17
Verifying : php72w-opcache-7.2.34-1.w7.x86_64 5/17
Verifying : php72w-process-7.2.34-1.w7.x86_64 6/17
Verifying : 1:php72w-pear-1.10.12-1.w7.noarch 7/17
Verifying : php72w-mbstring-7.2.34-1.w7.x86_64 8/17
Verifying : mod_php72w-7.2.34-1.w7.x86_64 9/17
Verifying : php72w-cli-7.2.34-1.w7.x86_64 10/17
Verifying : php72w-intl-7.2.34-1.w7.x86_64 11/17
Verifying : php72w-devel-7.2.34-1.w7.x86_64 12/17
Verifying : php72w-xml-7.2.34-1.w7.x86_64 13/17
Verifying : libargon2-20161029-3.el7.x86_64 14/17
Verifying : php72w-gd-7.2.34-1.w7.x86_64 15/17
Verifying : php72w-pdo-7.2.34-1.w7.x86_64 16/17
Verifying : php72w-common-7.2.34-1.w7.x86_64 17/17
Installed:
mod_php72w.x86_64 0:7.2.34-1.w7 php72w-devel.x86_64 0:7.2.34-1.w7 php72w-fpm.x86_64 0:7.2.34-1.w7
php72w-gd.x86_64 0:7.2.34-1.w7 php72w-intl.x86_64 0:7.2.34-1.w7 php72w-mbstring.x86_64 0:7.2.34-1.w7
php72w-mysqlnd.x86_64 0:7.2.34-1.w7 php72w-opcache.x86_64 0:7.2.34-1.w7 php72w-pdo.x86_64 0:7.2.34-1.w7
php72w-pear.noarch 1:1.10.12-1.w7 php72w-xml.x86_64 0:7.2.34-1.w7
Dependency Installed:
libXpm.x86_64 0:3.5.12-1.el7 libargon2.x86_64 0:20161029-3.el7 libxslt.x86_64 0:1.1.28-6.el7
php72w-cli.x86_64 0:7.2.34-1.w7 php72w-common.x86_64 0:7.2.34-1.w7 php72w-process.x86_64 0:7.2.34-1.w7
Complete!
4. 启动 PHP;
systemctl start php-fpm
5. 配置 PHP 开机启动;
systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
6. 查看 PHP 版本;
php -v
PHP 7.2.34 (cli) (built: Oct 1 2020 13:37:37) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.34, Copyright (c) 1999-2018, by Zend Technologies
PHP 安装参考文档:https://webtatic.com/packages/php72/。
配置 PHP-FPM
1. 配置 Nginx 模板以支持 PHP 文件解析;
vi /etc/nginx/conf.d/default.conf
# The default server
# conf_file in /etc/nginx/conf.d/
server {
listen 80;
# listen 80 default_server;
# listen [::]:80 default_server;
server_name localhost;
root /usr/share/nginx/html;
#root /etc/nginx/html;
#rewrite ^(.*)$ https://$host$1 permanent;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
index index.html index.htm index.php;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
}
该文件可能系统默认没有,可以自行创建,以上配置文件内容需要注意以下字段:
root /usr/share/nginx/html; #html 文件所在路径,不同发行版本可能不同;
location / { index index.html index.htm index.php; } #默认的参数没有 index.php,需要手动添加;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name; #php 文件所在路径,一般和 html 文件路径相同;
2. 生成 PHP 信息页面;
echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/index.php
3. 完成以上配置后重启 Nginx 和 PHP 服务;
systemctl restart nginx && systemctl restart php-fpm
至此,访问服务器 IP 或绑定的域名追加/index.php 即可访问到 PHP 版本信息页面。
1 条评论
CentOS 安装 phpMyAdmin – 何星星 · 2022年3月19日 20:52
[…]《CentOS 8 搭建 LNMP 环境全面详细解析(Linux+Nginx+MariaDB+PHP)》的基础下,本次何星星来安装用于管理服务器数据库的 Web […]