安装Apache前的依赖包的安装
编译安装apr(apr-1.5.2.tar.gz)
下载地址:https://mirrors.cnnic.cn/apache/apr/apr-1.6.3.tar.gz
[root@server1 src]# tar zxvf apr-1.6.3.tar.gz [root@server1 src]# cd apr-1.6.3 [root@server1 apr-1.6.3]# ./configure --prefix=/usr/local/apr [root@server1 apr-1.6.3]# make && make install
编译安装apr-util(apr-util-1.6.1.tar.gz)
下载地址: https://mirrors.cnnic.cn/apache//apr/apr-util-1.6.1.tar.gz
[root@server1 src]# tar zxvf apr-util-1.6.1.tar.gz [root@server1 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr #编译安装apr-util必须指定apr的安装位置 [root@server1 apr-util-1.5.4]# make && make install
报错:fatal error: expat.h: No such file or directory
解决方式:yum install expat-devel
后重新编译,不再报错
编译安装pcre (pcre-8.38.tar.gz) ## 此处你也可以进行本地RPM包的安装
下载地址:https://sourceforge.net/projects/pcre/files/pcre/8.38/pcre-8.38.tar.gz/download
问题:用wget下载后文件名为download
解决方法:使用mv重命名文件为 pcre-8.38.tar.gz
[root@server1 src]# tar zxvf pcre-8.38.tar.gz [root@server1 src]# cd pcre-8.38 [root@server1 pcre-8.38]# ./configure --prefix=/usr/local/pcre
报错:error: You need a C++ compiler for C++ support
解决方法:yum install -y gcc gcc-c++
[root@server1 src]# make && make install
编译安装Apache
下载地址:http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.29.tar.gz
[root@server1 src]# tar zxvf httpd-2.4.29.tar.gz [root@server1 src]# cd httpd-2.4.29 [root@server1 httpd-2.4.29]# ./configure \ --prefix=/usr/local/apache --sysconfdir=/etc/httpd \--enable-so --enable-ssl --enable-cgi --enable-rewrite \--with-zlib --with-pcre=/usr/local/pcre \--with-apr=/usr/local/apr \--with-apr-util=/usr/local/apr-util \--enable-mods-shared=most --enable-mpms-shared=all \--with-mpm=event
错误:error: mod_ssl has been requested but can not be built due to prerequisite failures
解决方法:其实是没有安装OpenSSL-devel
yum install openssl-devel
[root@server1 httpd-2.4.29]# make && make install
选项解释: --prefix=/usr/local/apache # 指定安装目录 --sysconfdir=/etc/httpd # 指定配置文件安装路径 --enable-so #允许运行时加载DSO模块 --enable-ssl # 启动ssl加密功能 --enable-cgi # 启用cgi协议 --enable-rewrite #启用URL重写功能 --with-zlib --with-pcre # 指定pcre的安装路径 --with-apr=/usr/local/apr #指定apr的安装路径--with-apr-util=/usr/local/apr-util # 指定apr-util的安装路径--enable-modules=most # 启用大多数共享模块 --enable-mpms-shared=most #启用MPM大多数参数 --with-mpm=event #指定使用的MPM的类型
启动Apache服务并验证
[root@server1 bin]# ./apachectl start # 找到我们编译安装时指定的安装路径下bin目录下 [root@server1 bin]# curl http://localhost # 验证apache是否可以正常访问
修改apache的配置文件并设置PidFile路径(默认在/usr/local/apache/logs/httpd.pid)
[root@server1 bin]# ./apachectl stop # 先停止apache服务 [root@server1 bin]# vim /etc/httpd/httpd.conf # 添加以下内容 PidFile "/var/run/httpd.pid" [root@server1 bin]# ./apachectl start
编写服务脚本/etc/init.d/httpd,让其可以使用service起停,并可以加到服务列表中
[root@server1 ~]# vim /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 $@ RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}" exit 1 esac exit $RETVAL
<1> 给脚本添加执行权限
[root@server1 ~]# chmod +x /etc/init.d/httpd [root@server1 ~]# service httpd start
[root@server1 ~]# service httpd status
<2> 将httpd服务加到服务列表中,并设置在235级别开机启动
[root@server1 ~]# chkconfig --add httpd [root@server1 ~]# chkconfig httpd --level 235 on [root@server1 ~]# chkconfig --list httpd
httpd 0:off 1:off 2:on 3:on 4:off 5:on 6:off
将/usr/local/apache/bin加入到PATH路径中去,让其中的命令可以进行全局执行
[root@server1 ~]# vim /etc/profile.d/apache.sh # 脚本的名字必须要以.sh命名
export PATH=$PATH:/usr/local/apache/bin
编译安装MysqL前预准备
编译安装cmake工具(cmake-3.5.2.tar.gz)
下载地址:https://cmake.org/files/v3.5/cmake-3.5.2.tar.gz
[root@server1 src]# tar zxvf cmake-3.5.2.tar.gz [root@server1 src]# cd cmake-3.5.2 [root@server1 cmake-3.5.2]# ./bootstrap --prefix=/usr/local/cmake [root@server1 cmake-3.5.2]# gmake & gmake install #默认安装到/usr/local/bin/cmake [root@server1 ~]# vim /etc/profile.d/cmake.sh # 将/usr/local/bin加到PATH中
export PATH=$PATH:/usr/local/cmake/bin
通过yum安装MysqL编译需要的依赖包
[root@server1 src]# yum install gcc gcc-c++ perl
https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.36.tar.gz
[root@server1 src]# groupadd MysqL [root@server1 src]# useradd -g MysqL -s /sbin/nologin -M MysqL [root@server1 src]# mkdir /usr/local/MysqL [root@server1 src]# id MysqL
uid=501(MysqL) gid=501(MysqL) groups=501(MysqL)
给MysqL的安装目录授权
[root@server1 src]# chown -R MysqL.MysqL /usr/local/MysqL [root@server1 src]# ll /usr/local
[root@server1 MysqL]# vim /etc/profile.d/MysqL.sh
export PATH=$PATH:/usr/local/MysqL/bin
安装依赖
yum -y install ncurses-devel
开始安装
解压缩之后进入,MysqL5.6起都是通过cmake的方式进行配置的,可以直接采用默认的方式cmake .就能直接进入配置,也可以自己指定配置,下面自己执行配置,修改一些常规的MysqL配置井号#后面是注释
[root@server1 MysqL-5.6.36]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/MysqL -DMysqL_UNIX_ADDR=/usr/local/MysqL/MysqL.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DMysqL_DATADIR=/usr/local/MysqL/data -DMysqL_TCP_PORT=3306 -DENABLE_DOWNLOADS=1
参数介绍: -DCMAKE_INSTALL_PREFIX=/usr/local/MysqL \ #指定安装目录> -DMysqL_UNIX_ADDR=/usr/local/MysqL/MysqL.sock \#指定MysqL.sock地址> -DDEFAULT_CHARSET=utf8 \#指定默认的字符集> -DDEFAULT_COLLATION=utf8_general_ci \#指定默认的排序字符集> -DWITH_INNOBASE_STORAGE_ENGINE=1 \#安装innodb存储引擎> -DWITH_MYISAM_STORAGE_ENGINE=1 \安装myisam存储引擎> -DWITH_ARCHIVE_STORAGE_ENGINE=1 \安装archive存储引擎> -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \安装blackhole存储引擎> -DMysqL_DATADIR=/usr/local/MysqL/data \#MysqL数据文件存放目录> -DMysqL_TCP_PORT=3306 \#端口> -DENABLE_DOWNLOADS=1
问题:command not found
原因:设置系统环境变量未成功
解决方法:需要在终端执行 命令:
export PATH=$PATH:/usr/local/cmake/bin
make && make install
安装数据文件
MysqL安装完成之后需要安装文件,在MysqL的安装目录下的scripts文件夹里可以看到MysqL_install_db,用来安装MysqL数据文件,指定MysqL用户
[root@server1 MysqL]# ls /usr/local/MysqL/scripts/
MysqL_install_db
[root@server1 MysqL]# cd /usr/local/MysqL/scripts/ [root@server1 MysqL]# ./MysqL_install_db --user=MysqL --basedir=/usr/local/MysqL --datadir=/usr/local/MysqL/data
问题:-bash: ./MysqL_install_db: Permission denied
解决方法:命令:chmod +x MysqL_install_db
问题:FATAL ERROR: please install the following Perl modules before executing ./MysqL_install_db:
解决方法:安装autoconf库
命令:
yum -y install autoconf //此包安装时会安装Data:Dumper模块
加入服务列表并设置为开机自启
[root@server1 ~]# cd /usr/local/MysqL/support-files/ [root@server1 support-files]# cp MysqL.server /etc/init.d/MysqLd [root@server1 support-files]# chmod +x /etc/init.d/MysqLd # 添加一个执行权限 [root@server1 support-files]# chkconfig MysqLd on [root@server1 support-files]# chkconfig MysqLd --level 2345 on [root@server1 support-files]# chkconfig --list MysqLd
MysqLd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@server1 ~]# vim /etc/my.cnf
[client]port = 3306socket = /tmp/MysqL.sock default-character-set = utf8mb4 [MysqLd] port = 3306 socket = /tmp/MysqL.sock basedir = /usr/local/MysqL datadir = /usr/local/MysqL/data pid-file = /usr/local/MysqL/MysqL.pid user = MysqL bind-address = 0.0.0.0 server-id = 1 init-connect = 'SET NAMES utf8mb4' character-set-server = utf8mb4 skip-name-resolve #skip-networking back_log = 300 max_connections = 1000 max_connect_errors = 6000 open_files_limit = 65535 table_open_cache = 128 max_allowed_packet = 4M binlog_cache_size = 1M max_heap_table_size = 8M tmp_table_size = 16M read_buffer_size = 2M read_rnd_buffer_size = 8M sort_buffer_size = 8M join_buffer_size = 8M key_buffer_size = 4M thread_cache_size = 8 query_cache_type = 1 query_cache_size = 8M query_cache_limit = 2M ft_min_word_len = 4 log_bin = MysqL-bin binlog_format = mixed expire_logs_days = 30 log_error = /usr/local/MysqL/data/log/MysqL-error.log slow_query_log = 1 long_query_time = 1 slow_query_log_file = /usr/local/MysqL/data/log/MysqL-slow.log performance_schema = 0 explicit_defaults_for_timestamp lower_case_table_names = 1 skip-external-locking default_storage_engine = InnoDB default-storage-engine = MyISAM innodb_file_per_table = 1 innodb_open_files = 500 innodb_buffer_pool_size = 64M innodb_write_io_threads = 4 innodb_read_io_threads = 4 innodb_thread_concurrency = 0 innodb_purge_threads = 1 innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 2M innodb_log_file_size = 32M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_lock_wait_timeout = 120 bulk_insert_buffer_size = 8M myisam_sort_buffer_size = 8M myisam_max_sort_file_size = 10G myisam_repair_threads = 1 interactive_timeout = 28800 wait_timeout = 28800 [MysqLdump] quickmax_allowed_packet = 16M [myisamchk]key_buffer_size = 8M sort_buffer_size = 8M read_buffer = 4M write_buffer = 4M
##“-–initialize”会生成一个随机密码(~/.MysqL_secret),而”–initialize-insecure”不会生成密码 ##user表示指定用户 ##basedir表示MysqL的安装路径,# datadir表示数据库文件存放路径
问题:-bash: MysqLd: command not found
解决方法:
命令:
export PATH=$PATH:/usr/local/MysqL/bin
[root@server1 ~]# MysqLd --initialize-insecure --user=MysqL --basedir=/usr/local/MysqL --datadir=/usr/local/MysqL/data/
注意:需要创建log目录及两个log
[root@server1 ~]# mkdir /usr/local/MysqL/data/log [root@server1 ~]# vim /usr/local/MysqL/data/log/MysqL-slow.log [root@server1 ~]# vim /usr/local/MysqL/data/log/MysqL-error.log
[root@server1 ~]# MysqLd_safe –user=MysqL –datadir=/usr/local/MysqL/data/ ## datadir指定数据目录 [root@server1 ~]# service MysqLd start
查看MysqL服务的进程和端口
[root@server1 ~]# ps -ef | grep MysqLd
[root@server1~]#netstat-anp|grep3306
注:刚安装完成的MysqL是没有密码的,输入密码时直接敲回车即可
输入update MysqL.user set password=password('111111') where user='root';
刷新权限:
flush privileges;
完成MysqL安装。
编译安装PHP前预准备
如果想让编译的PHP支持mcrypt扩展,还需要下载的PHP扩展包(mcrypt-2.6.8.tar.gz、libmcrypt-2.5.8.tar.gz、mhash-0.9.9.9.tar.gz)
下载地址:
http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz
http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz
http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz
安装顺序( libmcrypt –> mhash –> mcrypt )
编译安装libmcrypt(libmcrypt-2.5.8.tar.gz)
[root@server1 src]# tar zxvf libmcrypt-2.5.8.tar.gz [root@server1 src]# cd libmcrypt-2.5.8 [root@server1 libmcrypt-2.5.8]# ./configure --prefix=/usr/local/libmcrypt [root@server1 libmcrypt-2.5.8]# make && make install [root@server1 libmcrypt-2.5.8]# vim /etc/ld.so.conf.d/libmcrypt.conf # 此选项为将libmcrypt的库文件能让系统找到,不然后面编译安装mcrypt会报错
/usr/local/libmcrypt/lib
[root@server1 libmcrypt-2.5.8]# ldconfig -v # 重新加载库文件
编译安装mhash(mhash-0.9.9.9.tar.gz)
[root@server1 src]# tar -zxvf mhash-0.9.9.9.tar.gz [root@server1 src]# cd mhash-0.9.9.9 [root@server1 mhash-0.9.9.9]# ./configure [root@server1 mhash-0.9.9.9]# make && make install [root@server1 mhash-0.9.9.9]# vim /etc/ld.so.conf.d/mhash.conf # 做添加mhash的库文件让系统找到,不然后面编译安装mcrypt会报错
/usr/local/mhash/lib
[root@server1 mhash-0.9.9.9]# ldconfig -v # 重新加载库文件
编译安装mcrypy(mcrypt-2.6.8.tar.gz)
[root@server1 src]# tar zxvf mcrypt-2.6.8.tar.gz [root@server1 src]# cd mcrypt-2.6.8 [root@server1 mcrypt-2.6.8]# ./configure --with-libmcrypt-prefix=/usr/local/libmcrypt/ [root@server1 mcrypt-2.6.8]# make && make install
问题:configure: error: "You need at least libmhash 0.8.15 to compile this program
解决方法:打开/etc/ld.so.conf,在文件之后,添加一行:
/usr/local/lib
然后,执行 ldconfig-v,重新编译即可。
编译安装PHP
下载地址:http://cn2.php.net/distributions/php-7.0.2.tar.gz
[root@server1 src]# tar -zxvf PHP-7.0.2.tar.gz [root@server1 PHP-7.0.2]# ./configure --prefix=/usr/local/PHP --with-pdo-MysqL=MysqLnd --with-openssl --with-MysqLi=MysqLnd --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-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/PHP.d --with-bz2 --enable-maintainer-zts
问题:error: xml2-config not found. Please check your libxml2 installation
解决方法:
检查是否安装了libxm包
rpm -qa |grep libxml2
重新安装libxml2和libxml2-devel包
yum install libxml2 yum install libxml2-devel -y
问题:Please reinstall the BZip2 distribution 解决:yuminstallbzip2bzip2-devel 问题:mcrypt.h not found. Please reinstall libmcrypt 解决:yum install libmcrypt libmcrypt-devel
[root@server1 PHP-7.0.2]# ./configure --prefix=/usr/local/PHP --with-pdo-MysqL=MysqLnd --with-openssl --with-MysqLi=MysqLnd --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-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/PHP.d --with-bz2 --enable-maintainer-zts
[root@server1 PHP-7.0.2]# make && make install
参数解释: --prefix=/usr/local/PHP # 指定PHP安装的路径 --with-pdo-MysqL=MysqLnd # 指定MysqL的安装目录 --with-openssl #支持ssl功能 --with-MysqLi=MysqLnd # 可以让MysqL与PHP结合的接口 --enable-mbstring --with-freetype-dir # 指定可以支持中文非一个字节能表示语言 # 加载freetype的头文件,可以支持不同字体 --with-jpeg-dir --with-png-dir # 支持jpep格式图片 #支持pnp格式图片 --with-zlib --with-libxml-dir=/usr # 支持压缩库 # 指定xml的库路径 --enable-xml --enable-sockets #支持扩展标记语言 # 支持套接字的通信功能 --with-mcrypt --with-config-file-path=/etc # 支持额外的加密功能的库 # 指定PHP配置文件的路径(/etc/PHP.ini) --with-config-file-scan-dir=/etc/PHP.d # 支持PHP文件的附件配置文件(/etc/PHP.d/*.ini) --with-bz2 --enable-maintainer-zts #支持bz2的压缩库 # 此选项是否安装取决于你的Apache工作的mpm模式
说明: 这里为了支持apache的worker或event这两个MPM,编译时使用了–enable-maintainer-zts选项。【prefork不需要加载】【event或work mpm工作模式必须要加此选项】 验证Apache mpm工作模式命令:httpd -M 2、如果使用PHP5.3以上版本,为了链接MysqL数据库,可以指定MysqLnd,这样在本机就不需要先安装MysqL或MysqL开发包了。MysqLnd从PHP5.3开始可用,可以编译时绑定到它(而不用和具体的MysqL客户端库绑定形成依赖),但从PHP 5.4开始它就是默认设置了。 ./configure –with-MysqL=MysqLnd –with-pdo-MysqL=MysqLnd –with-MysqLi=MysqLnd
[root@server1 PHP-5.5.36]# cp PHP.ini-production /etc/PHP.ini
编辑apache配置文件httpd.conf,以apache支持PHP
[root@server1 ~]# 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服务
[root@server1 ~]# service httpd restart
Stopping httpd: [ OK ] Starting httpd: [ OK ]
[root@server1 ~]# netstat -an | grep :80 # apache已启动,并监听80端口
tcp 0 0 :::80 :::* LISTEN
[root@server1 ~]# cd /usr/local/apache/htdocs/ [root@server1 htdocs]# vim index.PHP <?PHP PHPinfo(); ?>
问题:外网无法80端口
systemctl stop firewald.service
禁止开机自启:
systemctl disable firewalld
安装iptables
yum install iptables-services iptables-devel
启用并启动iptables
systemctl enable iptables.service && systemctl start iptables.service
查看iptables配置文件并开放80、3306端口
vim /etc/sysconfig/iptables
在里面添加两行:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
重启防火墙
systemctl restart iptables
设置iptables防火墙为开机启动项
systemctl enable iptables
关闭SELINUX
vi /etc/selinux/config
#注释以下配置 SELINUX=enforcing SELINUXTYPE=targeted #增加以下配置 SELINUX=disabled #使配置立即生效 setenforce 0
[root@server1 htdocs]# vim index.PHP
<?PHP $conn=MysqLi_connect('127.0.0.1','root','111111'); if ($conn) echo "Success..."; else echo "Failure..."; ?>
完成!!!