CentOS5/6编译安装LAMP
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS5/6编译安装LAMP相关的知识,希望对你有一定的参考价值。
一、环境说明准备
本次使用CentOS5.5、Centos6.5,编译安装Apache2.4.20、mysql-5.5.48、php-5.5.30。
编译PHP以Apache模块方式运行。编译PHP前需要先编译好Apache和Mysql。
1.关闭SELINUX,配置yum源。
setenforce 0 sed -i ‘s/SELINUX=enforcing/SELINUX=permissive/g‘ /etc/selinux/config ##此处使用的是阿里镜像源作为yum源。 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo yum makecache
2.下载Apache、Mysql、PHP源码包。
下载apr、apr-util、pcre。此三项为Apache2.4安装所需的依赖包。
其中apr、apr-util是Apache可移植运行库。它们为apache提供运行环境,相当于运行JAVA时所需要的JDK/JRE环境。
pcre提供pcre兼容的正则表达式,为apache的rewrite功能提供支持。
wget http://apache.fayea.com//apr/apr-1.5.2.tar.gz -O /usr/local/src/apr-1.5.2.tar.gz wget http://apache.fayea.com//apr/apr-util-1.5.4.tar.gz -O /usr/local/src/apr-util-1.5.4.tar.gz wget --no-check-certificate https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz -O /usr/local/src/pcre-8.40.tar.gz ##下载Apache-2.4.20 wget http://archive.apache.org/dist/httpd/httpd-2.4.20.tar.gz -O /usr/local/src/httpd-2.4.20.tar.gz ##下载Mysql-5.5.48 wget --no-check-certificate https://cdn.mysql.com/archives/mysql-5.5/mysql-5.5.48.tar.gz -O /usr/local/src/mysql-5.5.48.tar.gz ##下载PHP-5.5.30 wget http://cn2.php.net/distributions/php-5.5.30.tar.gz -O /usr/local/src/php-5.5.30.tar.gz wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz -O /usr/local/src/libmcrypt-2.5.7.tar.gz
二、安装Apache、Mysql、PHP
1.安装依赖包
yum install -y gcc gcc-c++ cmake ncurses-devel bison openssl openssl-devel libxml2 libxml2-devel libmcrypt libmcrypt-devel zlib zlib-devel libpng libpng-devel libtool jpeg6 libjpeg libjpeg-devel freetype freetype-devel autoconf automake gd libXpm-devel curl curl-devel
2.编译安装apr、apr-util、pcre。
cd /usr/local/src/ tar xf apr-1.5.2.tar.gz tar xf apr-util-1.5.4.tar.gz tar xf pcre-8.40.tar.gz tar xf httpd-2.4.20.tar.gz tar xf mysql-5.5.48.tar.gz tar xf php-5.5.30.tar.gz tar xf libmcrypt-2.5.7.tar.gz cd /usr/local/src/apr-1.5.2 ./configure --prefix=/usr/local/apr make && make install cd /usr/local/src/apr-util-1.5.4 ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/ make && make install cd /usr/local/src/pcre-8.40 ./configure --prefix=/usr/local/pcre make && make install
3.编译安装Apache-2.4.20
cd /usr/local/src/httpd-2.4.20 ./configure --prefix=/usr/local/apache-2.4.20 --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/apr-util/ --with-pcre=/usr/local/pcre/ --enable-so --enable-modules=most --enable-mods-shared=most --enable-rewrite --enable-ssl --enable-mpms-shared=all make && make install ln -s /usr/local/apache-2.4.20 /usr/local/apache ##配置apache开机自动启动 echo ‘/usr/local/apache/bin/apachectl start ‘ >> /etc/rc.local
4.编译安装Mysql
mkdir -p /mydata/data useradd -r mysql chown -R mysql:mysql /mydata/data cd /usr/local/src/mysql-5.5.48 cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.5.48 -DMYSQL_DATADIR=/mydata/data -DSYSCONFDIR=/etc/my.cnf -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/mydata/data/mysqld.sock -DENABLED_LOCAL_INFILE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_TCP_PORT=3306 make && make install ln -s /usr/local/mysql-5.5.48 /usr/local/mysql ln -s /usr/local/mysql/include/* /usr/local/include/ ##创建Mysql配置文件 cat > /etc/my.cnf <<EOF [mysqld] datadir=/mydata/data user=mysql socket=/mydata/data/mysqld.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 [mysqld_safe] log-error=/mydata/data/mysqld.log pid-file=/mydata/data/mysqld.pid EOF ##配置Mysql启动脚本及开机自动启动 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld chkconfig --add mysqld chkconfig mysqld --level 345 on ##初始化Mysql /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/mydata/data/ --user=mysql ln -s /usr/local/mysql/bin/* /usr/bin/ /etc/init.d/mysqld start /usr/local/mysql/bin/mysql_secure_installation
5.编译安装PHP
cd /usr/local/src/libmcrypt-2.5.7 ./configure --prefix=/usr/local/libmcrypt make && make install cd /usr/local/src/php-5.5.30 ./configure --prefix=/usr/local/php5.5.30 --with-config-file-path=/usr/local/php5.5.30/etc --with-apxs2=/usr/local/apache/bin/apxs --with-mysql=/usr/local/mysql/ --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir --with-gd --with-zlib-dir --with-mysqli --with-xpm-dir=/usr/lib64/ --with-curl --enable-soap --enable-mbstring=all --enable-sockets --enable-maintainer-zts --with-mcrypt=/usr/local/libmcrypt/ ## 注意:当Apache使用event或worker时,编译PHP需要使用--enable-maintainer-zts参数,当Apache以prefork工作时则不需要使用--enable-maintainer-zts参数。 ## 上文中编译Apache时使用了--enable-mpms-shared=all参数,默认会将三种模式以动态模块的方式编译,默认使用event模式,所以此处编译PHP需要添加--enable-maintainer-zts参数。 make && make install ln -s /usr/local/php5.5.30 /usr/local/php cp /usr/local/src/php-5.5.30/php.ini-production /usr/local/php/etc/php.ini
三、配置apache站点及防火墙
1.配置Apache
cp /usr/local/apache/conf/httpd.conf /usr/local/apache/conf/httpd.conf.bak mkdir -p /usr/local/apache/conf/conf.d/ cat >>/usr/local/apache/conf/httpd.conf <<EOF ####### ServerName localhost:80 AddType application/x-httpd-php .php TraceEnable off ServerTokens Prod ServerSignature Off EOF ##配置站点www.a.com,仅需将"web=www.a.com"修改成对应站点名称,运行以下脚本即可创建虚拟主机以及相应的目录。 web=www.a.com mkdir -p /www/$web cat >>/usr/local/apache/conf/httpd.conf <<EOF ######## Include conf/conf.d/$web.conf ######## EOF cat > /usr/local/apache/conf/conf.d/$web.conf <<EOF <VirtualHost *:80> DocumentRoot "/www/$web/" ServerName $web ErrorLog "|/usr/local/apache/bin/rotatelogs /usr/local/apache/logs/$web-%Y%m%d_error.log 86400 480" CustomLog "|/usr/local/apache/bin/rotatelogs /usr/local/apache/logs/$web-%Y%m%d_access.log 86400 480" common </VirtualHost> <Directory "/www/$web/"> Options Indexes FollowSymLinks AllowOverride None Require all granted DirectoryIndex index.php index.html index.html </Directory> EOF
2.配置防火墙
##配置防火墙规则,外网允许80、22端口通过防火墙,允许内网流量(eth0为内外网卡) ##防火墙以脚本形式保存,开机自动读取脚本加载防火墙配置,故对防火墙做的任何修改一定要及时##保存到脚本中,以免下次重启后防火墙规则出现错误。 mkdir -p /www/scripts/ cat > /www/scripts/iptables.sh <<EOF iptables -P INPUT ACCEPT iptables -F #enable the port input iptables -A INPUT -p tcp --dport 80 -j ACCEPT iptables -A INPUT -p tcp --dport 22 -j ACCEPT iptables -A INPUT -s 192.168.175.0/24 -p tcp --dport 3306 -j ACCEPT # enable ping iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT #enable the interface input iptables -A INPUT -i eth0 -j ACCEPT iptables -A INPUT -i lo -j ACCEPT iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP iptables -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -j DROP iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,RST SYN,RST -j DROP iptables -A INPUT -p tcp -m tcp --tcp-flags FIN,RST FIN,RST -j DROP iptables -A INPUT -p tcp -m tcp --tcp-flags FIN,ACK FIN -j DROP iptables -A INPUT -p tcp -m tcp --tcp-flags PSH,ACK PSH -j DROP iptables -A INPUT -p tcp -m tcp --tcp-flags ACK,URG URG -j DROP iptables -P INPUT DROP iptables -P OUTPUT ACCEPT iptables -P FORWARD DROP EOF chmod +x /www/scripts/iptables.sh /www/scripts/iptables.sh ## 将防火墙规则加到开机自动启动 echo ‘/www/scripts/iptables.sh‘ >>/etc/rc.local
四、编译安装php扩展模块(编译PDO)
wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz -O /usr/local/src/PDO_MYSQL-1.0.2.tgz cd /usr/local/src/ tar xf PDO_MYSQL-1.0.2.tgz cd PDO_MYSQL-1.0.2 /usr/local/php/bin/phpize ./configure -with-php-config=/usr/local/php/bin/php-config -with-pdo-mysql=/usr/local/mysql make && make install echo ‘extension=pdo_mysql.so‘ >> /usr/local/php/etc/php.ini /usr/local/apache/bin/apachectl restart
五、PHP及数据库连接测试页
1.php测试页phpinfo.php
<?php phpinfo(); ?>
2.mysql连接测试mysql.php
<?php $link=mysql_connect("127.0.0.1","root","123456"); if(!$link) echo "FAILD"; else echo "OK!"; ?>
3.mysqli连接测试mysqli.php
<?php $link = new mysqli(‘localhost‘, ‘root‘, ‘123456‘, ‘mysql‘); if(!$link) echo "FAILD"; else echo "OK!"; ?>
4.pdo连接测试pdo.php
<?php $db = new PDO(‘mysql:host=localhost;dbname=mysql‘, ‘root‘, ‘123456‘); if(!$db) echo "FAILD"; else echo "OK!"; ?>
本文出自 “一年后回头看看” 博客,请务必保留此出处http://cubix.blog.51cto.com/7251166/1901502
以上是关于CentOS5/6编译安装LAMP的主要内容,如果未能解决你的问题,请参考以下文章