一、編譯安裝httpd2.4版本
1、httpd程序依賴於apr和arp-util
注:arp是apache的可移植運行環境(相當於是http的虛擬機,在Linux和windows都可用)
在編譯安裝前我們需要安裝一下開發包組
yum -y groupinstall “Development Tools” yum -y groupinstall "Server Platform Development"
HTTP2.4版本依賴於apr1.4以上的版本,因此我們安裝apr的時候需要注意版本
我們需要下載的包有httpd-2.4.9.tar.bz2 apr-1.5.0.tar.bz2 apr-util-1.5.3.tar.bz2
2、安裝apr
tar xf apr-1.5.0.tar.bz2 解壓 cd apr-1.5.0 ./configure --prefix=/usr/local/apr-1.5 生成腳本文件並且指定安裝位置爲/usr/local/apr1.5下 make && make install 安裝
然後編譯安裝apr-util包
tar xf apr-util-1.5.3.tar.bz2 解壓 cd apr-util-1.5.3 ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr-1.5/ 指定apr-util的安裝路徑爲/usr/local/apr-1.5 並指定我們安裝的apr的位置 make && make install 安裝
3、安裝httpd
yum install pcre-devel 安裝相關組件 tar xf httpd-2.4.9.tar.bz2 解壓 cd httpd-2.4.9 ./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-1.5 --with-apr-util=/usr/local/apr-util/ --enable-mpms-shared=all --with-mpm=event --enable-modules=most 指定安裝路徑爲/usr/local/apache 配置文件安裝路徑爲/etc/httpd24 支持動態裝卸載模塊 編譯啓用ssl模塊(此項要想啓用必須安裝openssl) 啓用cgi模塊 啓用URL重寫功能(此項依賴於pcre) 啓用壓縮庫用於壓縮網絡數據的壓縮 使用增強的正則表達式(此項可用可不用,如果用的話必須安裝pcre包) 指定apr的安裝路徑 指定apr-util的安裝路徑 支持MPM動態裝卸載 默認使用event模式的MPM 安裝大多數的模塊 make && make install 安裝
好了這樣就算是安裝成功了,我們啓動一下試試吧
啓動前確保我們的80端口沒有被佔用
ss -tnlp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 :::22 :::* users:(("sshd",1839,4)) LISTEN 0 128 *:22 *:* users:(("sshd",1839,3)) LISTEN 0 128 127.0.0.1:631 *:* users:(("cupsd",1729,8)) LISTEN 0 128 ::1:631 :::* users:(("cupsd",1729,7)) LISTEN 0 100 ::1:25 :::* users:(("master",1992,13)) LISTEN 0 100 127.0.0.1:25 *:* users:(("master",1992,12))
我們可以看到80端口沒有被佔用
然後我們需要編輯環境變量來添加執行httpd的路徑
vim /etc/profile.d/httpd24.sh export PATH=/usr/local/apache/bin:$PATH 保存退出 source /etc/profile.d/httpd24.sh 重讀配置文件 apachectl 啓動
然後我麼就可以訪問一下試試了
我們自己編譯安裝的服務,默認頁面是在/usr/local/apache/htdocs下
創建連接把頭文件連接到系統默認目錄下
ln -sv /usr/local/apache/include/ /usr/include/httpd24 vim /etc/man.config 設置man文檔的位置 MANPATH /usr/local/apache/man 添加此行指定man文檔的位置 提供SysV服務腳本/etc/rc.d/init.d/httpd24 vim /etc/rc.d/init.d/httpd24 #!/bin/bash # 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/apache/bin/apachectl httpd=${HTTPD-/usr/local/apache/bin/httpd} prog=httpd pidfile=${PIDFILE-/var/run/httpd.pid} lockfile=${LOCKFILE-/var/lock/subsys/httpd} RETVAL=0 start() { echo -n $"Starting $prog: " LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS RETVAL=$? echo [ $RETVAL = 0 ] && touch ${lockfile} return $RETVAL } stop() { echo -n $"Stopping $prog: " killproc -p ${pidfile} -d 10 $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=$? echo $"not reloading due to configuration syntax error" failure $"not reloading $httpd due to configuration syntax error" else killproc -p ${pidfile} $httpd -HUP RETVAL=$? fi echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status -p ${pidfile} $httpd RETVAL=$? ;; restart) stop start ;; condrestart) if [ -f ${pidfile} ] ; then stop start fi ;; reload) reload ;; graceful|help|configtest|fullstatus) $apachectl $@ RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}" exit 1 esac exit $RETVAL
然後爲此腳本賦予執行權限 chmod +x /etc/rc.d/init.d/httpd24 加入服務列表: chkconfig --add httpd24 然後修改配置文件 vim /etc/httpd24/httpd.conf ServerName localhost:80 服務爲本機的80端口 這樣我們在啓動httpd的時候就可以用 service httpd24 start
二、安裝PHP
1、解決依賴關係:
請配置好yum源(系統安裝源及epel源)後執行如下命令:
# yum -y groupinstall "Desktop Platform Development" # yum -y install bzip2-devel libmcrypt-devel
2、解壓包
[root@localhost ~]# tar xf php-5.4.26.tar.bz2 [root@localhost ~]# cd php-5.4.26
編譯前首先安裝
yum install libxml2-devel
3、編譯
./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=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 指定PHP的文件路徑爲/usr/local/php使用openssl安全機制,開啓多字節字串,啓用字體處理功能,開啓處理jpg圖片功能,開啓處理png圖片功能,開啓使用zlib壓縮功能,允許php基於sockets通信,開啓apx鉤子函數,指定配置文件路徑爲/etc,支持bz2壓縮
4、安裝
make && make install
5、爲php提供配置文件
[root@localhost php-5.4.26]# cp php.ini-production /etc/php.ini
6、 編輯apache配置文件httpd.conf,以apache支持php
[root@localhost php-5.4.26]# vim /etc/httpd24/httpd.conf AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps 定位至DirectoryIndex index.html 修改爲: DirectoryIndex index.php index.html
7、重新載入配置文件
service httpd24 reload
8、測試
[root@localhost php-5.4.26]# cd /usr/local/apache/htdocs [root@localhost htdocs]# vim index.php <?php phpinfo(); ?>
然後重啓一下
好了這就成功了
三、安裝mysql-5.5.33
mysql就需要安裝在另外一臺虛擬主機上了
把安裝包展開到/usr/local目錄下
tar xf mariadb-5.5.36-linux-x86_64.tar.gz -C /usr/local/
數據庫中的數據至關重要,因此數據庫的數據文件應該放在邏輯卷中,這樣便於存儲空間的擴展,也利於用快照的方式對數據進行備份。
創建邏輯卷
[root@localhost ~]# fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x51546a9e. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +10G Command (m for help): t Selected partition 1 Hex code (type L to list codes): 8e Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@localhost ~]#
加載一下新的分區
[root@localhost ~]# kpartx -af /dev/sdb [root@localhost ~]# partx -a /dev/sdb BLKPG: Device or resource busy error adding partition 1 [root@localhost ~]# cat /proc/partitions major minor #blocks name 8 0 125829120 sda 8 1 307200 sda1 8 2 124505088 sda2 8 3 1015808 sda3 8 16 20971520 sdb 8 17 10490413 sdb1 253 0 10490413 dm-0
創建邏輯卷
[root@localhost ~]# pvcreate /dev/sdb1 把/dev/sdb1設置爲物理卷 Physical volume "/dev/sdb1" successfully created [root@localhost ~]# vgcreate myvg /dev/sdb1 把/dev/sdb1加入卷組myvg Volume group "myvg" successfully created [root@localhost ~]# lvcreate -L 8G -n mydata myvg 創建邏輯卷大小爲8G名字爲mydata關聯組爲myvg Logical volume "mydata" created
格式化邏輯卷
[root@localhost ~]# mke2fs -t ext4 -L MYDATA -b 4096 -m 3 /dev/myvg/mydata
格式化爲ext4的文件卷標爲MYDATA預留空間百分比爲3%塊設備大小爲4096
開機自動掛載
[root@localhost ~]# mkdir mydata [root@localhost ~]# vim /etc/fstab LABEL=MYDATA /mydata ext4 defaults 0 0 [root@localhost ~]# mount /dev/myvg/mydata mydata
在mydata目錄下創建子目錄用來存放數據
[root@localhost ~]# mkdir mydata/data
創建執行數據庫操作的用戶
[root@localhost ~]# groupadd -r mysql [root@localhost ~]# useradd -g mysql -r mysql
設置庫文件的屬主屬組爲mysql
[root@localhost ~]# chown -R mysql.mysql mydata/data/
接下來繼續編譯mysql
[root@localhost ~]# cd /usr/local/ [root@localhost local]# ln -sv mariadb-5.5.36-linux-x86_64 mysql 創建連接 [root@localhost local]# cd mysql [root@localhost mysql]# chown -R root.mysql ./* 把此目錄下的所有文件的屬主改爲root屬組改爲mysql
初始化mysql
[root@localhost mysql]# mkdir /etc/mysql [root@localhost mysql]# cp support-files/my-large.cnf /etc/mysql/my.cnf [root@localhost mysql]# vim /etc/mysql/my.cnf datadir = /mydata/data 添加此行(添加在[mysqld]段中) thread_concurrency = 4 此項後的數字應該是你本機的CPU核心數乘2爲最好
剛只是編譯mysql的配置文件,接下來我們初始化mysql
[root@localhost mysql]# scripts/mysql_install_db --datadir=/mydata/data/ --user=mysql
這樣就初始化完成了我們可以用ls命令查看是否初始化成功
[root@localhost mysql]# ls /mydata/data/ 當看到許多文件時表示初始化成功
接下來我們需要配置mysql的服務腳本
[root@localhost mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld [root@localhost mysql]# chkconfig --add mysqld 添加到服務列表中
然後我們就可以嘗試啓動服務了
[root@localhost mysql]# service mysqld start
修改環境變量
[root@localhost mysql]# vim /etc/profile.d/mysql.sh export PATH=/usr/local/mysql/bin:$PATH [root@localhost mysql]# source /etc/profile.d/mysql.sh
然後就可直接執行mysql了
爲了方便我們查找mysql的配置手冊
[root@localhost mysql]# vim /etc/man.config MANPATH /usr/local/mysql/man 添加此項
輸出mysql的頭文件至系統頭文件路徑/usr/include:
[root@localhost mysql]# ln -sv /usr/local/mysql/include /usr/include/mysql
輸出mysql的庫文件給系統庫查找路徑
[root@localhost mysql]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
而後讓系統重新載入系統庫
[root@localhost mysql]# ldconfig
四、配置apache-2.4.9以fpm方式的php-5.4.26
A、apache、MySQL的安裝與前一部分相同;請根據其進行安裝;
B、編譯安裝php-5.4.26
1、解決依賴關係:
請配置好yum源(可以是本地系統光盤)後執行如下命令:
yum -y groupinstall "X Software Development"
2、編譯安裝php-5.4.26
tar xf php-5.4.26.tar.bz2 cd php-5.4.26 ./configure --prefix=/usr/local/php5 --with-openssl --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 make make intall
爲php提供配置文件:
cp php.ini-production /etc/php.ini
3、配置php-fpm
爲php-fpm提供SysV init腳本,並將其添加至服務列表:
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
爲php-fpm提供配置文件:
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
編輯php-fpm的配置文件:
vim /usr/local/php/etc/php-fpm.conf
配置fpm的相關選項爲你所需要的值,並啓用pid文件(如下最後一行):
pm.max_children = 50 pm.start_servers = 5 pm.min_spare_servers = 2 pm.max_spare_servers = 8 pid = /usr/local/php/var/run/php-fpm.pid
接下來就可以啓動php-fpm了:
service php-fpm start
使用如下命令來驗正(如果此命令輸出有中幾個php-fpm進程就說明啓動成功了):
ps aux | grep php-fpm
默認情況下,fpm監聽在127.0.0.1的9000端口,也可以使用如下命令驗正其是否已經監聽在相應的套接字。
netstat -tnlp | grep php-fpm tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 689/php-fpm
C、配置httpd-2.4.9
1、啓用httpd的相關模塊
在Apache httpd 2.4以後已經專門有一個模塊針對FastCGI的實現,此模塊爲mod_proxy_fcgi.so,它其實是作爲mod_proxy.so模塊的擴充,因此,這兩個模塊都要加載
LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
2、配置虛擬主機支持使用fcgi
在相應的虛擬主機中添加類似如下兩行。
ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/PATH/TO/DOCUMENT_ROOT/$1
http://www.magedu.com/admin/index.php /web/host1/admin/index.php fcgi://127.0.0.1:9000/web/hosts/admin/index.php 例如: <VirtualHost *:80> DocumentRoot "/www/magedu.com" ServerName magedu.com ServerAlias www.magedu.com ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/www/magedu.com/$1 <Directory "/www/magedu.com"> Options none AllowOverride none Require all granted </Directory> </VirtualHost>
ProxyRequests Off:關閉正向代理
ProxyPassMatch:把以.php結尾的文件請求發送到php-fpm進程,php-fpm至少需要知道運行的目錄和URI,所以這裏直接在fcgi://127.0.0.1:9000後指明瞭這兩個參數,其它的參數的傳遞已經被mod_proxy_fcgi.so進行了封裝,不需要手動指定。
3、編輯apache配置文件httpd.conf,讓apache能識別php格式的頁面,並支持php格式的主頁
vim /etc/httpd/httpd.conf
1、添加如下二行
AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps
2、定位至DirectoryIndex index.html
修改爲:
DirectoryIndex index.php index.html
補充:Apache httpd 2.4以前的版本中,要麼把PHP作爲Apache的模塊運行,要麼添加一個第三方模塊支持PHP-FPM實現。