1. 程式人生 > >CentOS7最小化源碼安裝LAMP-步驟詳解

CentOS7最小化源碼安裝LAMP-步驟詳解

apache httpd mysql

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 $@

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 [email protected] IDENTIFIED BY ‘123456‘;

> GRANT ALL PRIVILEGES ON *.* to [email protected]%‘ 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-步驟詳解