Linux Centos编译安装Apache , MySQL , PHP 搭建LAMP环境

Linux Centos编译安装Apache , MySQL , PHP 搭建LAMP环境

lamp简介

Linux+Apache+MySQL+PHP 常用来搭建动态网站或者服务器的开源软件,本身都是各自独立的程序,但是因为常被放在一起使用,拥有了越来越高的兼容度,共同组成了一个强大的 Web 应用程序平台。

Apache相对Nginx来说更加稳定,动态页面的处理更加合适。

编译安装Apache

这里我们构建MPM为默认模块,这里需要aprapr-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

centos7.4编译安装lamp

make && make install

centos7.4编译安装lamp
编辑/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

显示如下字段证明可以正常工作
centos7.4编译安装lamp
到此为止httpd编译基本完成。
如果想要更改运行账号和所属组可以通过修改配置文件的user和group字段为apache

useradd -r -s /sbin/nologin apache
chown -R apache:apache /usr/local/apache

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

编译安装MySQL

获取mariadb的源码包

wget https://cdn.mysql.com//archives/mysql-5.6/mysql-5.6.15.tar.gz

解压并且进入目录

tar xvf mysql-5.6.15.tar.gz
cd mysql-5.6.15/

使用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

centos7.4编译安装lamp
添加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地址反解成主机名

centos7.4编译安装lamp
添加环境变量

    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

centos7.4编译安装lamp
安全初始化,这里会要求设置密码

/usr/local/mysql/bin/mysql_secure_installation

centos7.4编译安装lamp

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
centos7.4编译安装lamp
centos7.4编译安装lamp

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

centos7.4编译安装lamp
2、定位至DirectoryIndex index.html
修改为:

            DirectoryIndex  index.php  index.html

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

重启httpd之后进行验证

apachectl stop
apachectl start
发布了284 篇原创文章 · 获赞 258 · 访问量 121万+

猜你喜欢

转载自blog.csdn.net/meimeieee/article/details/103938431