首页 编程开发在centos7环境下使用源码和remi源两种方式安装php环境

在centos7环境下使用源码和remi源两种方式安装php环境

发布于: 2020-07-27 03:12:00
字数: 4738
评论: 0
阅读: 2156

本文介绍了在centos7环境下使用源码和remi源两种方式安装 php环境 , 本次安装的软件有:

  • PHP 8.2.7 (cli) (built: Jun 6 2023 21:28:56) (NTS gcc x86_64)
  • mysql Ver 8.0.33 for Linux on x86_64 (MySQL Community Server - GPL)
  • nginx version: nginx/1.20.1

本次安装所用的系统为:CentOS Linux release 7.9.2009 (Core)

Nginx安装

通常服务器中可能自带nginx,可以使用 nginx -v 来查看是否已经安装,如显示出nginx的版本信息说明已经安装。
如果提示命令不存在,则可以用yum来安装

[root@VM-16-17-centos ~]# yum update
[root@VM-16-17-centos ~]# yum install -y nginx

运行完成后,使用nginx -v来验证是否安装成功。

[root@VM-16-17-centos ~]# nginx -v
nginx version: nginx/1.20.1
[root@VM-16-17-centos ~]# systemctl status nginx
····active (running)····

以上表示安装成功.

参考命令行

# 查看nginx版本
nginx -v
# 查看nginx服务状态
systemctl status nginx
# 启动nginx服务
systemctl start nginx
# 停止nginx服务
systemctl stop nginx
# 重启nginx服务
systemctl restart nginx

注意事项

安装完毕之后,可以使用浏览器访问http://服务器ip,如果显示默认页面则表示安装成功,且已经可以访问
如果无法访问,则可能需要配置selinux规则,或者查看防火墙是否允许访问80端口, 如iptables、firewall、云安全组(阿里云、腾讯云等)

PHP安装

PHP的安装可以使用源码编译安装,也可以使用remi源来安装,个人更倾向于使用remi源来安装,因为安装方便好管理。

php源码安装

源码安装之前应先安装编译时的依赖库:(如有不全遗漏的安装时会报错提示,安装上即可)

yum -y install gcc gcc-c++
yum -y install libxml2
yum -y install libxml2-devel
yum -y install openssl
yum -y install openssl-devel
yum -y install curl
yum -y install curl-devel
yum -y install libjpeg
yum -y install libjpeg-devel
yum -y install libpng
yum -y install libpng-devel
yum -y install freetype
yum -y install freetype-devel
yum -y install pcre
yum -y install pcre-devel
yum -y install libxslt
yum -y install libxslt-devel
yum -y install bzip2
yum -y install bzip2-devel

下载PHP源码

[root@192 ~]# curl -o php-8.2.7.tar.gz https://www.php.net/distributions/php-8.2.7.tar.gz
[root@192 ~]# tar -xvzf php-8.2.7.tar.gz
[root@192 ~]# cd php-8.2.7

编译安装php

[root@192 php-8.2.7]# ./configure --prefix=/usr/local/php --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-xml --enable-zip
[root@192 php-8.2.7]# make && make install

安装完之后将php添加到环境变量中:

[root@192 php-8.2.7]# vi /etc/profile

在文件末尾添加

PATH=$PATH:/usr/local/php/bin
export PATH

然后使环境变量生效,并且验证php安装

[root@192 php-8.2.7]# source /etc/profile
[root@192 php-8.2.7]# php -v
PHP 8.2.7 (cli) (built: Jun  6 2023 21:28:56) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.2.7, Copyright (c) Zend Technologies

remi源安装php

从清华镜像安装remi源

yum install https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm

yum安装php82,可自行增加/删除所需扩展

yum install php82 php82-php-fpm php82-php-mysqli php82-php-mbstring php82-php-pecl-zip php82-php-xml

# 查看php82相关扩展
yum search php82

php-fpm服务命令

# 开启fpm服务
systemctl start php82-php-fpm
# 加入开机自动启动
systemctl enable php82-php-fpm

至此,php安装完毕

配置nginx支持PHP

安装完php之后,可以通过cgi方式让php-fpm来解析nginx请求:
进入nginx配置文件目录路径

/etc/nginx/conf.d/

创建一个对应站点的配置文件,并加入配置

vi blog_phpat_com.conf
# blog
server {
    root /webroot/blog_phpat_com;
    server_name blog.phpat.com;
    index index.php index.html index.htm;
    client_max_body_size 50m;
    location / {
        index index.php index.html;
        if (!-e $request_filename){
            rewrite ^/(.*)$ /index.php last;
        }
    }
    location ~ .php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        try_files $uri $uri/ /index.php?$query_string;
    }
    listen 80;
}

MySQL安装

# 下载mysql rpm-bundle包
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.33-1.el7.x86_64.rpm-bundle.tar
# 解压
tar -xvf mysql-8.0.33-1.el7.x86_64.rpm-bundle.tar
# 安装
rpm -ivh mysql-community-common-8.0.33-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.33-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.33-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-compat-8.0.33-1.el7.x86_64.rpm
rpm -ivh mysql-community-icu-data-files-8.0.33-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.33-1.el7.x86_64.rpm

# 启动
systemctl start mysqld
# 加入开机自启
systemctl  enabled mysqld
# 查看状态
systemctl  status mysqld
# 查看mysql的root账号初始密码
cat /var/log/mysqld.log

启动MySQL后,使用默认的初始密码登录,并修改密码

mysql -uroot -p
# 修改root密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
# 新建phpat用户
use mysql;
CREATE USER 'phpat'@'%' IDENTIFIED BY 'phpat1234';
flush privileges;
# 给予remote_account用户远程连接权限(超级管理员权限)
GRANT ALL PRIVILEGES ON *.* TO 'phpat'@'%' WITH GRANT OPTION;
flush privileges;

至此,MySQL就安装完毕了


扫描二维码,在手机上阅读