CentOS7最小化源码安装LAMP-步骤详解

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS7最小化源码安装LAMP-步骤详解相关的知识,希望对你有一定的参考价值。

CentOS7最小化源码安装LAMP-步骤详解


系统:CentOS 7.3.1611(最小化安装)

软件:httpd-2.4.27

      mysql-5.7.18

      php-5.6.3


一、配置系统环境


1.1. 查看系统版本

# cat /etc/centos-release

CentOS Linux release 7.3.1611 (Core) 


1.2. 查看防火墙状态,关闭防火墙及其开机启动

# systemctl status firewalld

# systemctl stop firewalld

# systemctl disable firewalld


1.3. 关闭SELINUX

# getenforce

Enforcing

# setenforce 0

# getenforce

Permissive

# sed -i ‘s/\(^SELINUX=\)enforcing/\1disabled/‘ /etc/selinux/config


1.4. 安装依赖及所需工具

# yum install -y wget bzip2


二、编译安装Apache httpd


2.1. 依赖包

# yum install -y gcc-c++ expat-devel openssl-devel


2.2. 下载httpd安装程序及依赖包(官方网站:http://httpd.apache.org/)

# cd /usr/local/src; mkdir lamp; cd lamp/

# wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.6.2.tar.bz2

# wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-util-1.6.0.tar.bz2

# wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-iconv-1.2.1.tar.bz2

# wget http://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.27.tar.bz2

# wget https://ftp.pcre.org/pub/pcre/pcre-8.41.tar.bz2


2.3. 解压安装pcre

# cd /usr/local/src/lamp

# tar xf pcre-8.41.tar.bz2

# cd pcre-8.41

# ./configure --prefix=/usr/local/pcre841

# make && make install


2.4. 解压安装apr

# cd /usr/local/src/lamp

# tar xf apr-1.6.2.tar.bz2

# cd apr-1.6.2

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

# make && make install


2.5. 解压安装apr-util和apr-iconv

# cd /usr/local/src/lamp

# tar xf apr-iconv-1.2.1.tar.bz2

# tar xf apr-util-1.6.0.tar.bz2

# mv apr-iconv-1.2.1 apr-util-1.6.0

# cd apr-util-1.6.0

# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr --with-apr-iconv=apr-iconv-1.2.1 && make && make install

# ln -s /usr/local/apr-util/lib/libapriconv-1.la ./apr-iconv-1.2.1/lib/libapriconv.la


2.6. 解压安装apache httpd

# cd /usr/local/src/lamp

# tar xf httpd-2.4.27.tar.bz2

# cd httpd-2.4.27

# ./configure --prefix=/usr/local/apache2 --sysconfdir=/etc/httpd --with-pcre=/usr/local/pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mods-shared=most --enable-so --enable-deflate --enable-proxy --enable-proxy-fcgi --enable-cgi --enable-rewrite --enable-mime-magic  --with-ssl --enable-ssl --enable-mpms-shared=all --with-mpm=event

# make && make install


2.7. 修改httpd配置文件/usr/local/apache/conf/httpd.conf


    添加

    PidFile "/var/run/httpd.pid"

    

    可修改ServerAdmin项为:

    ServerAdmin [email protected]

    可修改ServerName项为:

    ServerName localhost:80


2.8. 服务脚本/etc/rc.d/init.d/httpd

#!/bin/bash

#

# httpd        Startup script for the Apache HTTP Server

#

# chkconfig: - 85 15

# description: The Apache HTTP Server is an efficient and extensible  \

#              server implementing the current HTTP standards.

# processname: httpd

# config: /etc/httpd/httpd.conf

# config: /etc/sysconfig/httpd

# pidfile: /var/run/httpd.pid

#


### BEGIN INIT INFO

# Provides: httpd

# Required-Start: $local_fs $remote_fs $network $named

# Required-Stop: $local_fs $remote_fs $network

# Should-Start: distcache

# Short-Description: start and stop Apache HTTP Server

# Description: The Apache HTTP Server is an extensible server

#  implementing the current HTTP standards.

### END INIT INFO


# Source function library.

. /etc/rc.d/init.d/functions


if [ -f /etc/sysconfig/httpd ]; then

. /etc/sysconfig/httpd

fi


# Start httpd in the C locale by default.

HTTPD_LANG=${HTTPD_LANG-"C"}


# This will prevent initlog from swallowing up a pass-phrase prompt if

# mod_ssl needs a pass-phrase from the user.

INITLOG_ARGS=""


# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server

# with the thread-based "worker" MPM; BE WARNED that some modules may not

# work correctly with a thread-based MPM; notably PHP will refuse to start.


# Path to the apachectl script, server binary, and short-form for messages.

apachectl=/usr/local/apache2/bin/apachectl

httpd=/usr/local/apache2/bin/httpd

prog=httpd

pidfile=/var/run/httpd.pid

lockfile=/var/lock/subsys/httpd

RETVAL=0

STOP_TIMEOUT=${STOP_TIMEOUT-10}


# The semantics of these two functions differ from the way apachectl does

# things -- attempting to start while running is a failure, and shutdown

# when not running is also a failure.  So we just do it the way init scripts

# are expected to behave here.

start() {

    echo -n $"Starting $prog: "

    LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS

    RETVAL=$?

    echo

    [ $RETVAL = 0 ] && touch ${lockfile}

    return $RETVAL

}


# When stopping httpd, a delay (of default 10 second) is required

# before SIGKILLing the httpd parent; this gives enough time for the

# httpd parent to SIGKILL any errant children.

stop() {

    echo -n $"Stopping $prog: "

    killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd

    RETVAL=$?

    echo

    [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}

}


reload() {

    echo -n $"Reloading $prog: "

    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then

        RETVAL=6

        echo $"not reloading due to configuration syntax error"

        failure $"not reloading $httpd due to configuration syntax error"

    else

        # Force LSB behaviour from killproc

        LSB=1 killproc -p ${pidfile} $httpd -HUP

        RETVAL=$?

        if [ $RETVAL -eq 7 ]; then

            failure $"httpd shutdown"

        fi

    fi

    echo

}


# See how we were called.

case "$1" in

    start)

        start

        ;;

    stop)

        stop

        ;;

    status)

        status -p ${pidfile} $httpd

        RETVAL=$?

        ;;

    restart)

        stop

        start

        ;;

    condrestart|try-restart)

        if status -p ${pidfile} $httpd >&/dev/null; then

            stop

            start

        fi

        ;;

    force-reload|reload)

        reload

        ;;

    graceful|help|configtest|fullstatus)

        $apachectl [email protected]

        RETVAL=$?

        ;;

    *)

        echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"

        RETVAL=2

