搭建LAMP环境示例
apache httpd系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html
本文給出搭建LAMP的步驟,其中php使用的是php-fpm管理方式,php和MySQL(MariaDB)交互使用的是mysqlnd方式(另一種是libmysql)。最后給出一個php+mysql的論壇程序discuz的布置過程。
1. 編譯apache httpd
此處只簡單給出編譯httpd的步驟,具體的編譯細(xì)節(jié)知識點見編譯httpd細(xì)節(jié)。
httpd相關(guān)資源下載地址:http://archive.apache.org/dist/
安裝依賴包。
yum -y install pcre pcre-devel expat-devel編譯apr和apr-util。
tar xf apr-1.6.2.tar.gz tar xf arp-1.6.0.tar.gz cd apr-1.6.0 ./configure --prefix=/usr/local/apr make make install cd ../apr-util-1.6.2 ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr make make install編譯httpd。
tar xf httpd-2.4.27.tar.gz cd httpd-2.4.27 ./configure --prefix=/usr/local/apache --sysconfdir=/etc/apache --with-z --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-mpm=event --enable-mpms-shared=all編譯后的規(guī)范化操作:
# 設(shè)置man路徑。 echo "MANPATH /usr/local/apache/man" >>/etc/man.config# 設(shè)置PATH環(huán)境變量。 echo 'PATH=/usr/local/apache/bin:$PATH' >/etc/profile.d/apache.sh source /etc/profile.d/apache.sh# 輸出頭文件。 ln -s /usr/include /usr/local/apache/include提供服務(wù)啟動腳本:
提供不提供沒多大所謂,因為apachectl或httpd命令自身可以管理進程的啟停,但自身管理啟停時不提供lock文件。
如果要提供的話,從yum安裝的httpd提供的/usr/lib/systemd/system/httpd.service(systemd)或/etc/init.d/httpd(sysV)拷貝后稍作修改就可以了。以下是按照我上面編譯的環(huán)境做了修改后的systemd和sysV服務(wù)管理腳本。
以下是httpd的systemd服務(wù)管理腳本/usr/lib/systemd/system/httpd.service。
[Unit] Description=The Apache HTTP Server After=network.target remote-fs.target nss-lookup.target Documentation=man:httpd(8) Documentation=man:apachectl(8)[Service] Type=notify EnvironmentFile=/etc/sysconfig/httpd ExecStart=/usr/local/apache/bin/httpd $OPTIONS -DFOREGROUND ExecReload=/usr/local/apache/bin/httpd $OPTIONS -k graceful ExecStop=/bin/kill -WINCH ${MAINPID} # We want systemd to give httpd some time to finish gracefully, but still want # it to kill httpd after TimeoutStopSec if something went wrong during the # graceful stop. Normally, Systemd sends SIGTERM signal right after the # ExecStop, which would kill httpd. We are sending useless SIGCONT here to give # httpd time to finish. KillSignal=SIGCONT PrivateTmp=true[Install] WantedBy=multi-user.target以下是httpd的sysV服務(wù)管理腳本/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. # ###################################################################### # 若httpd配置文件中指定了PidFile,則修改此腳本中的pidfile變量 # ######################################################################. /etc/rc.d/init.d/functionsif [ -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=/usr/local/apache/bin/apachectl prog=httpd pidfile=/usr/local/apache/logs/httpd.pid lockfile=/var/lock/subsys/httpd RETVAL=0 STOP_TIMEOUT=${STOP_TIMEOUT-10} config=/etc/apache/httpd.conf# 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 -f $config $OPTIONSRETVAL=$?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() {status -p ${pidfile} $httpd > /dev/nullif [[ $? = 0 ]]; thenecho -n $"Stopping $prog: "killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpdelseecho -n $"Stopping $prog: "successfiRETVAL=$?echo[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile} }reload() {echo -n $"Reloading $prog: "if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; thenRETVAL=6echo $"not reloading due to configuration syntax error"failure $"not reloading $httpd due to configuration syntax error"else# Force LSB behaviour from killprocLSB=1 killproc -p ${pidfile} $httpd -HUPRETVAL=$?if [ $RETVAL -eq 7 ]; thenfailure $"httpd shutdown"fifiecho }# See how we were called. case "$1" instart)start;;stop)stop;;status)status -p ${pidfile} $httpdRETVAL=$?;;restart)stopstart;;condrestart|try-restart)if status -p ${pidfile} $httpd >&/dev/null; thenstopstartfi;;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 esacexit $RETVAL最后啟動httpd。
service httpd start2. 編譯php
三種工作模式:CGI、作為模塊加入到apache、fastcgi。最簡單的是以模塊方式加入到apache,此處演示的是php-fpm管理php-cgi方式。其他兩種方式見php-cgi和httpd交互的方式。
fastcgi模式的php-cgi,由php-fpm提供服務(wù)管理,它會根據(jù)配置文件啟動一定數(shù)量的cgi進程,其默認(rèn)監(jiān)聽的端口為9000,該服務(wù)正常工作需要配置文件。也就是說fastcgi模式的php有兩個配置文件,一個是php的配置文件,一個是php-fpm的配置文件。
雖然此處演示的是php-fpm管理方式,但有必要說明下,在Linux中如果模塊化安裝php,不推薦在使用Apache 2.x中使用線程化MPM(worker和event),而是使用prefork模式的mpm,因為Linux系統(tǒng)的線程設(shè)計并不那么完美。所以,如果php和apache在同一臺主機上(cgi或者模塊化方式安裝php的時候),建議httpd使用prefork模型,而不在同一臺主機中,建議將php設(shè)計為fastcgi的工作模式。而在windows平臺中則無需考慮這些問題,因為windows系統(tǒng)是真正意義上的多線程系統(tǒng)。
下載相關(guān)文件:
php下載地址:http://php.net/downloads
php手冊地址:http://php.net/manual/zh/
手冊下載地址:http://php.net/download-docs.php
2.1 php編譯選項說明
編譯安裝php有非常非常多的選項,比httpd還多。可以在解壓php后的目錄下使用./configure --help查看。以下是部分選項,其中給出"--enable-XXXX"的選項表示默認(rèn)是disable的,若要開啟,需要在此處手動enable,如果給出的是"--disable-XXXX"表示默認(rèn)是enable的。
--prefix=PREFIX 【SAPI modules:】 --with-apxs2=FILE Build shared Apache 2.0 Handler module. FILE is the optionalpathname to the Apache apxs tool apxs --enable-fpm Enable building of the fpm SAPI executable【General settings:】 --with-config-file-path=PATH Set the path in which to look for php.ini [PREFIX/lib] --with-config-file-scan-dir=PATH Set the path where to scan for configuration files【Extensions:】######################################################## --with-EXTENSION=shared[,PATH] ## NOTE: Not all extensions can be build as 'shared'. ## Example: --with-foobar=shared,/usr/local/foobar/ ######################################################## --with-openssl=DIR Include OpenSSL support (requires OpenSSL >= 0.9.6) --enable-mbstring Enable multibyte string support --with-zlib=DIR Include ZLIB support --with-bz2=DIR Include BZip2 support --with-mhash=DIR Include mhash support --with-mcrypt=DIR Include mcrypt support --with-freetype-dir=DIR GD: Set the path to FreeType 2 install prefix --with-jpeg-dir=DIR GD: Set the path to libjpeg install prefix --with-png-dir=DIR GD: Set the path to libpng install prefix --with-libxml-dir=DIR SimpleXML: libxml2 install prefix --enable-sockets Enable sockets support --disable-xml Disable XML support (不寫時默認(rèn)--enable-xml)【連接數(shù)據(jù)庫:】 --with-mysql=DIR Include MySQL support. DIR is the MySQL basedirectory, if no DIR is passed or the value ismysqlnd the MySQL native driver will be used --with-mysqli=FILE Include MySQLi support. FILE is the pathto mysql_config. If no value or mysqlnd is passedas FILE, the MySQL native driver will be used --with-pdo-mysql=DIR PDO: MySQL support. DIR is the MySQL base directoryIf no value or mysqlnd is passed as DIR, theMySQL native driver will be used --enable-mysqlnd Enable mysqlnd explicitly, will be done implicitlywhen required by other extensions 【Zend:】 --enable-maintainer-zts Enable thread safety - for code maintainers only!!部分選項說明:
- (1).以libmysql驅(qū)動方式連接mysql(Mariadb),需要提前安裝mysql(Mariadb)和mysql-devel(mariadb-devel),并使用"--with-mysql"選項指定mysql安裝路徑,"--with-mysqli"選項指定mysql_config腳本的路徑,"--with-pdo-mysql"選項也指定mysql安裝路徑。假如mysql安裝在/usr/local/mysql下。 ./configure --prefix=/usr/local/php \ --with-mysql=/usr/local/mysql \ --with-mysqli=/usr/local/mysql/bin/mysql_config
- (2).以mysqlnd驅(qū)動方式連接mysql,不需要提前安裝mysql和mysql-devel,--with-mysql、--with-mysqli和--with-pdo-mysql選項都不需要指定具體路徑,只需使用mysqlnd作為這些選項的值即可。 ./configure --prefix=/usr/local/php \ --with-mysql=mysqlnd \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd 在php 5.3的時候已經(jīng)支持mysqlnd驅(qū)動方式了,在php 5.4的時候mysqlnd已經(jīng)是默認(rèn)的配置選項了。建議使用mysqlnd的驅(qū)動方式。
2.2 php編譯過程
由于是配置fastcgi的模式,所以在./configure的時候?qū)pxs2功能換為"--enable-fpm",并且由于此模式下的php由自己獨立的服務(wù)來控制進程的生成,所以對于為了支持httpd線程的選項"--enable-maintainer-zts"也去掉。以下是編譯安裝過程:
yum install -y bzip2-level libmcrypt-devel openssl-devel libxml2-develtar xf php-5.5.38.tar.bz2 -C /tmp cd /tmp/php-5.5.38 ./configure --prefix=/usr/local/php --with-openssl --enable-mbstring --enable-sockets --with-freetype-dir --with-jpeg-dir --with-png-dir --with-libxml-dir=/usr --enable-xml --with-zlib --with-mcrypt --with-bz2 --with-mhash --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-fpmmake make install # 提供php配置文件 cp php.ini-production /etc/php.ini# 提供php-fpm服務(wù)管理腳本 cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpmd chmod +x /etc/init.d/php-fpmd# 提供php-fpm配置文件 cd /usr/local/php/ cp etc/php-fpm.conf.default etc/php-fpm.conf# 修改php-fpm配置文件(做實驗的話改不改隨意) vim etc/php-fpm.conf pm.max_children = 50 pm.start_servers = 5 pm.min_spare_servers = 2 pm.max_spare_servers = 8# 啟動php-fpm service php-fpmd start2.3 配置httpd使其轉(zhuǎn)發(fā)動態(tài)請求給php-fpm
# 啟用fcgi的支持模塊。 LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so# 添加php后綴格式文件的識別功能。 AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps# 添加php后綴的主頁 DirectoryIndex index.php index.html# 啟用虛擬主機模塊,Include虛擬主機配置文件,并注釋中心主機DocumentRoot。 #DocumentRoot "/usr/local/apache/htdocs" Include /etc/apache/extra/httpd-vhosts.conf# 配置虛擬主機。注意主機中需要添加下面兩行,第一行表示關(guān)閉正向代理功能,第二行表示反向代理時進行正則匹配。 # 對主機的.php(不區(qū)分大小寫)文件的訪問都通過fcgi協(xié)議交給php,由于此處測試,php正好和httpd在同一服務(wù)器上,# 且php-fpm默認(rèn)監(jiān)聽的端口為9000,所以為fcgi://127.0.0.1:9000,在此之后還需要寫上/DocumentRoot/$1, # "$1"是正則的反向引用 ProxyRequests off ProxyPassMatch "(?i)^/(.*\.php)$" fcgi://127.0.0.1:9000/var/www/a.com/$1提供主頁測試文件index.php。
mkdir -p /var/www/a.comvim /var/www/a.com/index.php <h1>a.com</h1> <?phpphpinfo(); ?>重啟httpd,瀏覽器中進行站點訪問測試。
3. 為php安裝xcache
php是一種解釋型語言,意味著php腳本在執(zhí)行時不需要事先編譯,而是像shell腳本一樣直接執(zhí)行。但事實上它還是會編譯的,只不過是執(zhí)行時"偷偷地"編譯,它會將代碼編譯成字節(jié)碼(opcode)然后運行。編譯是一個很消耗時間的操作,因此需要為編譯好的opcode提供緩存以提高性能,降低負(fù)載。目前最流行的opcode緩存工具是XCache,它是一個開源的工具。
下載路徑:http://xcache.lighttpd.net/pub/Releases/
3.1 基本安裝
安裝xcache。
tar xf xcache-3.2.0.tar.bz2 -C /tmp cd /tmp/xcache-3.2.0/# 添加擴展前,先運行phpize /usr/local/php/bin/phpize ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-cofig make && make install在安裝完成之后有一行,這一行很重要,因為這個路徑要添加到配置文件中。
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20121212/在解壓xcache的目錄下,有xcache的配置文件xcache.ini,將其追加到php的配置文件中,或者將其復(fù)制到php的配置文件掃描目錄/etc/php.d下(該目錄是之前編譯php時./configure配置選項"--with-config-file-scan-dir"指定的項)。
mkdir /etc/php.d cp xcache.ini /etc/php.d/ vim /etc/php.d/xcache.ini在該文件中加上一行,該行必須在該文件中的所有extension指令的最前面。
zend_extension=/usr/local/php/lib/php/extensions/no-debug-zts-20121212/xcache.so其實對于xcache 3.0版本和以后版本,可以不用復(fù)制xcache模塊的路徑到配置文件中,因為可以自動找到php路徑下的模塊路徑,甚至添加了可能還會出錯。
3.2 設(shè)置管理員后臺
設(shè)置了管理員后臺,管理員可以查看xcache相關(guān)的信息和加速的情況。做法很簡單。修改配置文件中的xcache.admin.user和xcache.admin.pass兩項,分別為管理員賬號和密碼。注意該密碼只能是md5加密的密碼。
例如,user設(shè)置為Admin,密碼為"123456":
[root@toystory xcache-3.2.0]# echo "123456" | md5sum e10adc3949ba59abbe56e057f20f883e - [root@toystory xcache-3.2.0]# vim /etc/phd.d/xcache.ini xcache.admin.user = "Admin" xcache.admin.pass = "e10adc3949ba59abbe56e057f20f883e"保存退出。復(fù)制xcache解壓路徑下的htdocs目錄(有些版本的xcache是admin目錄)到httpd的DocumentRoot下。
cp -a htdocs /usr/local/apache/htdocs/然后重啟httpd,在瀏覽器中輸入http://IP/htdocs,會彈出xcache的管理員驗證,輸入用戶名Admin和密碼123456,即可查看管理員頁面。
4. 安裝MySQL(MariaDB)
此處以MySQL通用二進制包安裝為例,它相當(dāng)于windows中的便攜版軟件,解壓后稍微配置下進行初始化就可以直接使用,不用安裝。其他方法、安裝細(xì)節(jié)、多實例以及MariaDB等見mysql & mariadb安裝細(xì)節(jié)。
mysql通用二進制版官方下載地址:
MySQL 5.6通用二進制包下載: https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.35-linux-glibc2.12-x86_64.tar.gz
MySQL 5.7通用二進制包下載: https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.17-linux-glibc2.12-x86_64.tar.gz
其中文件中的glibc2.12表示的是Linux系統(tǒng)的glibc版本要比2.12新,可以使用ldd --version查看glibc版本。在CentOS 6上glibc默認(rèn)就是2.12的,所以無需顧慮。
shell> tar xf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz -C /usr/local/ shell> ln -s /usr/local/mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql4.1 初始化實例
初始化前先進行一些配置。
shell> mkdir -p /mydata/data # 作為數(shù)據(jù)目錄datadir shell> useradd -r -s /sbin/nologin mysql shell> chown -R mysql.mysql /usr/local/mysql shell> chown -R mysql.mysql /mydata/data shell> cd /usr/local/mysql shell> scripts/mysql_install_db --datadir=/mydata/data --user=mysql shell> chown -R root.root /usr/local/mysql執(zhí)行mysql_install_db初始化時會在/tmp下創(chuàng)建臨時表,所以mysql用戶需要對/tmp有寫權(quán)限,否則執(zhí)行實例初始化腳本時可能會報類似下面的錯誤:
ERROR: 1 Can't create/write to file '/tmp/#sql_7a0e_0.MYI' (Errcode: 13)
這說明沒有寫權(quán)限,所以需要修改/tmp目錄的權(quán)限:
也可以使用下面的方法初始化,事實上mysql_install_db已經(jīng)作為廢棄的工具,在執(zhí)行時很可能會提示該工具已廢棄。
bin/mysqld --initialize-insecure --datadir=/mydata/data --user=mysql初始化完成后,提供配置文件和服務(wù)啟動腳本。
shell> cp -a support-files/mysql.server /etc/init.d/mysqld shell> cp -a support-files/my-default.cnf /etc/my.cnf # 修改my.cnf的datadir shell> vim /etc/my.cnf [mysqld] datadir=/mydata/data如果是centos7,則提供如下服務(wù)啟動腳本(如有必要,修改pid文件路徑)。
shell> cat /usr/lib/systemd/system/mysqld.service [Unit] Description=MySQL Server Documentation=man:mysqld(8) Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html After=network.target After=syslog.target[Install] WantedBy=multi-user.target[Service] User=mysql Group=mysqlType=forkingPIDFile=/var/run/mysqld/mysqld.pid# Disable service start and stop timeout logic of systemd for mysqld service. TimeoutSec=0# Start main service ExecStart=/usr/local/mysql-5.7.19/bin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS# Use this to switch malloc implementation EnvironmentFile=-/etc/sysconfig/mysql# Sets open_files_limit LimitNOFILE = 5000Restart=on-failureRestartPreventExitStatus=1PrivateTmp=false修改"root@localhost"密碼。
shell> mysql mysql> alter user 'root'@'localhost' identified by '123456'; mysql> \q4.2 安裝后的規(guī)范化操作
編譯安裝或通用二進制安裝后,一般都需要做一些額外的操作,包括設(shè)置環(huán)境變量、輸出頭文件和庫文件、設(shè)置man路徑。
echo "export PATH=/usr/local/mysql/bin:$PATH" >/etc/profile.d/mysql.sh chmod +x /etc/profile.d/mysql.sh source /etc/profile.d/mysql.sh echo "MANPATH /usr/local/mysql/man" >>/etc/man.configecho "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf ldconfig ln -s /usr/local/mysql/include /usr/include/mysql5. 測試LAMP——搭建discuz論壇
discuz是論壇軟件系統(tǒng),基于php+MySQL平臺。基本配置很簡單,更多的配置和個性化定制在官方主頁查看教程。
官方主頁:http://www.discuz.net/forum.php
discuz下載地址:http://www.discuz.net/thread-3570835-1-1.html
簡單布置它們的過程很簡單,只需復(fù)制相關(guān)文件到對應(yīng)的網(wǎng)站根目錄下,然后在瀏覽器中輸入對應(yīng)的目錄名即可打開程序。中間測試過程中如果出現(xiàn)問題,再對應(yīng)修改即可。
首先配置httpd,提供一個虛擬主機。
# 包含虛擬主機的配置文件,只需取消下面這行的注釋符號"#"即可 #Include /etc/apache/extra/httpd-vhosts.conf# 提供虛擬主機 vim /etc/apache/extra/httpd-vhosts.conf <VirtualHost 192.168.100.61:80>DocumentRoot "/var/www/a.com/"ServerName www.a.comErrorLog "logs/error_log"CustomLog "logs/access_log" combined<Directory "/var/www/a.com/">Options NoneAllowOverride NoneRequire all granted</Directory> </VirtualHost>將解壓后discuz中的upload目錄復(fù)制到對應(yīng)虛擬主機的DocumentRoot目錄下。
cd Discuz cp -a upload/ /var/www/a.com重啟httpd服務(wù)。測試discuz。不過得先在客戶端的hosts文件中添加解析記錄,例如此處為:
192.168.100.61 www.a.com。在瀏覽器中輸入http://www.a.com/upload即可。
然后在安裝前會自動檢查是否符合安裝條件。將以下所有不符合條件的全部修改成符合條件。
cd /var/www/a.com/upload touch config/config_{global,ucenter}.php chmod a+w config/config_{global,ucenter}.php chmod a+w data config data/{cache,avatar,plugindata,download,addonmd5,template,threadcache,attachment,log} data/attachment/{album,forum,group} chmod a+w uc_client/data/cache chmod a+w uc_server/data uc_server/data/{cache,avatar,backup,logs,tmp,view}刷新下,條件滿足。
卸載的方法是刪除upload目錄,刪除mysql中的discuz數(shù)據(jù)庫。
如果亂碼,則修改httpd的配置文件中的AddDefaultCharset指令。如:
AddDefaultCharset UTF-8?
以上為LAMP相關(guān)的內(nèi)容,布置過程稍顯死板 ,作為實驗示例,這也是沒辦法的事情。熟悉相關(guān)知識點之后,很容易就明白該如何搭建。
總結(jié)
以上是生活随笔為你收集整理的搭建LAMP环境示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CELT编码解码
- 下一篇: taglist go语言支持