Linux 系统快速安装PHP环境(新手版)
1、下载安装包,这里安装php-7.4.22.tar.gz。PHP安装包下载。
2、上传到local并解压
cd php-7.4.22
3、安装必备依赖
(如果yum源需要更新可以运行 yum -y update)
yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses curl gdbm-devel db4-devel libXpm-devel libX11-devel gd-devel gmp-devel expat-devel xmlrpc-c xmlrpc-c-devel libicu-devel libmcrypt-devel libmemcached-devel libzip gcc-c++
4、进行预编译
cd /usr/local/php-7.4.22
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/lib \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-fpm \
--enable-opcache \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-mysqlnd-compression-support \
--with-iconv-dir \
--with-zlib \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-ftp \
--enable-gd \
--enable-gd-jis-conv \
--with-jpeg \
--with-freetype \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--with-zip \
--enable-soap \
--with-gettext \
--disable-fileinfo \
--with-pear \
--enable-maintainer-zts \
--with-ldap=shared \
--without-gdbm
5、编译&&安装
make && make install
6、配置并启动
cd /usr/local/php/ #先进入安装目录
cp ./etc/php-fpm.conf.default ./etc/php-fpm.conf #配置文件
vim /usr/local/php/etc/php-fpm.conf
将;pid = run/php-fpm.pid 前面的;分号删掉
#配置文件
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
cp /usr/local/php-7.4.22/php.ini-production /usr/local/php/lib/php.ini
修改php.ini配置文件
max_execution_time = 300
max_input_time = 600
post_max_size = 80M
max_file_uploads = 20
date.timezone = Asia/Shanghai
7、配置启动服务
1、添加php-fpm服务启动脚本
vim /lib/systemd/system/php-fpm.service
[Unit]
Description=php-fpm
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm ##需要注意修改自己的路径
ExecStop=/bin/pkill -9 php-fpm
PrivateTmp=true
[Install]
WantedBy=multi-user.target
2、 systemctl list-unit-files --type=service查看有没有php-fpm.service
如果没有就是用systemctl daemon-reload重新加载,在使用以上命令查看
3、添加开机自启 systemctl enable php-fpm.service
------------------------------------------------
服务字段说明
[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
-----------------------------------------------------------------------------
******************第二种方法**********************
//拷贝php-fpm脚本至/etc/init.d目录
cp /usr/local/php-7.4.22/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
//设置权限并启动php-fpm:
chmod 755 /etc/init.d/php-fpm
/etc/init.d/php-fpm start
// php-fpm以服务的方式启动、停止和重启:
##vim /etc/init.d/php-fpm
chkconfig --add /etc/init.d/php-fpm
service php-fpm start
service php-fpm stop
service php-fpm reload
设置php-fpm开机自启:
cp /usr/local/php-7.4.22/sapi/fpm/php-fpm.service /usr/lib/systemd/system
systemctl enable php-fpm
8、使用Nginx解析PHP(Nginx的安装详见以前博客)
进入到nginx安装目录 cd /usr/local/nginx/
修改配置文件 vim ./conf/nginx.conf
server {
listen 8088;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.php index.html index.htm;
}
修改路径
把scripts改为 document_root
location ~ \.php$ {
root /usr/local/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
(1)增加index.php
在 /usr/local/nginx/html路径下增加index.php 文件进行测试index.php内容如下
<?php echo phpinfo(); ?>
(2)测试与mysql数据库连接
<?php
header("content-type:text/html;charset=utf-8");
$servername = "192.168.0.118";
$username = "root";
$password = "123";
// 创建连接
$conn = new mysqli($servername, $username, $password);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
echo "连接成功";
?>
9、添加环境变量
vim /etc/profile
export PATH=$PATH:'/usr/local/php/bin/'
source /etc/profile
#############################################################################
问题
#############################################################################
问题:
1、如果提示No package sqlite3 found问题
先检查yum源 yum search sqlite 发现 sqlite-devel.x86_64 暂没有安装
执行
sudo yum install -y sqlite-devel.x86_64
2、 No package 'oniguruma' found
下载 oniguruma5php-6.9.8-1.el7.remi.x86_64.rpm oniguruma5php-devel-6.9.8-1.el7.remi.x86_64.rpm
这两个包(已下载在问题库包里)
安装这两个包 yum -y localinstall *.rpm
然后再进入PHP目录 运行config就不报这个错误了
3、报 configure: error: Cannot find ldap.h
解决办法:
yum install openldap openldap-devel -y
4、报错:configure: error: Cannot find ldap libraries in /usr/lib
解决办法:
cp -frp /usr/lib64/libldap* /usr/lib/
5、报
checking for libzip >= 0.11 libzip != 1.3.1 libzip != 1.7.0... no
configure: error: Package requirements (libzip >= 0.11 libzip != 1.3.1 libzip != 1.7.0) were not met:
No package 'libzip' found
No package 'libzip' found
No package 'libzip' found
解决方法:
检查是否安装过 libzip
rpm -q libzip
rpm -q libzip-devel
如果有则卸载它, yum remove libzip-devel libzip
然后下载libzip-1.2.0.tar.gz(此包已经下载,在\Linux部署PHP\问题处理文件包下)
cd libzip-1.2.0
./configure
make
make install
export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/"
启动
#cd /usr/local/php/sbin/
# ./php-fpm #启动
#ps -ef|grep php-fpm #查看启动状态