esac


exit $RETVAL


2.9. 修改服务脚本执行权限

# chmod +x /etc/rc.d/init.d/httpd


2.10. 加入开机启动服务列表

# chkconfig --and httpd

# chkconfig --level 35 httpd on


2.11. 添加apache命令路径/etc/profile.d/httpd.sh

export $PATH:/usr/local/apache2/bin


三、编译安装MySQL


3.1. 依赖包

# yum install -y cmake ncurses-devel


3.2. 下载源码文件并解压(含boost-1.59.0)

# cd /usr/local/src/lamp

# wget https://cdn.mysql.com/Downloads/MySQL-5.7/mysql-boost-5.7.18.tar.gz

# tar xf mysql-boost-5.7.18.tar.gz

# cd mysql-5.7.18

# mv boost/boost_1_59_0/ /usr/local/  #移动boost/至/usr/local/目录下


3.2. 新建mysql用户和组,创建/mydata/data数据存放目录

    # groupadd -r -g 306 mysql

    # useradd -r -M -u 306 -g mysql mysql

    # mkdir -p /mydata/{data,logs,temp}


3.3. 预编译及编译安装MySQL

# cmake \

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \

-DWITH_BOOST=/usr/local/boost_1_59_0 \

-DMYSQL_DATADIR=/mydata/data \

-DTMPDIR=/mydata/temp \

-DMYSQL_UNIX_ADDR=/mydata/temp/mysql.sock \

-DWITH_MYISAM_STORAGE_ENGINE=1 \

-DWITH_INNOBASE_STORAGE_ENGINE=1 \

-DWITH_FEDERATED_STORAGE_ENGINE=1 \

-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \

-DWITH_EXAMPLE_STORAGE_ENGINE=1 \

-DWITH_PARTITION_STORAGE_ENGINE=1 \

-DWITH_MEMORY_STORAGE_ENGINE=1 \

-DWITH_MERGE_STORAGE_ENGINE=1 \

-DWITH_CSV_STORAGE_ENGINE=1 \

-DWITH_ARCHIVE_STORAGE_ENGINE=1 \

-DDEFAULT_CHARSET=utf8 \

-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \

-DDEFAULT_COLLATION=utf8_general_ci \

-DMYSQL_TCP_PORT=3306 \

-DMYSQL_USER=mysql \

