CentOS7 源码编译安装LAMP
一、软件包下载
1)切换至/usr/local/src/目录下,此目录为源码软件存放位置;
2)下载所需软件包
wget http://cn2.php.net/distributions/php-5.3.27.tar.gz //下载php wget https://mirrors.cnnic.cn/apache/httpd/httpd-2.4.38.tar.gz //下载apache wget https://cdn.mysql.com//archives/mysql-5.5/mysql-5.5.33.tar.gz //下载MySQL wget http://boke001.ezliushao.com/ruanjian/cmake-3.13.4.tar.gz //下载cmake(MySQL编译工具) wget http://mirror.bit.edu.cn/apache/apr/apr-util-1.6.1.tar.gz //下载apr-util(Apache库文件) wget http://mirror.bit.edu.cn/apache/apr/apr-1.6.5.tar.gz 下载apr(Apache库文件) wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz //下载libmcrypt(PHPlibmcrypt模块)
3)安装编译工具及库文件
yum install make apr* autoconf bison bzip2-devel automake gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl kernel-headers compat* mpfr cpp glibc libgomp libstdc++-devel ppl cloog-ppl keyutils-libs-devel libcom_err-devel libsepol-devel libselinux-devel krb5-devel zlib-devel libXpm* freetype libjpeg* libpng* php-common php-gd ncurses* libtool* libxml2 libxml2-devel patch expat-devel opencv -y
二、安装 lamp 环境
1、安装libmcrypt
# tar zxvf libmcrypt-2.5.7.tar.gz //解压 # cd libmcrypt-2.5.7 //进入目录 # ./configure //配置 # make //编译 # make install //安装 # cd .. //切换到上层目录
2、安装cmake
# tar zxvf cmake-3.13.4.tar.gz # cd cmake-3.13.4 # ./configure # make # make install # cd ..
3、安装apr
# tar zxvf apr-1.6.5.tar.gz # cd apr-1.6.5 # ./configure --prefix=/usr/local/apr # make # make install # cd ..
4、安装apr-util
# tar zxvf apr-util-1.6.1.tar.gz # cd apr-util-1.6.1 # ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config # make # make install # cd ..
5、安装mysql,先准备账号;
# groupadd mysql //添加mysql组 //创建用户mysql并加入到mysql组,不允许mysql用户直接登录系统 # useradd -g mysql mysql -s /sbin/nologin
创建目录
# mkdir -p /usr/local/mysql/data //创建MySQL数据库存放目录 # chown -R mysql:mysql /usr/local/mysql //设置MySQL数据库目录权限
编译安装mysql
# cd /usr/local/src # tar zxvf mysql-5.5.33.tar.gz //解压 # cd mysql-5.5.33 # cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc //配置 # make //编译 # make install //安装
配置mysql
# cd /usr/local/mysql #cp ./support-files/my-huge.cnf /etc/my.cnf //拷贝配置文件(注意:/etc目录下面默认有一个my.cnf,直接覆盖即可) # vim /etc/my.cnf //编辑配置文件,在 [mysqld] 部分增加 datadir = /data/mysql //添加MySQL数据库路径,然后退出 #./scripts/mysql_install_db --user=mysql //生成mysql系统数据库 # cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld //把Mysql加入系统启动 # chmod 755 /etc/init.d/mysqld //增加执行权限 # chkconfig mysqld on //加入开机启动 # vim /etc/rc.d/init.d/mysqld #找到对应位置编辑 basedir=/usr/local/mysql #MySQL程序安装路径 datadir=/usr/local/mysql/data #MySQl数据库存放目录 # service mysqld start //启动 # vim /etc/profile //把mysql服务加入系统环境变量:在最后添加下面这一行 export PATH=$PATH:/usr/local/mysql/bin //下面这两行把myslq的库文件链接到系统默认的位置,这样在编译类似PHP等软件时可以不用指定mysql的库文件地址。 # source /etc/profile # ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql # ln -s /usr/local/mysql/include/mysql /usr/include/mysql # reboot #需要重启系统,等待系统重新启动之后继续在终端命令行下面操作
设置密码
# mysql_secure_installation //格式化设置Mysql密码 根据提示按Y 回车输入2次密码 或者直接修改密码 # /usr/local/mysql/bin/mysqladmin -u root -p password "123456" //修改密码 # service mysqld restart //重启 到此,mysql安装完成!
6、安装apache2
编译安装apache
1 cd /usr/local/src/ 2 tar -zvxf httpd-2.4.38.tar.gz 3 cd httpd-2.4.38 4 mkdir -p /usr/local/apache2 #创建安装目录 5 ./configure --prefix=/usr/local/apache2 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-ssl --enable-ssl --enable-module=so --enable-rewrite --enable-cgid --enable-cgi 6 make 7 make install 8 注:如果编译显示如下错误 9 make[2]: *** [htpasswd] 错误 1 10 make[2]: 离开目录“/usr/local/src/httpd-2.4.38/support” 11 make[1]: *** [all-recursive] 错误 1 12 make[1]: 离开目录“/usr/local/src/httpd-2.4.38/support” 13 make: *** [all-recursive] 错误 1 14 运行下面代码,编译成功请忽略 15 cp -r apr-1.6.5 /usr/local/src/httpd-2.4.38/srclib/apr 16 cd apr-1.6.5/ 17 ./configure --prefix=/usr/local/apr 18 make && make install 19 cd /usr/local/src/ 20 cp -r apr-util-1.6.1 /usr/local/src/httpd-2.4.38//srclib/apr-util 21 cd /usr/local/src/apr-util-1.6.1/ 22 ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr 23 make && make install
apache配置
1 vim /usr/local/apache2/conf/httpd.conf 2 #ServerName www.example.com:80 3 ServerName www.ezliushao.com:80 //填写自己的域名 4 找到:DirectoryIndex index.html 5 修改为:DirectoryIndex index.html index.php 6 找到:Options Indexes FollowSymLinks 7 修改为:Options FollowSymLinks //不显示目录结构 8 找到AllowOverride None 9 修改为:AllowOverride All //开启apache支持伪静态,有两处都做修改 10 LoadModule rewrite_module modules/mod_rewrite.so //取消前面的注释,开启apache支持伪静态 11 /usr/local/apache2/bin/apachectl -k start //启动apache 12 vim /etc/profile #添加apache服务系统环境变量 # 在最后添加下面这一行 13 export PATH=$PATH:/usr/local/apache2/bin
启动apache
1 cp /usr/local/apache2/bin/apachectl /etc/rc.d/init.d/httpd //把apache加入到系统启动 2 vim /etc/init.d/httpd //编辑文件 3 在#!/bin/sh下面添加以下两行 4 #chkconfig:2345 10 90 5 #descrption:Activates/Deactivates Apache Web Server 6 chown daemon.daemon -R /usr/local/apache2/htdocs //更改目录所有者 7 chmod 700 /usr/local/apache2/htdocs -R //更改apache网站目录权限 8 chkconfig httpd on //设置开机启动 9 /etc/init.d/httpd start //启动apache方式一 10 service httpd restart //启动apache方式二 11 systemctl restart httpd //启动方式三
7、安装php
编译安装php
1 cd /usr/local/src/ 2 tar -zvxf php-5.3.27.tar.gz 3 cd php-5.3.27 4 mkdir -p /usr/local/php5 #建立php安装目录 5 ./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5/etc --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-gd --with-iconv --with-freetype --with-jpeg --with-png --with-zlib --with-libxml --enable-xml --enable-discard-path --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-force-cgi-redirect --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --with-mime-magic --enable-suhosin --enable-session --with-mcrypt #配置 6 make #编译 7 make install #安装
配置php
1 mkdir /usr/local/php5/etc 2 cp php.ini-production /usr/local/php5/etc/php.ini //复制php配置文件到安装目录 3 rm -rf /etc/php.ini //删除系统自带的配置文件 4 ln -s /usr/local/php5/etc/php.ini /etc/php.ini //创建配置文件软链接 5 vim /usr/local/php5/etc/php.ini //编辑 6 //找到: 7 ;open_basedir = 8 //修改为: 9 open_basedir = .:/tmp/ //防止php木马跨站 10 //找到: 11 disable_functions = 12 //修改为: 13 disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid,posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname //列出PHP可以禁用的函数,如果某些程序需要用到这个函数,可以删除,取消禁用。 14 //找到: 15 ;date.timezone = 16 //修改为: 17 date.timezone = PRC 18 //找到: 19 expose_php = On 20 //修改为: 21 expose_php = Off //禁止显示php版本的信息 22 //找到: 23 display_errors = On 24 //修改为: 25 display_errors = Off //关闭错误提示
配置apache支持php5
1 vim /usr/local/apache2/conf/httpd.conf //编辑apache配置文件 2 在LoadModule php5_module modules/libphp5.so这一行下面添加 3 AddType application/x-httpd-php .php 4 service httpd restart //重启apache 5 service mysqld restart //重启mysql
配置apache支持php7
vim /usr/local/apache2/conf/httpd.conf //编辑apache配置文件 1.载入PHP模块,如httpd.conf中有下列代码则直接去掉前面#即可,没有则加入 LoadModule php7_module modules/libphp7.so 2.在底部加入以下代码使得Apache可以解析php文件 <IfModule mod_php7.c> AddType application/x-httpd-php .php </IfModule> 3.找到如下代码,在index.html后面加入index.php <IfModule dir_module> DirectoryIndex index.html </IfModule> 重启Apache service httpd restart
apache还可以结合php-fpm模块使用;如果启动php-fpm功能,先关闭第一种配置,再按如下配置即可;
修改主配置文件: LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so #取消注释 LoadModule proxy_module modules/mod_proxy.so #取消注释 ProxyRequests Off #在文本中添加 ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache2/htdocs/ #在文本中添加
测试
1 cd /usr/local/apache2/htdocs 2 rm -rf index.html 3 echo "<?php phpinfo(); ?>" > index.php 4 确保改目录为以下权限 5 chown daemon.daemon -R /usr/local/apache2/htdocs 6 chmod -R 700 /usr/local/apache2/htdocs 7 systemctl restart httpd //重启apache 8 浏览器访问:http://ip/index.php,查看是否显示php测试页面。