centos7.4编译安装php,centos7.4编译安装lamp

centos7.4编译安装lamp

lamp简介

Linux+Apache+Mysql/MariaDB+PHP一组常用来搭建动态网站或者服务器的开源软件,本身都是各自独立的程序,但是因为常被放在一起使用,拥有了越来越高的兼容度,共同组成了一个强大的Web应用程序平台。apache相对nginx来说更加稳定,动态页面的处理更加合适。

源码包

httpd-2.4.33

mariadb-10.2.14

php-7.2.5

ab8f7f7ff8f2e225c489a1202ef95353.png

基本编译环境构建

系统版本:CentOS 7.4 x86_64

安装开发包:Development Tools

yum groupinstall Development\ Tools -y

编译安装mariadb需要cmake ncurses-devel

yum install cmake -y

yum install ncurses-devel.x86_64 -y

开始编译

编译安装httpd-2.4.33

这里我们构建MPM为默认模块,这里需要apr和apr-util 1.5以上的版本

因此先到官方网站去下载相应版本

官方网站地址 https://apr.apache.org/

先编译安装apr,apr-util之后要依赖apr

wget http://archive.apache.org/dist/apr/apr-1.5.1.tar.gz

tar xvf apr-1.5.1.tar.gz

cd apr-1.5.1/

./configure --prefix=/usr/local/apr

make && make install

之后编译安装apr-util

wget http://archive.apache.org/dist/apr/apr-util-1.5.4.tar.gz

tar xvf apr-util-1.5.4.tar.gz

cd apr-util-1.5.4/

./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr

#with-apr要指明apr的安装目录,apr-util要依赖apr编译

make && make install

httpd编译依赖的包yum安装即可,这里提前安装以便编译过程一次通过

yum install pcre-devel.x86_64 -y

yum install openssl-devel.x86_64 -y

下载并且编译安装httpd

wget http://archive.apache.org/dist/httpd/httpd-2.4.33.tar.gz

tar xvf httpd-2.4.33.tar.gz

cd httpd-2.4.33/

./configure \

--prefix=/usr/local/apache \

--sysconfdir=/etc/httpd24 \

--enable-so \

--enable-ssl \

--enable-cgi \

--enable-rewrite \

--with-zlib \

--with-pcre \

--with-apr=/usr/local/apr \

--with-apr-util=/usr/local/apr-util \

--enable-modules=most \

--enable-mpms-shared=all \

--with-mpm=event

注释:

./configure下的第一行指明编译安装的路径

第二行指明配置文件的路径

第三行支持动态装载卸载模块

第四行支持https通信

第五行支持cgi协议

第六行支持url重写

第七行支持数据压缩

第八行兼容正则表达式

第九行和第十行指明apr和apr-util路径

第十一行 支持大多数模块

第十二行 支持全部的工作模型

第十三行 默认工作模式为event

68b560448a182a0f0c1e55471d96e9fb.png

make && make install

ef925af1eb61195936dac8e70376fbb1.png

编辑/etc/httpd24/httpd.conf,添加如下行即可:

PidFile "/var/run/httpd.pid"

添加PATH变量

编辑/etc/profile

vim /etc/profile

添加如下字段

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/apache/bin

保存之后重读配置文件

source /etc/profile

启动httpd验证httpd能够正常工作

apachectl start

curl 127.0.0.1

显示如下字段证明可以正常工作

97c19407703d2323bbacb2e37acdfa0d.png

到此为止httpd编译基本完成。

如果想要更改运行账号和所属组可以通过修改配置文件的user和group字段为apache

useradd -r -s /sbin/nologin apache

chown -R apache:apache /usr/local/apache

f8e3bb29a43a23da4dfe64f481db7aef.png

想要启动脚本可以去yum安装的httpd中拷贝一份放到对应目录即可,这里不再赘述。

编译安装mariadb

获取mariadb的源码包

wget http://mirrors.neusoft.edu.cn/mariadb//mariadb-10.2.14/source/mariadb-10.2.14.tar.gz

解压并且进入目录

tar xvf mariadb-10.2.14.tar.gz