-DWITH_READLINE=1 \

-DENABLED_LOCAL_INFILE=1 \

-DWITH_ZLIB=bundled \

-DWITH_READLINE=1 \

-DWITH_FAST_MUTEXES=1 \

-DWITH_EMBEDDED_SERVER=1 \

-DWITH_DEBUG=0 \

-DWITH_SSL=system


# make && make install


3.4. 设置开机启动脚本

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


3.5. 修改mysql配置文件/etc/my.cnf

[mysqld]

basedir=/usr/local/mysql

datadir=/mydata/data

pid-file=/mydata/data/mysql.pid

user=mysql

socket=/mydata/temp/mysql.sock

symbolic-links=0


[mysqld_safe]

log-error=/mydata/logs/mysql.log

pid-file=/mydata/data/mysqld.pid


!includedir /etc/my.cnf.d


3.6. 配置MySQL环境变量

# vi /etc/profile.d/mysql.sh

export PATH=%PATH:/usr/local/mysql/bin

# source /etc/profile

# touch /mydata/logs/mysql.log

# chown -R  mysql:mysql /mydata

# chmod -R o-rx /mydata/data    #安全选项

# chown -R root.mysql /usr/local/mysql/*      #为安全,将mysql安装目录下的文件改为root用户,mysql组权限


编辑/etc/ld.so.conf.d/mysql.conf         #导出lib库

/usr/local/mysql/lib

# ldconfig -v      # 加载并显示lib库缓存到/etc/ld.so.cache文件


# ln -sv /usr/local/mysql/include /usr/include/mysql     #导出链接头文件


3.4. 初始化MySQL数据库


# mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/mydata/data

2017-07-14T09:39:18.237980Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2017-07-14T09:39:19.392666Z 0 [Warning] InnoDB: New log files created, LSN=45790

2017-07-14T09:39:19.557103Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.

2017-07-14T09:39:19.671378Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 4f77eeaf-6878-11e7-8ac2-000c29f55b28.

2017-07-14T09:39:19.672344Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened.

2017-07-14T09:39:19.998278Z 0 [Warning] CA certificate ca.pem is self signed.

2017-07-14T09:39:20.096930Z 1 [Note] A temporary password is generated for [email protected]: kkjMdfwfE3?w


# 注:初始化完成后,最后一行提供临时生成的root用户登录密码(kkjMdfwfE3?w)。


3.9. 启动mysqld服务

# systemctl enable mysqld

# service mysqld start


3.10. 登录mysql,更改root密码,添加授权

# mysql -uroot -p‘kkjMdfwfE3?w‘

> ALTER USER ‘root‘@‘localhost‘ IDENTIFIED BY ‘123456‘;

> GRANT ALL PRIVILEGES ON *.* to ‘root‘@‘192.168.%‘ IDENTIFIED BY ‘123456‘;

> FLUSH PRIVILEGES;


四、源码编译安装PHP


4.1 依赖包

# yum install libxml2-devel bzip2-devel libcurl-devel gd-devel gmp-devel readline-devel libxslt-devel


4.2. 下载并安装libmcrypt,libmcrypt-devel

# cd /usr/local/src/lamp

# wget ftp://rpmfind.net/linux/epel/7/x86_64/l/libmcrypt-devel-2.5.8-13.el7.x86_64.rpm

# wget ftp://rpmfind.net/linux/epel/7/x86_64/l/libmcrypt-2.5.8-13.el7.x86_64.rpm

# rpm -ivh libmcrypt*.rpm


4.3. 链接文件

# ln -s /usr/local/mysql/lib/libmysqlclient.so.20.3.5 /usr/local/mysql/lib/libmysqlclient_r.so


4.4. 下载并解压安装PHP-5.6.31

# wget http://php.net/distributions/php-5.6.31.tar.bz2

# tar xf php-5.6.31.tar.bz2


# cd php-5.6.31

# ./configure \

--prefix=/usr/local/php \

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

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

--with-mysql=/usr/local/mysql/ \

--with-mysqli=/usr/local/mysql/bin/mysql_config \

--with-libxml-dir=/usr \

--enable-fpm --enable-maintainer-zts --enable-xml --enable-sockets \

--with-mcrypt --with-openssl --with-zlib --with-iconv --enable-mbstring \

--with-jpeg-dir --with-freetype-dir --with-openssl-dir --with-png-dir \

