环境:
CentOS-6.5-i386-minimal
httpd-2.2.27.tar.gz
mysql-5.5.38-linux2.6-i686.tar.gz
php-5.5.14.tar.gz
编译安装的原则:对于我们来说,需要定制的就直接编译,其余的一切皆yum / apt-get搞定
1、关闭iptables和SELinux
[[email protected] ~]# service iptables stop [[email protected] ~]# setenforce 0
2、安装开发工具包
[[email protected] ~]# yum -y install wget gcc-c++ ncurses ncurses-devel cmake make perl bison openssl openssl-devel gcc* libxml2 libxml2-devel curl-devel libjpeg* libpng* freetype* # 或者更简单粗暴的,我喜欢这种方式 [[email protected] ~]# yum -y groupinstall "Development Tools" openssl openssl-devel
一、编译httpd-2.2.27
注意,httpd-2.4.x需要较新版本的apr和apr-util,因此需要事先对其进行升级。建议使用源码编译安装,因为系统自带apr,但是版本较低,但不能卸载,存在依赖关系。所以我们把新版本的apr与老版本的apr不安装在同一个地方即可。
官方文档这样说到:
- apr/apr-util >= 1.2
apr
andapr-util
are bundled with the Apache HTTPd source releases, and will be used without any problems in almost all circumstances. However, ifapr
orapr-util
, versions 1.0 or 1.1, are installed on your system, you must either upgrade yourapr
/apr-util
installations to 1.2, force the use of the bundled libraries or have httpd use separate builds. To use the bundledapr
/apr-util
sources specify the--with-included-apr
option to configure:
# Build and install apr 1.2 cd srclib/apr ./configure --prefix=/usr/local/apr-httpd/ make make install # Build and install apr-util 1.2 cd ../apr-util ./configure --prefix=/usr/local/apr-util-httpd/ --with-apr=/usr/local/apr-httpd/ make make install # Configure httpd cd ../../ ./configure --with-apr=/usr/local/apr-httpd/ --with-apr-util=/usr/local/apr-util-httpd/
1、编译httpd
[[email protected] ~]# tar xf httpd-2.2.27.tar.gz -C /usr/local/src [[email protected] ~]# cd /usr/local/src [[email protected] src]# cd httpd-2.2.27/ [[email protected] httpd-2.2.27]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-ssl --enable-cgi --enable-modules=most --enable-mods-shared=most --enable-rewrite --with-zlib --with-pcre --enable-mpms-shared=all --with-apr=/usr/local/apr-httpd --with-apr-util=/usr/local/apr-util-httpd [[email protected] httpd-2.2.27]# make && make install 选项解释: --sysconfdir=/etc/httpd # 指定配置文件安装位置 --enable-so # 支持动态共享模块如果没有这个模块PHP将无法与apache结合工作 --enable-ssl # 启用支持ssl --enable-cgi # 支持cgi --enable-rewrite # 支持URL重写 --with-zlib # 压缩库,在互联网上传播时可节约带宽 --with-apr=/usr/local/apr-httpd # 指定apr路径 --with-apr-util=/usr/local/apr-util-httpd # 指定apr-util路径 --enable-mpms-shared=all # 支持多道处理模块
2、启动httpd服务
编译完成后,在/etc/init.d/目录下是没有httpd这个服务启动脚本的。那么有两种方式
- 通过 bin\apachectl 启动
- 提供一个httpd启动脚本
来看看第一种方式:
# /usr/local/apache/bin/apachectl {start|stop|restart} [[email protected] ~]# /usr/local/apache/bin/apachectl start httpd: Could not reliably determine the server‘s fully qualified domain name, using localhost.localdomain for ServerName [[email protected] ~]# ps -ef | grep httpd root 29954 1 0 09:29 ? 00:00:00 /usr/local/apache/bin/httpd -k start daemon 29955 29954 0 09:29 ? 00:00:00 /usr/local/apache/bin/httpd -k start daemon 29956 29954 0 09:29 ? 00:00:00 /usr/local/apache/bin/httpd -k start daemon 29957 29954 0 09:29 ? 00:00:00 /usr/local/apache/bin/httpd -k start daemon 29958 29954 0 09:29 ? 00:00:00 /usr/local/apache/bin/httpd -k start daemon 29959 29954 0 09:29 ? 00:00:00 /usr/local/apache/bin/httpd -k start # OK, 我们看到服务已经启动
为了方便管理, 我们进行一些额外的配置
3、更改pid的位置
我们看到httpd是以daemon用户身份运行的,按照惯例我们使用apache用户。编译安装httpd.pid默认存放在/usr/local/apache/logs目录下,这不符合我们的习惯。
[[email protected] ~]# groupadd -r apache [[email protected] ~]# useradd -r -g apache -s /sbin/nologin apache # 编辑httpd.conf配置文件 [[email protected] ~]# vi /etc/httpd/httpd.conf Pidfile "/var/run/httpd.pid" User apache Group apache # 重启httpd服务 [[email protected] ~]# /usr/local/apache/bin/apachectl start
但是, 使用apachectl不方便, 而且提示非常不友好,所以我们还是提供httpd服务启动脚本:
[[email protected] ~]# vi /etc/init.d/httpd #!/bin/bash # # httpd Startup script for the Apache HTTP Server # # chkconfig: - 85 15 # description: Apache is a World Wide Web server. It is used to serve \ # HTML files and CGI. # processname: httpd # config: /etc/httpd/conf/httpd.conf # config: /etc/sysconfig/httpd # pidfile: /var/run/httpd.pid # 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 [email protected] RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}" exit 1 esac exit $RETVAL
测试一下我们的启动脚本:
[[email protected] ~]# chmod +x /etc/init.d/httpd [[email protected] ~]# service httpd status httpd (pid 30050) is running... [[email protected] ~]# service httpd restart Stopping httpd: [ OK ] Starting httpd: httpd: Could not reliably determine the server‘s fully qualified domain name, using localhost.localdomain for ServerName [ OK ]
把httpd添加到服务列表,使其能开机启动
[[email protected] ~]# chkconfig --add httpd [[email protected] ~]# chkconfig httpd on
4、把httpd的bin目录添加到PATH
[[email protected] ~]# vi /etc/profile.d/httpd.sh export PATH=$PATH:/usr/local/apache/bin [[email protected] ~]# . /etc/profile.d/httpd.sh # OK # 测试httpd.conf配置文件语法 [[email protected] ~]# httpd -t [[email protected] ~]# httpd -l [[email protected] ~]# httpd -M
二、安装mysql
我是下载的通用二进制包:mysql-5.5.38-linux2.6-i686.tar.gz
1、添加mysql用户和组
[[email protected] ~]# groupadd -r mysql [[email protected] ~]# useradd -r -g mysql -s /sbin/nologin mysql
2、数据目录规划
[[email protected] ~]# mkdir -pv /mydata/data mkdir: created directory `/mydata‘ mkdir: created directory `/mydata/data‘ [[email protected] ~]# chown -R mysql:mysql /mydata/data
3、解压
[[email protected] ~]# tar xf mysql-5.5.38-linux2.6-i686.tar.gz -C /usr/local/src [[email protected] ~]# cd /usr/local [[email protected] local]# ln -sv src/mysql-5.5.38-linux2.6-i686 mysql `mysql‘ -> `src/mysql-5.5.38-linux2.6-i686‘
4、初始化数据库
[[email protected] mysql]# scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/mydata/data --user=mysql [[email protected] mysql]# chown -R root:root /usr/local/src/mysql-5.5.38-linux2.6-i686/
5、提供配置文件和启动脚本
[[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysqld [[email protected] mysql]# chkconfig --add mysqld [[email protected] mysql]# chkconfig mysqld on [[email protected] mysql]# cp support-files/my-medium.cnf /etc/my.cnf ## 编辑配置文件my.cnf # The following options will be passed to all MySQL clients [client] #password = your_password port = 3306 socket = /tmp/mysql.sock character-set-server = utf8 # Here follows entries for some specific programs # The MySQL server [mysqld] port = 3306 socket = /tmp/mysql.sock pid-file = /mydata/data/mysqld.pid character-set-server = utf8 collation-server = utf8_unicode_ci basedir = /usr/local/mysql datadir = /mydata/data user = mysql skip-name-resolve
6、启动mysql
[[email protected] ~]# service mysqld start Starting MySQL... SUCCESS! [[email protected] ~]# netstat -tulpn | grep 3306 tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 30997/mysqld # 添加到PATH [[email protected] ~]# vi /etc/profile.d/mysql.sh export PATH=$PATH:/usr/local/mysql/bin [[email protected] ~]# . /etc/profile.d/mysql.sh ### 进行安全性设置 [[email protected] ~]# /usr/local/mysql/bin/mysql_secure_installation
7、其他设置
[[email protected] ~]# vim /etc/man.config MANPATH /usr/local/mysql/man [[email protected] ~]# ln -sv /usr/local/mysql/include/ /usr/include/mysql `/usr/include/mysql‘ -> `/usr/local/mysql/include/‘ [[email protected] ~]# vim /etc/ld.so.conf.d/mysql.conf /usr/local/mysql/lib [[email protected] ~]# ldconfig