CentOS 7手动搭建Nginx 1.20+MySQL 5.7.36+PHP 7.4.27环境

CentOS 7手动搭建Nginx 1.20+ MySQL 5.7.36 + PHP 7.4.27环境
一、安装Mysql 5.7.36
1、下载 RPM 安装包
2、删除本地默认安装的 MySQL
查看是否已安装mysql或mariadb
[root@webserver ~]# rpm -qa|grep mysql
[root@webserver ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
如果发现存在,用下面命令删除
rpm -e –nodeps  XXX  — 其中 XXX 表示不同版本
例:[root@webserver ~]# rpm -e –nodeps mariadb-libs-5.5.56-2.el7.x86_64
3、安装 MySQL
上传第一步已经下载好的安装包mysql-5.7.36-1.el7.x86_64.rpm-bundle.tar到/tmp目录
安装依赖包
[root@webserver ~]# yum -y install libaio perl-Data-Dumper perl-JSON net-tools
安装MySQL
[root@webserver ~]# cd /tmp/
[root@webserver tmp]# tar  xvf mysql-5.7.36-1.el7.x86_64.rpm-bundle.tar
使用 RPM 安装命令安装,按下面的安装顺序一个个执行安装命令,并且需要自己解决依赖问题。
rpm -ivh mysql-community-common-5.7.36-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.36-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-compat-5.7.36-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.36-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.36-1.el7.x86_64.rpm
———————————————————————————————
启动MySQL服务器
[root@webserver tmp]# systemctl start mysqld
[root@webserver tmp]# systemctl status mysqld
查看mysql初始密码
[root@webserver tmp]# grep password /var/log/mysqld.log
[Note] A temporary password is generated for root@localhost: 8ogiTmyDHa;d
修改初始密码,用初始密码登录后修改
[root@webserver tmp]# mysql -uroot -p’8ogiTmyDHa;d’
mysql> alter user ‘root’@’localhost’ identified by ‘MySQL123!’;
Query OK, 0 rows affected (0.00 sec)
二、安装 Nginx 1.20
[root@webserver ~]# yum install -y epel-release
[root@webserver ~]# yum -y update
[root@webserver ~]# yum install -y nginx
安装成功后,默认的网站目录为: /usr/share/nginx/html
默认的配置文件为:/etc/nginx/nginx.conf
自定义配置文件目录为: /etc/nginx/conf.d/
启动Nginx
[root@webserver ~]# systemctl status nginx
启用开机启动Nginx
[root@webserver ~]# systemctl enable nginx
二、安装PHP 7.4.27
下安装文件# 没有wget可以yum安装下
[root@webserver tmp]# yum install -y wget
[root@webserver tmp]#  wget https://www.php.net/distributions/php-7.4.27.tar.gz
解压安装文件
[root@webserver tmp]#  tar -zxvf php-7.4.27.tar.gz
安装依赖
# 以下依赖是下面configure必须使用到的,如果有其他额外配置可自行安装
[root@webserver tmp]# yum -y install gcc gcc-c++
[root@webserver tmp]# yum -y install libxml2-devel openssl-devel curl-devel libjpeg-devel libpng-devel libicu-devel freetype-devel openldap-devel openldap openldap-devel oniguruma-devel libxslt-devel sqlite-devel 
[root@webserver tmp]# yum install -y bzip2-devel
注:因为用yum安装libzip-devel,安装的版本达不到要求,所以我们手动安装libzip更新的版本
configure: error: Package requirements (libzip >= 0.11 libzip != 1.3.1 libzip != 1.7.0) were not met:No package ‘libzip’ found
下载libzip-1.3.2和解压libzip-1.3.2
[root@webserver tmp]# wget https://libzip.org/download/libzip-1.3.2.tar.gz
[root@webserver tmp]# tar zxf libzip-1.3.2.tar.gz
[root@webserver tmp]# cd libzip-1.3.2
[root@webserver libzip-1.3.2]#./configure
[root@webserver libzip-1.3.2]# make && make install
[root@webserver libzip-1.3.2]# export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
 
4.编译安装php
[root@webserver tmp]# cd php-7.4.27          注意路径
[root@webserver php-7.4.27]# 
./configure \
–prefix=/usr/local/php \
–with-config-file-path=/usr/local/php/etc \
–with-fpm-user=nginx \
–with-fpm-group=nginx \
–with-curl \
–with-MySQL=/usr/local/mysql \
–with-freetype-dir=/usr/local/freetype \
–enable-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-pdo-sqlite \
–with-pear \
–with-png-dir=/usr/local/libpng \
–with-jpeg-dir=/usr/local/libjpeg \
–with-xmlrpc \
–with-xsl \
–with-zlib \
–with-zlib-dir=/usr/local/zlib \
–with-bz2 \
–with-mhash \
–with-zip \
–enable-fpm \
–enable-bcmath \
–enable-libxml \
–enable-inline-optimization \
–enable-mbregex \
–enable-mbstring \
–enable-opcache \
–enable-pcntl \
–enable-shmop \
–enable-soap \
–enable-sockets \
–enable-sysvsem \
–enable-sysvshm \
–enable-xml \
–enable-zip \
–enable-fpm
 执行后,出现License和Thank you for using PHP,则继续执行 make && make install
[root@webserver php-7.4.27]#  make && make install
安装成功后截图
将 PHP 添加到环境变量
[root@webserver ~]# vi /etc/profile
添加在文件最后
export PATH=/usr/local/php/bin:/usr/local/php/sbin:$PATH
[root@webserver ~]# source /etc/profile 
查看PHP版本
[root@webserver ~]# php -v
PHP 7.4.27 (cli) (built: Feb 21 2022 07:28:48) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
将 PHP.ini 文件放到安装目录,编译php.ini文件
[root@webserver ~]# cp /tmp/php-7.4.27/php.ini-production /usr/local/php/etc/php.ini
[root@webserver ~]# vi /usr/local/php/etc/php.ini
………..
extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-20190902”
………….
zend_extension=opcache.so
………..
; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
; http://php.net/mysqli.default-socket
mysqli.default_socket = /var/lib/mysql/mysql.sock
……………
[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=1
复制 php-fpm 配置文件
[root@webserver ~]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@webserver ~]# vi /usr/local/php/etc/php-fpm.conf
; Error log file
; If it’s set to “syslog”, log is sent to syslogd instead of being written
; into a local file.
; Note: the default prefix is /usr/local/php/var
; Default Value: log/php-fpm.log
;error_log = log/php-fpm.log
error_log = /var/log/php-fpm/error.log
….
设备开机自启动php-fpm
[root@webserver ~]# cp /tmp/php-7.4.27/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@webserver ~]# chmod +x /etc/init.d/php-fpm
[root@webserver ~]# chkconfig –add php-fpm
[root@webserver ~]# chkconfig php-fpm on
设置www.conf文件,此文件是php-fpm运行的扩展配置文件
[root@webserver ~]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
[root@webserver ~]# vi /usr/local/php/etc/php-fpm.d/www.conf
; Note: The user is mandatory. If the group is not set, the default user’s group
;       will be used.
user = nginx
group = nginx
;   ‘/path/to/unix/socket’ – to listen on a unix socket.
; Note: This value is mandatory.
;listen = 127.0.0.1:9000
listen = /var/run/php-fpm/php-fpm.sock
 
; Default Values: user and group are set as the running user
;                mode is set to 0660
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
 
; The log file for slow requests
; Default Value: not set
; Note: slowlog is mandatory if request_slowlog_timeout is set
;slowlog = log/$pool.log.slow
slowlog = /var/log/php-fpm/$pool.log.slow
 
; The timeout for serving a single request after which a PHP backtrace will be
; dumped to the ‘slowlog’ file. A value of ‘0s’ means ‘off’.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
request_slowlog_timeout = 1
 
设置php-fpm运行目录及相关目录和权限
[root@webserver ~]# mkdir -p /var/log/php-fpm /var/run/php-fpm
[root@webserver ~]# chown nginx:nginx -R /var/run/php-fpm/
 
在 /etc/init.d/php-fpm中添加如下代码,让php-fpm运行目录自动创建并修改权限
[root@webserver ~]# vi /etc/init.d/php-fpm 
 
php_fpm_log=”/var/log/php-fpm”
php_fpm_run=”/var/run/php-fpm”
if [ ! -d $php_fpm_log ];then
mkdir -p $php_fpm_log
chown -R nginx.nginx $php_fpm_run
fi
if [ ! -d $php_fpm_run ];then
mkdir -p $php_fpm_run
chown -R nginx.nginx $php_fpm_run
fi
chown -R nginx:nginx /var/run/php-fpm/
 
检测php-fpm配置
[root@webserver ~]# php-fpm -t
如果有下面的错误,只需要去安装对应的扩展即可
扩展补完后,启动PHP-FPM
service php-fpm start
查看php模块
php -m
php-fpm -m
 
测试一:测试nginx应用php是否成功
[root@webserver ~]# cd /usr/share/nginx/html/
[root@webserver html]# vi test.php
<?php phpinfo();?>
[root@webserver html]# vi /etc/nginx/nginx.conf
    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;
        index index.php index.htm index.html;
        location / {
            try_files $uri $uri/ /index.php$is_args$args;
        }
        location ~ \.php$ {
            try_files $uri = 404;
            fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
[root@webserver html]# systemctl restart nginx
打开浏览器访问http://10.10.10.11/test.php,测试php是否应用成功,成功截图如下
测试二:测php通过msqli_connect连接mysql是否成功
[root@webserver ~]# vi test1.php
<?php 
$link=mysqli_connect(“localhost”,”root”,”MySQL123!”); 
if(!$link) echo “FAILD!连接错误,用户名密码不对”; 
else echo “OK!可以连接”; 
?>
[root@webserver ~]# php test1.php 
OK!可以连接

2人评论了“CentOS 7手动搭建Nginx 1.20+MySQL 5.7.36+PHP 7.4.27环境”

评论区已关闭。