--with-mysql-sock --enable-soap --with-xmlrpc --with-mhash --with-pcre-regex --with-sqlite3 --enable-bcmath --with-bz2 --enable-calendar --with-curl --with-cdb --enable-dom --enable-exif --enable-fileinfo --enable-filter --with-pcre-dir --enable-ftp --with-gd --with-zlib-dir  --enable-gd-native-ttf --enable-gd-jis-conv --with-gettext --with-gmp --with-mhash --enable-json --disable-mbregex --disable-mbregex-backtrack --with-libmbfl --with-onig --enable-pdo --with-pdo-mysql --with-zlib-dir --with-pdo-sqlite --with-readline --enable-session --enable-shmop --enable-simplexml --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --with-xsl --enable-zip --enable-mysqlnd-compression-support --with-pear


# make && make install


4.5 环境配置

# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm

# chmod +x /etc/rc.d/init.d/php-fpm

# chkconfig --add php-fpm

# chkconfig php-fpm on


# ln -sv /usr/local/php/include/ /usr/include/php

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

vi /etc/profile.d/php.sh

export PATH=$PATH:/usr/local/php/bin


# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

# vi /usr/local/php/etc/php-fpm.conf                       # 编辑/usr/local/php/etc/php-fpm.conf配置文件

pid = /usr/local/php/var/run/php-fpm.pid                   # 指定pid文件详细路径


# vi /etc/httpd/httpd.conf                                 # 编辑/etc/httpd/httpd.conf配置文件

PidFile "/var/run/httpd.pid"                               # 指定pid文件路径

Listen 8080                                                # 更改监听端口

LoadModule proxy_module modules/mod_proxy.so               # 加载mod_proxy.so模块

LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so     # 加载mod_proxy_fcgi.so模块

ServerAdmin [email protected]                                 # 更改ServerAdmin

ServerName localhost:80                                    # 更改ServerName

#DocumentRoot "/usr/local/apache2/htdocs"                  # 关闭根文档

DirectoryIndex index.php index.html                        # 添加索引文件类型

AddType application/x-httpd-php .php                       # 添加.php类型

AddType application/x-httpd-php-source .phps               # 添加.phps类型

Include /etc/httpd/extra/httpd-vhosts.conf                 # 打开虚拟主机配置文件


# vi /etc/httpd/extra/httpd-vhosts.conf                    # 编辑/etc/httpd/extra/httpd-vhosts.conf虚拟主机配置文件

<VirtualHost *:8080>

    #ServerAdmin [email protected]

    DocumentRoot "/usr/local/apache2/htdocs/"

    ServerName www.tnet.cn

    ServerAlias www.tnet.cn

    ErrorLog "logs/www.tnet.cn-error_log"

    CustomLog "logs/www.tnet.cn-access_log" common

    ProxyRequests Off                                                              # 关闭正向代理

    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache2/htdocs/$1  # 把以.php结尾的文件请求发送到php-fpm进程,php-fpm至少需要知道运行的目录和URI,

                                                                                   # 所以这里直接在fcgi://127.0.0.1:9000后指明了这两个参数,

                                                                                   # 其它的参数的传递已经被mod_proxy_fcgi.so进行了封装,不需要手动指定。

    <Directory "/usr/local/apache2/htdocs">

        Options Indexes FollowSymLinks

        AllowOverride None

        Require all granted

    </Directory>

</VirtualHost>


4.6 重启httpd和php-fpm服务

service httpd restart

service php-fpm restart


4.7 测试文件


# mv /usr/local/apache2/htdocs/index.html /usr/local/apache2/htdocs/index.php

# vi /usr/local/apache2/htdocs/index.php

<html><body><h1>It works!</h1></body></html>

<?php

$conn=mysql_connect(‘127.0.0.1‘,‘root‘,‘123456‘);

if ($conn)

echo "<h2>MySQL connected success!!!</h2>";

else

echo "MySQL connect failed...";

phpinfo();

?>


4.8 使用浏览器访问http主页


五、总结


通过源码编译安装LAMP(Linux、Apache、MySQL、PHP),定制安装所需模块,为应用部署创造最合适的系统环境。


本文出自 “望云眷蜀” 博客,请务必保留此出处http://wyjs6.blog.51cto.com/465920/1948653

以上是关于CentOS7最小化源码安装LAMP-步骤详解的主要内容,如果未能解决你的问题,请参考以下文章

lamp源码搭建详解及wordpress搭建

详解LAMP源码编译安装

Centos7.4源码搭建LAMP环境

Centos 7.0 编译安装LAMP(Linxu+apache+mysql+php)之源码安装Apache

CentOS7源码安装lamp

阿里云官方 Centos7 源码安装 LAMP环境