CentOS7安装最新Nginx、PHP7.4.6、MySql5.7、Redis6.0.3,搭建PHP环境
准备前的工作
- 需要安装 CentOS7 系统,没有安装 lamp、OneinStack、宝塔之类的集成环境
- 执行 yum 更新命令让 yum 保持最新
yum update
- 如果不是 root 用户,请用以下命令切换到 root 用户(输入命令后需要输入 root 用户密码,回车确认)
sudo su
安装 Nginx
- 添加 Nginx 到 yum 源
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
- 安装 Nginx
yum install -y nginx
完成之后,最新的 nginx 已经安装到你系统里了,不过刚安装的 nginx 并不会自动启动,所以你需要手动启动:
systemctl start nginx
输入上面的命令回车后没有任何输入一般都表示成功了,不过保险起见还是查看一下,输入以下命令:
systemctl status nginx
如果显示以下信息,则表示成功:
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since 三 2020-05-27 09:18:34 CST; 1h 5min ago
Docs: http://nginx.org/en/docs/
Main PID: 19684 (nginx)
CGroup: /system.slice/nginx.service
├─19684 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─19690 nginx: worker process
5 月 27 09:18:34 localhost.localdomain systemd[1]: Starting nginx - high performance w….
5 月 27 09:18:34 localhost.localdomain systemd[1]: Started nginx - high performance we….
Hint: Some lines were ellipsized, use -l to show in full.
如果一切进展顺利的话,现在你可以通过你的域名或 IP 来访问你的 Web 页面
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.
如果没有正确显示的话,可能是防火墙没有打开 务 80 端口或者服器规则没有添加 80 端口(阿里云之类的需要添加规则,一般 80 端口是默认打开的)
CentOS7防火墙Firewalld的使用
阿里云服务器规则
Nginx 配置信息
网站文件存放默认目录:/usr/share/nginx/html
网站默认站点配置文件:/etc/nginx/conf.d/default.conf
自定义 Nginx 站点配置文件存放目录:/etc/nginx/conf.d/
Nginx 全局配置文件:/etc/nginx/nginx.conf
开机自启动命令:
systemctl enable nginx
安装 PHP
首先说一下 PHP 7.4 的主要特性包括:预加载、FFI、类属性的类型支持、NULL 合并赋值运算符、弃用 WDDX 扩展、简化匿名函数、新增 mb_str_split 函数、始终可用的 Hash 扩展
- 添加 EPEL 和 REMI 存储库:
yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm # yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
- 启用 PHP 7.4 Remi 存储库:
yum -y install yum-utils # yum-config-manager —enable remi-php74
- 安装 PHP 7.4:
yum -y update # yum -y install php php-cli
- 使用命令安装其他软件包:
yum -y install php-xxx
例如运行以下命令:
yum -y install php php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mcrypt php-mbstring php-curl php-xml php-pear php-bcmath php-json
等待安装完成后,利用命令查看是否安装成功:
php -v
成功显示如下信息则代表成功:
PHP 7.4.6 (cli) (built: May 12 2020 08:09:15) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
启动 php:
systemctl start php-fpm
同样的,可以使用以下命令查看 php 的运行状态:
systemctl status php-fpm
一般正常会显示以下信息:
● php-fpm.service - The PHP FastCGI Process Manager
Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset: disabled)
Active: active (running) since 三 2020-05-27 10:21:07 CST; 1min 40s ago
Main PID: 64201 (php-fpm)
Status: “Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/sec”
CGroup: /system.slice/php-fpm.service
├─64201 php-fpm: master process (/etc/php-fpm.conf)
├─64202 php-fpm: pool www
├─64203 php-fpm: pool www
├─64204 php-fpm: pool www
├─64205 php-fpm: pool www
└─64206 php-fpm: pool www
5 月 27 10:21:06 localhost.localdomain systemd[1]: Starting The PHP FastCGI Process Ma….
5 月 27 10:21:07 localhost.localdomain systemd[1]: Started The PHP FastCGI Process Man….
Hint: Some lines were ellipsized, use -l to show in full.
开机自启动命令:
systemctl enable php-fpm
补充:查看启用的模块命令:
php —modules
安装 Mysql
- 下载并安装 MySQL 官方的 Yum Repository
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
- 安装 mysql
yum -y install mysql57-community-release-el7-10.noarch.rpm # yum -y install mysql-community-server
安装完成后先用以下命令启动:
systemctl start mysqld
下面的命令是查看 MySQL 运行状态:
systemctl status mysqld
- 修改默认密码,因为不修改默认密码是不能操作数据库的,先从日志中找到默认密码,命令是:
grep “password” /var/log/mysqld.log
回车后会显示类似以下信息:
2020-05-27T02:40:05.094185Z 1 [Note] A temporary password is generated for root@localhost: 470?sxuCPL0%
其中 470?sxuCPL0% 就是默认密码,每个人都不一样,根据自己实际情况而定
- 登录 mysql:
mysql -u root -p
此时可以试着操作一下数据库
mysql> use mysql
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
可以看到,提示说需要修改默认密码才能操作
- 修改 root 密码:
mysql> ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘123456’;
其中 123456 就是需要你修改的密码,密码根据自己的情况设置
输入之后会提示错误
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
这是由于密码设置过于简单,所以不能成功,不过可以通过设置来使用简单的密码,不过不建议,如果需要可以按下面的步骤操作,否则可以跳过,直接到第 7 步
- 设置简单密码
mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=1;
mysql> ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘123456’;
按顺序输入上面的三条命令后会提示修改密码成功
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.01 sec)
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘123456’;
Query OK, 0 rows affected (0.00 sec)
PS:一般不推荐设置过于简单的密码
退出 mysql 命令:
mysql> exit;
- 卸载 Yum Repository,否则每次 yum 操作都会自动更新:
yum -y remove mysql57-community-release-el7-10.noarch
开机自启动:
systemctl enable mysqld
安装 redis
- 首先安装 redis 官方源:
yum install -y http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
- 安装 redis:
yum -y install redis
- 启动 redis:
systemctl start redis
- 查看 redis 状态:
systemctl status redis
显示如下信息则表示成功安装并运行:
● redis.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/redis.service.d
└─limit.conf
Active: active (running) since 三 2020-05-27 11:17:05 CST; 47s ago
Main PID: 78379 (redis-server)
CGroup: /system.slice/redis.service
└─78379 /usr/bin/redis-server 127.0.0.1:6379
5 月 27 11:17:05 localhost.localdomain systemd[1]: Starting Redis persistent key-value….
5 月 27 11:17:05 localhost.localdomain systemd[1]: Started Redis persistent key-value ….
Hint: Some lines were ellipsized, use -l to show in full.
开机自启动:
systemctl enable redis