在Linux中使用dnf或yum安装LNMP环境
更新yum
yum update
更新yum的软件仓库缓存
yum makecache
检查yum仓库中可用的php版本
yum list available --showduplicates|grep "php"
安装PHP依赖的软件
yum -y install libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel automake libtool pcre-devel sqlite-devel
yum=dnf,yum是老版本,现在从8开始,都叫dnf了。
因为yum默认的软件仓库里的软件版本比较低,所以先安装remi软件仓库。
阿里Alibaba Cloud Linux release 3安装remi
rpm -ivh --nodeps https://rpms.remirepo.net/enterprise/remi-release-8.rpm
在Rocky Linux 9系统中安装remi
rpm -ivh --nodeps https://rpms.remirepo.net/enterprise/remi-release-9.rpm
查看有哪些redis版本的模块可以使用
dnf module list redis
启用redis的7.2版本模块
dnf module enable redis:remi-7.2
安装的redis 7.2版本
dnf install -y redis
启用PHP的8.3模型
dnf module enable php:remi-8.3
安装PHP,同时安装redis扩展
dnf install -y php php-redis
如果安装PHP的时候没有安装redis扩展,使用这条命令安装
dnf install php-redis php-curl php-dom php-exif php-fileinfo php-cli php-fpm php-mysqlnd php-zip php-gd php-hash php-json php-mcrypt php-pear php-bcmath php-mbstring php-mysqli php-openssl php-pcre php-xml libsodium
安装软件仓库,以下依赖的库需要用到
dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm
安装依赖的库
yum install libmcrypt
yum install gd3php
安装PHP
yum -y install php php-curl php-dom php-exif php-fileinfo php-cli php-fpm php-mysqlnd php-zip php-gd php-hash php-json php-mcrypt php-pear php-bcmath php-mbstring php-mysqli php-openssl php-pcre php-xml libsodium
yum install mysql(这句好像没有用,安装之后无法启动,后面执行了下面这句后才能正常启动)
dnf install mysql-server
因为我需要用到Nginx的这三个模块:redis2-nginx-module、lua-nginx-module、ngx_devel_kit,这三个东西是用来操作lua和redis的,必须要手工编译安装Nginx才行,我本想装最新版本的Nginx,而尝试了一天后失败了,后来发现OpenResty里是比较新的Nginx,并且集成了lua和redis的模块,就果断选择了OpenResty。
官方安装教程在这里,是替代Nginx的很好的方案,有专门针对Rocky Linux的安装教程,其实与红帽系统是一样的:
查看有哪些nginx版本的模块
dnf module list nginx
dnf module enable nginx:1.22
yum install nginx
检查服务的状态,确认它是否正在运行【active (running)】,和是否已经设置成开机启动【某某服务.service; enabled;】:
systemctl status mysqld
设置开机启动项
systemctl enable nginx
systemctl enable openresty
systemctl enable mysqld
systemctl enable redis
systemctl enable php-fpm
重新加载 systemd 配置
systemctl daemon-reload
设置MySQL密码
MySQL8安装之后是没有密码的,可以在本地无密码登录
选择数据库
USE mysql;
设置root用户密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass1!';
创建远程连接的用户
CREATE USER 'username1'@'%' IDENTIFIED with mysql_native_password BY 'MyNewPass2!';
赋予所有权限
GRANT ALL PRIVILEGES ON *.* to 'username1'@'%' WITH GRANT OPTION;
创建wordpress的用户
CREATE USER 'username2'@'localhost' IDENTIFIED with mysql_native_password BY 'MyNewPass3!';
赋予所有权限
GRANT ALL PRIVILEGES ON *.* to 'username2'@'localhost' WITH GRANT OPTION;
刷新权限
FLUSH PRIVILEGES;
删除用户
DROP USER 'username'@'host';
设置redis密码
打开配置文件:
vim /etc/redis/redis.conf
以下项修改成:
允许远程连接:bind 0.0.0.0
受保护模式关闭:protected-mode no
设置密码:requirepass MyNewPassword
修改php-fpm的配置
vim /etc/php-fpm.d/www.conf
把运行用户和监听用户都改成nginx,与nginx的运行用户一致,要不然会页面显示“File not found.”,nginx错误日志中会显示连接不上sock文件
user = nginx
group = nginx
listen.owner = nginx
listen.group = nginx
修改nginx默认配置文件(以下Nginx配置方式适合dnf安装的Nginx,OpenResty默认的安装目录在:/usr/local/openresty/nginx/)
vim /etc/nginx/nginx.conf
将下面一句调整位置,放在server{...}的后面。
include /etc/nginx/conf.d/*.conf;
不允许使用IP访问,在server{...}中加一行return 403;
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen 80;
listen [::]:80;
listen 443 default_server;
server_name _;
return 403;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /404.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
记得重启php-fpm
systemctl restart php-fpm
在nginx中添加一个虚拟主机
vim /etc/nginx/conf.d/yiluphp.com.conf
内容如下
server {
#设置监听端口
listen 80;
#在这里设置你的访问域名
server_name www.yiluphp.com yiluphp.cn;
#设置nginx访问日志的保存地址,需要确保目录和文件具有读写的权限
access_log /var/log/nginx/www.yiluphp.com-access.log main;
#设置nginx错误日志的保存地址,需要确保目录和文件具有读写的权限
error_log /var/log/nginx/www.yiluphp.com-error.log;
#return 301 https://www.yiluphp.com$request_uri;
set $web_root /data/web/www.yiluphp.com;
root $web_root;
location / {
#设置默认首页,默认首页使用静态文件,提高访问速度
index index.php;
# 如果在目录中找不到真实存在的文件,把请求分发至index.php
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
#fastcgi_pass 127.0.0.1:9001;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
fastcgi_intercept_errors on;
#设置404页面路径
#error_page 404 /error/404.html;
}
记得重启nginx
systemctl restart nginx
如果访问页面报403或者file not find,很可能是SELinux的策略限制。
SELinux(Security-Enhanced Linux)是一个用于提供访问控制安全策略的Linux内核安全模块。要检查和修改SELinux的状态和策略,您可以使用一系列的命令行工具和配置文件。以下是一些基本的步骤和命令:
### 检查SELinux的状态
1. **查看SELinux当前的运行模式**:
getenforce
这个命令会显示SELinux的当前模式,可能是 `Enforcing`(强制模式)或 `Permissive`(宽容模式)。
2. **查看SELinux的全局策略设置**:
cat /etc/selinux/config
这个文件包含了SELinux的全局配置选项,包括SELinux的模式和其他启动参数。
### 修改SELinux的策略
1. **临时更改SELinux模式**:
- 要将SELinux设置为宽容模式,运行:
setenforce 0
这将使SELinux在当前会话中不强制执行策略,但仍会记录违反策略的行为。
- 要将SELinux恢复为强制模式,运行:
setenforce 1
这将重新启用SELinux的强制模式。
请注意,这些更改只在当前会话中有效。要使更改永久生效,您需要编辑 `/etc/selinux/config` 文件。
2. **永久更改SELinux模式**:
打开 `/etc/selinux/config` 文件进行编辑:
sudo vim /etc/selinux/config
找到 `SELINUX=` 这一行,并根据需要将其更改为 `enforcing` 或 `permissive`。保存并关闭文件后,更改将在使用 `reboot` 命令重启系统后生效。
3. **管理SELinux策略**:
SELinux策略通常由一系列预定义的策略包管理,您可以使用 `semodule` 命令来管理这些策略包:
- 安装一个新的策略包:
sudo semodule -i /path/to/module.pp
- 卸载策略包:
sudo semodule -r module_name
- 列出所有已安装的策略包:
semodule -l
### 查看SELinux的日志
SELinux的日志通常存储在 `/var/log/audit/audit.log` 文件中。您可以使用 `ausearch` 命令来搜索和过滤日志中的事件:
sudo ausearch -m avc -ts recent
这个命令将搜索最近的 AVC(Access Vector Cache)日志条目,这些条目记录了SELinux拒绝的操作。
### 重要提示
- 在更改SELinux的配置之前,请确保您了解这些更改的影响。错误的配置可能会导致系统安全问题或服务不可用。
- 在生产环境中,建议在更改任何配置之前备份当前的SELinux策略和配置。
- 如果您不熟悉SELinux,建议在进行更改之前咨询经验丰富的系统管理员或安全专家。