cd mariadb-10.2.14/

使用cmake进行编译安装

cmake \

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \

-DSYSCONFDIR=/etc \

-DDEFAULT_CHARSET=utf8 \

-DDEFAULT_COLLATION=utf8_general_ci \

-DWITH_EXTRA_CHARSETS=all

# 注:

#第一行是mysql主程序安装目录

#第二行是配置文件目录

#第三行默认字符集为utf8

#第四行默认的字符集效对规则

#第五行安装所有字符集

make && make install

1e2b5e327da214523e28fb6926d3b632.png

添加mysql用户和组

useradd -r -M -s /sbin/nologin mysql

chown mysql:root /usr/local/mysql/

进行一些基本配置

cp support-files/my-large.cnf /etc/my.cnf

#复制配置文件

cp support-files/mysql.server /etc/init.d/mysqld

#复制启动脚本

vim /etc/my.conf datadir = /mydata

#指定数据库路径,不然无法启动mysql 自己定义

innodb_file_per_table = on

#设置后当创建数据库的表的时候表文件都会分离开,方便复制表,不开启创建的表都在一个文件

skip_name_resolve = on

#跳过名称反解,Mysql每次使用客户端链接时都会把ip地址反解成主机名

f028210f54c18182a92a80bea548e642.png

添加环境变量

vim /etc/profile

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/apache/bin:/usr/local/mysql/bin

#添加mysql的目录

source /etc/profile

初始化数据库

/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/mydata/

#初始化数据库

启动数据库

service mysql start

670874a884ec308a3eacdf38a049b975.png

安全初始化,这里会要求设置密码

/usr/local/mysql/bin/mysql_secure_installation

ca958b07e835c5bf5c5d6354afd6973d.png

mysql基本完成

编译安装php

编译过程中缺少的一些包

yum install libxml2-devel.x86_64 -y

yum install bzip2-devel.x86_64 -y

下载php-7.2.5并且解压

wget http://us1.php.net/distributions/php-7.2.5.tar.bz2

tar xvf php-7.2.5.tar.bz2

cd php-7.2.5/

编译安装php

./configure \

--prefix=/usr/local/php \

--with-mysqli=mysqlnd \

--with-pdo-mysql=mysqlnd \

--with-openssl \

--enable-mbstring \

--with-freetype-dir \

--with-jpeg-dir \

--with-png-dir \

--with-zlib \

--with-libxml-dir=/usr \

--enable-xml \

--enable-sockets \

--with-apxs2=/usr/local/apache/bin/apxs \

--with-config-file-path=/etc \

--with-config-file-scan-dir=/etc/php.d \

--with-bz2 \

--enable-maintainer-zts

#这里要注意的是原来的--with-mysql在5.5废弃,在php7开始被移除,之后推荐使用 MySQLi 或 PDO_MySQL 扩展来替换

官方示例mysqli

295cf5298ad4613725fae62d9a13623a.png

d21e09c2a44cfca3127dc88d44b11e90.png

make && make install

配置文件

为php提供配置文件:

cp php.ini-production /etc/php.ini

编辑apache配置文件httpd.conf,以apache支持php

vim /etc/httpd/httpd.conf

1、添加如下二行

AddType application/x-httpd-php .php

AddType application/x-httpd-php-source .phps

8a5ed0d63c91d067492693bee25b15e4.png

2、定位至DirectoryIndex index.html

修改为:

DirectoryIndex index.php index.html

f540769395e4c6aa7c1b416f2392c762.png

而后重新启动httpd,或让其重新载入配置文件即可测试php是否已经可以正常使用。

测试页面index.php示例如下:

phpinfo();

?>

$link = mysqli_connect('127.0.0.1','root','123');

if ($link)

echo "Success...";

else

echo "Failure...";

mysql_close();

?>

b919c67f12baccb03d257220cc7c90a5.png

ebee1a00a7f5a540a52bb1d5b49d30ea.png

42e79063a59e19f8180ca40a69566c71.png

重启httpd之后进行验证

apachectl stop

apachectl start

e7f07ff8a22ea85566c8e76d8f1b392b.png

完结撒花~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值