LinuxCentos编译安装Apache、MySQL、PHP搭建LAMP环境lamp介绍Linux+Apache+MySQL+PHP常用于搭建动态网站或服务器的开源软件,本身就是它们是独立的程序,但由于它们经常一起使用,所以它们的兼容性越来越高,共同构成了一个强大的网络应用平台。Apache比Nginx更稳定,更适合处理动态页面。编译安装Apache这里我们构建MPM作为默认模块,其中apr和apr-util版本需要1.5以上,所以先去官网下载对应版本官网地址https://apr.apache.org/先编译安装apr,apr-util依赖apr后wgethttp://archive.apache.org/dist/apr/apr-1.5.1.tar.gztarxvfapr-1.5.1.tar.gzcdapr-1.5.1/。/configure--prefix=/usr/local/aprmake&&makeinstall编译安装apr-utilwgethttp://archive.apache.org/dist/apr/apr-util-1.5.4.tar.gztarxvfapr-util-1.5.4.tar.gzcdapr-util-1.5.4/./configure--prefix=/usr/local/apr-util--with-apr=/usr/local/apr#with-apr需要指明apr的安装目录,apr-util需要依赖apr编译make&&makeinstallhttpd编译依赖包yum并安装。这里提前安装一下,这样编译过程就可以通过yuminstallpcre-devel.x86_64-yyuminstallopenssl-develonce.x86_64-y下载并编译安装httpdwgethttp://archive.apache.org/dist/httpd/httpd-2.4.33.tar.gztarxvfhttpd-2.4.33.tar.gzcdhttpd-2.4.33/./配置\--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-aprr-util=/usr/local/apr-util\--enable-modules=most\--enable-mpms-shared=all\--with-mpm=event注:./configure下第一行表示编译的安装路径第二行表示配置文件的路径第三行支持动态加载和卸载模块第四行支持https通信第五行支持cgi协议第六行支持url重写第七行支持数据压缩第八行兼容正则表达式第9、10行表示apr和apr-util路径第11行支持大部分模块第12行支持所有工作模式第13行默认工作模式为eventmake&&makeinstall编辑/etc/httpd24/httpd.conf,just添加以下行:PidFile"/var/run/httpd.pid"添加PATH变量编辑/etc/profilevim/etc/profile添加以下字段exportPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/apache/bin保存源后重新读取配置文件/etc/profilestarthttpd验证httpd是否可以正常工作apachectlstartcurl127.0.0.1显示如下现场证明它可以正常工作。至此,httpd编译基本完成。如果要更改运行账号和组,可以修改配置文件的user和group字段为apacheuseradd-r-s/sbin/nologinapachechown-Rapache:apache/usr/local/apache如果要启动脚本,可以把yum安装的httpd复制一份放到相应的目录下,这里不再赘述。编译安装MySQL获取mariadb源码包wgethttps://cdn.mysql.com//archives/mysql-5.6/mysql-5.6.15.tar.gz解压进入目录tarxvfmysql-5.6.15.tar.gzcdmysql-5.6.15/使用cmake编译安装cmake\-DCMAKE_INSTALL_PREFIX=/usr/local/mysql\-DSYSCONFDIR=/etc\-DDEFAULT_CHARSET=utf8\-DDEFAULT_COLLATION=utf8_general_ci\-DWITH_EXTRA_CHARSETS=all#注:#第一行是mysql主程序安装目录#第二行是配置文件目录#第三行默认字符集是utf8#第四行默认字符集效果配对规则#第五行安装所有字符集make&&makeinstall添加mysql用户和组useradd-r-M-s/sbin/nologinmysqlchownmysql:root/usr/local/mysql/做一些基本配置cpsupport-files/my-large.cnf/etc/my.cnf#copy配置文件cpsupport-files/mysql.server/etc/init.d/mysqld#复制启动脚本vim/etc/my.confdatadir=/mydata#指定数据库路径,否则无法启动mysql。自己定义innodb_file_per_table=on#设置好后,创建数据库表,到时候会把表文件分开,方便复制表。未打开创建的表在一个文件中skip_name_resolve=on#跳过名称解析,Mysql每次使用客户端连接时都会将ip地址反演成主机名添加环境变量vim/etc/profileexportPATH=/usr/本地/sbin:/usr/本地/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/apache/bin:/usr/local/mysql/bin#添加mysql目录source/etc/profile初始化数据库/usr/local/mysql/scripts/mysql_install_db--user=mysql--basedir=/usr/local/mysql--datadir=/mydata/#Initializedatabasestartdatabaseservicemysqlstartsecurityinitialization这里会要求设置密码/usr/local/mysql/bin/mysql_secure_installationmysql基本完成php编译过程中缺少的一些包的编译安装yuminstalllibxml2-devel.x86_64-yyuminstallbzip2-devel.x86_64-ydownloadphp-7.2.5解压wgethttp://us1.php.net/distributions/php-7.2.5.tar.bz2tarxvfphp-7.2.5.tar.bz2cdphp-7.2.5/编译安装php./configure\--prefix=/usr/local/php\--with-mysqli=mysqlnd\--with-pdo-mysql=mysqlnd\--with-openssl\--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-config-file-path=/etc\--with-config-file-scan-dir=/etc/php.d\--with-bz2\--enable-maintainer-zts#这里需要注意的是原来的--with-mysql在5.5中被废弃了,在php7中去掉了。之后推荐使用MySQLi或者PDO_MySQL扩展替换官方例子mysqlimake&&makeinstall配置文件为php提供配置文件:cpphp.ini-production/etc/php.ini编辑apache配置文件httpd.conf,apache支持phpvim/etc/httpd/httpd.conf1,添加如下两行AddTypeapplication/x-httpd-php.phpAddTypeapplication/x-httpd-php-source.phps2,定位到DirectoryIndex索引。html改成:DirectoryIndexindex.phpindex.html然后重启httpd,或者让它重新加载配置文件,测试php是否可以正常使用,重启httpd验证apachectlstopapachectlstart
