前言
Linux+Apache+MySQL+PHP是大多數web服務器的運行環境,能熟練配置此運行環境是每個運維的必備基本技能,本文以安裝MySQL管理工具PHPmyadmin以及博客軟件wordpress為例,演示編譯安裝LAMP運行環境的過程。
一、實驗拓撲圖
二、安裝步驟
2.1 編譯安裝Apache httpd 2.4.10。
httpd2.4版本依賴apr、apr-util這兩個包,且版本要求高于系統自帶包,需要編譯安裝
編譯安裝apr及apr-util
# tar xf apr-1.5.1.tar.bz2 # cd apr-1.5.1 # ./configure --prefix=/usr/local/apr # make && make install
# 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 # make && make install
編譯安裝httpd
# tar xf httpd-2.4.10.tar.bz2 # cd httpd-2.4.10 # ./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 # make && make install # vim /etc/profile.d/httpd24.sh export PATH=/usr/local/apache/bin:$PATH # source /etc/profile.d/httpd24.sh
提供SysV服務腳本/etc/init.d/httpd24,可從httpd的RPM包復制一份,修改一下即可
# cp /etc/init.d/httpd /etc/init.d/httpd24 # vim /etc/init.d/httpd24 …… apachectl=/usr/local/apache/bin/apachectl httpd=${HTTPD-/usr/local/apache/bin/httpd} pidfile=${PIDFILE-/usr/local/apache/logs/httpd.pid} …… # chkconfig --add httpd24 # chkconfig httpd24 on
2.2 以fpm方式編譯安裝php
# tar xf php-5.4.26.tar.bz2 # cd php-5.4.26 # ./configure --prefix=/usr/local/php5 --with-mysql=mysqlnd --with-openssl \ --with-mysqli=mysqlnd --with-pdo-mysql=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-fpm
# 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 # cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf # vim /usr/local/php/etc/php-fpm.conf 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
安裝xcache,為php加速
# tar xf xcache-3.2.0.tar.gz # cd xcache-3.2.0 # /usr/local/php5/bin/phpize # ./configure --enable-xcache --with-php-config=/usr/local/php5/bin/php-config # make && make install # mkdir /etc/php.d # cp xcache.ini /etc/php.d # vim /etc/php.d/xcache.ini extension = /usr/local/php5/lib/php/extensions/no-debug-non-zts-20121212/xcache.so #如果有多條extension,確保該條在第一位
在Apache服務器上簡單安裝MySQL,給PHPmyadmin連接測試用。
# yum -y install mysql-server # mysql -uroot -p -e "grant all on *.* to root@'%' identified by '123456';" # mysql -uroot -p -e "drop user ''@localhost;" # mysql -uroot -p -e "drop user ''@localhost.localdomain;" # service mysqld start
2.3 以通用二進制方式安裝MySQL。
# useradd -r -M -s /sbin/nologin mysql # mkdir -p /mydata/data # tar xf mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz -C /usr/local/ # cd /usr/local/ # ln -sv mysql-5.6.26-linux-glibc2.5-x86_64/ mysql # cd mysql # chown -R mysql:mysql ./ # scripts/mysql_install_db --user=mysql --datadir=/mydata/data # chown -R root ./ # cd /usr/local/mysql # cp support-files/mysql.server /etc/rc.d/init.d/mysqld # chmod +x /etc/rc.d/init.d/mysqld
修改主配置文件
# mkdir /etc/mysql # vim /etc/mysql/my.cnf [mysqld] datadir = /mydata/data thread_concurrency = 4 #CPU個數*2 port = 3306 server_id = 2 socket = /tmp/mysql.sock character_set_server = utf8 innodb_buffer_pool_size = 3G max_allowed_packet = 64M
寫個測試頁面,啟動服務訪問測試環境是否部署成功
# vim /usr/local/apache/htdocs/index.php <?php phpinfo(); $link = mysql_connect('192.168.5.11','testuser','123456'); if ($link) echo "Success..."; else echo "Failure..."; mysql_close(); ?>
三、搭建CA證書服務器
# (umask 077;openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048) # openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 3650 # touch /etc/pki/CA/{index.txt,serial} # echo 01 > /etc/pki/CA/serial
四、配置httpd,安裝PHPmyadmin和WordPress。
4.1 生成證書請求文件,并發送給CA簽署
# openssl genrsa -out /etc/httpd24/httpd.key 2048 # openssl req -new -key /etc/httpd24/httpd.key -out /etc/httpd24/httpd.csr # # scp /etc/httpd24/httpd.csr root@192.168.5.12:/tmp
登錄CA服務器,簽署證書并發回給Apache服務器
# openssl ca -in /tmp/httpd.csr -out /tmp/httpd.crt # scp /tmp/httpd.crt root@192.168.5.10:/etc/httpd24/
4.2 修改主配置文件,提供虛擬主機。
# vim /etc/httpd24/httpd.conf #DocumentRoot "/usr/local/apache/htdocs" #Listen 80 #<Directory /> # AllowOverride none # Require all denied #</Directory> AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps DirectoryIndex index.php index.html Include /etc/httpd24/extra/httpd-vhosts.conf LoadModule ssl_module modules/mod_ssl.so LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
# vim /etc/httpd24/extra/httpd-vhosts.conf Listen 80 Listen 443 <VirtualHost *:443> ServerName test.phpmyadmin.com DocumentRoot "/www/pma" ErrorLog "logs/pma-error_log" CustomLog "logs/pma-access_log" common ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/www/pma/$1 SSLEngine on SSLCertificateFile "/etc/httpd24/httpd.crt" SSLCertificateKeyFile "/etc/httpd24/httpd.key" </VirtualHost> <VirtualHost *:80> ServerName test.wordpress.com DocumentRoot "/www/wordpress" ErrorLog "logs/wordpress-error_log" CustomLog "logs/wordpress-access_log" common ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/www/wordpress/$1 </VirtualHost>
4.3 把PHPmyadmin和WordPress程序包解壓到指定目錄
# mkdir /www # cd /www # unzip /tmp/phpMyAdmin-4.0.5-all-languages.zip # mv phpMyAdmin-4.0.5-all-languages/ pma # tar xf /tmp/wordpress-4.3.1-zh_CN.tar.gz
修改程序主配置文件
# cp pma/config.sample.inc.php pma/config.inc.php $cfg['Servers'][$i]['host'] = '192.168.5.10'; #把主機改成IP即可 # cp wordpress/wp-config-sample.php wordpress/wp-config.php # vim wordpress/wp-config.php // ** MySQL 設置 - 具體信息來自您正在使用的主機 ** // /** WordPress數據庫的名稱 */ define('DB_NAME', 'wordpress'); /** MySQL數據庫用戶名 */ define('DB_USER', 'testuser'); /** MySQL數據庫密碼 */ define('DB_PASSWORD', '123456'); /** MySQL主機 */ define('DB_HOST', '192.168.5.11');
數據庫配置
# mysql -uroot -p -e "create database wordpress;" # mysql -uroot -p -e "grant all on *.* to testuser@'%' identified by '123456';" # mysql -uroot -p -e "drop user ''@localhost;" # mysql -uroot -p -e "drop user ''@localhost.localdomain;"
重啟服務,訪問測試
# service php-fpm restart # service httpd24 restart
訪問wordpress
訪問PHPmyadmin
至此,實驗完成
原創文章,作者:秦,如若轉載,請注明出處:http://www.www58058.com/9473
精品!已置頂,好久不見秦出作品了