一、创建SSL的nginx网站
1.1 安装nginx
[root@server10 ~]# dnf -y install nginx 安装nginx
1.2 备份nginx的配置文件
[root@server10 ~]# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak 备份nginx的配置文件
[root@server10 ~]#
[root@server10 ~]# systemctl enable –now nginx 启动nginx并设置nginx开机启动
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@server10 ~]# systemctl enable –now firewalld 启动防火墙
Created symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service → /usr/lib/systemd/system/firewalld.service.
Created symlink /etc/systemd/system/multi-user.target.wants/firewalld.service → /usr/lib/systemd/system/firewalld.service.
[root@server10 ~]#
[root@server10 ~]# firewall-cmd –add-service=http 在防火墙里添加http(超文本链接协议)服务
success
[root@server10 ~]# firewall-cmd –add-service=https 在防火墙里添加https服务(加密安全的超文本链接协议)
success
[root@server10 ~]# firewall-cmd –runtime-to-permanent 在防火墙里永久添加运行时
success
[root@server10 ~]# dnf -y install openssl-devel 安装ssl(安全套阶层协议)
[root@server10 ~]# mkdir /etc/ssl/private 创建私有目录
[root@server10 ~]#
[root@server10 ~]# chmod 700 /etc/ssl/private/ 将私有目录添加700权限
[root@server10 ~]#
[root@server10 ~]# openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/nginx-selfsigned.key -out /etc/ssl/certs/nginx-selfsigned.crt
用openssl生成私有密钥和证书,
—–
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:BJ
Locality Name (eg, city) [Default City]:BJ
Organization Name (eg, company) [Default Company Ltd]:www.itcloud.net 公司名称:www.itcloud.net
Organizational Unit Name (eg, section) []:IT 部门:IT
Common Name (eg, your name or your server’s hostname) []:lee 名字
Email Address []:2823629811@qq.com 邮箱
[root@server10 ~]# openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048 生成密钥交换文件
[root@server10 ~]# vim /etc/nginx/conf.d/ssl.conf 创建ssl的虚拟主机的配置文件
server { 定义服务器
listen 443 http2 ssl; 监听ipv4网站https的443端口
listen [::]:443 http2 ssl; 监听ipv6网站https的443端口
server_name 192.168.7.10; 服务器的名称可以写域名或IP
ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt; 指定证书文件的位置
ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key; 指定密钥文件的位置
ssl_dhparam /etc/ssl/certs/dhparam.pem; 指定密钥交换文件的位置
root /usr/share/nginx/html; 指定网站的具体位置
location / {
}
error_page 404 /404.html; 定义错误页面404的位置(404表示:服务器找不到请求的网页)
location = /404.html {
}
error_page 500 502 503 504 /50x.html; 定义 500 502 503 504 /50x.html(50X))错误文件的位置
location = /50x.html {
}
}
[root@server10 ~]# vim /etc/nginx/default.d/ssl-redirect.conf 创建ssl重定向文件
return 301 https://$host$request_uri/; 网站由http重定向到https
301 表示请求的页面永久移动到新位置
[root@server10 ~]# nginx -t 检查nginx配置文件的语法
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@server10 ~]#
[root@server10 ~]# systemctl restart nginx 重启nginx
二、安装mariadb数据库
[root@server10 ~]# dnf -y install mariadb-server mariadb 安装数据库的服务端和客户端
[root@server10 ~]# systemctl enable --now mariadb 启动数据库并设置开机启动
[root@server10 ~]# mysql_secure_installation 数据库的安全设置
[root@server10 ~]# firewall-cmd --add-service=mysql 防火墙里添加mysql服务
success
[root@server10 ~]# firewall-cmd --runtime-to-permanent 在防火墙里永久添加运行时
success
[root@server10 ~]# dnf -y install https://rpms.remirepo.net/enterprise/remi-release-9.rpm 安装php的数据源
[root@server10 ~]# dnf module list php 查看php单元列表
Extra Packages for Enterprise Linux 9 - x86_64 3.6 MB/s | 23 MB 00:06
Extra Packages for Enterprise Linux 9 openh264 (From Cisco) - x86 1.0 kB/s | 2.5 kB 00:02
Remi's Modular repository for Enterprise Linux 9 - x86_64 176 kB/s | 798 kB 00:04
Safe Remi's RPM repository for Enterprise Linux 9 - x86_64 159 kB/s | 1.2 MB 00:07
Rocky Linux 9 - AppStream
Name Stream Profiles Summary
php 8.1 common [d], devel, minimal PHP scripting language
php 8.2 common [d], devel, minimal PHP scripting language
Remi's Modular repository for Enterprise Linux 9 - x86_64
Name Stream Profiles Summary
php remi-7.4 common [d], devel, minimal PHP scripting language
php remi-8.0 common [d], devel, minimal PHP scripting language
php remi-8.1 common [d], devel, minimal PHP scripting language
php remi-8.2 common [d], devel, minimal PHP scripting language
php remi-8.3 common [d], devel, minimal PHP scripting language
php remi-8.4 common [d], devel, minimal PHP scripting language
Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled
[root@server10 ~]#
[root@server10 ~]# dnf module enable php:remi-8.4 开启php8.4的单元
Last metadata expiration check: 0:00:30 ago on Mon 17 Mar 2025 10:56:44 AM CST.
Dependencies resolved.
==================================================================================================
Package Architecture Version Repository Size
==================================================================================================
Enabling module streams:
php remi-8.4
Transaction Summary
==================================================================================================
Is this ok [y/N]: y
Complete!
[root@server10 ~]# dnf -y install php-{common,gmp,fpm,curl,intl,pdo,mbstring,gd,xml,cli,zip,mysqli} 安装php及其插件
[root@server10 ~]# vim /etc/php-fpm.d/www.conf 编辑php引擎的配置文件(这个和阿帕奇的不同点)
24 user = nginx 用户为nginx
26 group = nginx 组为nginx
三、安装php引擎
[root@server10 ~]# systemctl enable php-fpm 设置php引擎开机启动
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
[root@server10 ~]#
[root@server10 ~]# systemctl start php-fpm 启动php引擎
[root@server10 ~]# vim /usr/share/nginx/html/info.php 创建php的测试页面
<?php
phpinfo();
?>
四、重启nginx、重启php引擎
[root@server10 ~]# systemctl restart nginx 重启nginx
[root@server10 ~]# systemctl restart php-fpm 重启php引擎
五、编辑nginx ssl的配置文件
[root@server10 ~]# vim /etc/nginx/conf.d/ssl.conf 编辑nginx ssl的配置文件
root /usr/share/nginx/html;
# location / {
# }
index index.php index.html; nginx首页的格式为index.php index.html
try_files $uri $uri/ /index.php?$args; 通过try files实现静态文件优先,动态请求转发
access_log /var/log/nginx/example_com.access; 定义日志文件的位置
error_log /var/log/nginx/example_com.error; 定义错误日志文件的位置
location ~ \.php$ { 匹配以.php结尾请求的路径,适用于处理php脚本
include fastcgi_params; 引入nginx默认的fatcgi参数配置
fastcgi_pass unix:/run/php-fpm/www.sock; 指定php引擎进程监听的unix套阶字路径
fastcgi_split_path_info ^(.+\.php)(/.+)$; 分离请求uri中的php脚本名和路径信息
fastcgi_index index.php; 构造php引擎解析脚本的结对路径
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 当请求路径以`/`结尾时(如`/index.php/`),自动附加`index.php`作为默认入口文件。
}
六、检查nginx配置文件的语法、重启nginx
[root@server10 ~]# nginx -t 检查nginx配置文件的语法
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@server10 ~]#
[root@server10 ~]# systemctl restart nginx 重启nginx