PHP 环境安装:Linux 开发环境 2 个改进

安装PHP

Linux 环境下推荐使用下载源码并配合 yum 工具安装。
安装依赖包

yum -y install gcc libxml2-dev sqlite-devel libpng-devel openssl-devel

通过官网下载最新稳定版本。

wget https://www.php.net/distributions/php-7.4.9.tar.gz

可选下载历史版本

https://museum.php.net/php5/
https://museum.php.net/php7/

解压并进入源文件

tar -zxvf php-7.4.9.tar.gz
cd php-7.4.9

查看需要配置的文件参数

./configure --help
--with 表示php默认不安装,需要手动安装
--without 表示php默认已经安装,如果不需要安装使用此命令
--enable 表示添加扩展
--disable 表示移除扩展

根据需要配置安装参数

./configure --prefix=/opt/php/php7.4.9  --with-config-file-path=/opt/php/php7.4.9/etc --with-openssl  --with-mysqli --with-apxs2 --enable-fpm --with-gd  --enable-xml --enable-opcache --enable-mysqlnd

编译安装

make
make install

创建 php 配置文件

cp /resource/php-7.4.9/php.ini-development /opt/php/php7.4.9/etc/php.ini

配置基于域名的主机

vim /etc/hosts
根据自己服务器ip配置
192.168.1.10 lamp.test.com
192.168.1.10 lnmp.test.com

LAMP 环境搭建

安装 Apache

yum -y install httpd
systemctl start httpd

创建 conf.d 目录

mkdir /etc/httpd/conf.d

编辑 Apache 配置文件

vim  /etc/httpd/conf/httpd.conf
在末尾添加
IncludeOptional conf.d/*.conf

添加防火墙例外

firewall-cmd --permanent --add-port=80/tcp
systemctl restart firewalld

创建 phpinfo.php 用于测试

echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php

下载并安装 Mysql

wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-community-server

运行 Mysql

systemctl start mysqld

使用初始密码进入数据库并更改密码

grep "password" /var/log/mysqld.log
mysql -u root-p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'A123bbcvoa$'

创建虚拟机

vim /etc/httpd/conf.d/lamp.test.com.conf
<VirtualHost *:80>
    ServerName lamp.test.com
    DocumentRoot /var/www/html

    <Directory /var/www/html>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

创建 mysql.php 测试

<?php
$con = new mysqli("localhost","root","A123bbcvoa$","sys");

if (!$con) {
  die('Could not connect: ' . $con->connect_error);
  } else {
    echo "connect successfully!";
}

mysqli_close($con);
?>

进行测试

lamp.test.com/phpinfo.php
lamp.test.com/mysql.php

关闭 Apache 服务

systemctl stop httpd

LNMP 环境搭建

安装依赖包

yum -y  install gd gd-devel gcc-c++

下载依赖包

wget https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.bz2
wget http://www.zlib.net/zlib-1.2.11.tar.gz

下载 Nginx 安装包

wget http://nginx.org/download/nginx-1.19.2.tar.gz

解压

 tar -zxvf nginx-1.19.2.tar.gz 

根据需求配置参数

./configure --prefix=/opt/nginx --with-pcre=/resource/pcre-8.44 --with-zlib=/resource/zlib-1.2.11 --with-http_ssl_module --with-http_image_filter_module --with-http_stub_status_module --http-log-path=/opt/nginx/logs/access.log --with-http_auth_request_module

编辑 Nginx 配置文件

cd /opt/nginx/conf/nginx.conf
在 http 段添加 include /opt/nginx/conf.d/*.conf;

配置 Nginx 根目录与 Apache 根目录一致

vim /opt/nginx/conf.d/lnmp.test.com.conf
server {
    listen 80;
    root /var/www/html;

    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;
       }
}

创建 php-fpm 配置文件

cp /opt/php/php7.4.9/etc/php-fpm.conf.default /opt/php/php7.4.9/etc/php-fpm.conf

创建 php-fpm 进程服务配置文件

cp /opt/php/php7.4.9/etc/php-fpm.d/www.conf.default /opt/php/php7.4.9/etc/php-fpm.d/www.conf

开启 php-fpm

 /opt/php/php7.4.9/sbin/php-fpm

开启 Nginx

/opt/nginx/sbin/nginx

进行测试

lnmp.test.com/phpinfo.php
lnmp.test.com/mysql.php

提示

本次实验演示将 LAMPLNMP 配置在同一台服务器上,现实工作中建议分开配置。

本文为 Wiki 文章,邀您参与纠错、纰漏和优化
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!