一、环境和软件准备
1.系统软件
centos6.7
Apache/2.4.23
PHP5.5.36
2.yum源更新
yum install epel-release.noarch
3.安装开发工具
yum -y install gcc
yum -y install gcc-c++
4.安全设置
关闭selinux和开启防火墙的80端口
二、lamp安装配置
(一)apache安装配置
1.解决依赖关系
apr库可以使用yum安装,但版本比较老,建议编译安装新版本的apr库。
[root@localhost ~]# wget http://mirror.bit.edu.cn/apache/apr/apr-1.5.2.tar.gz
tar -zxvf apr-1.5.2.tar.gz
cd apr-1.5.2
./configure --prefix=/usr/local/apr
make && make install
[root@localhost ~]# wget http://mirror.bit.edu.cn/apache/apr/apr-util-1.5.4.tar.gz
tar -zxvf apr-util-1.5.4.tar.gz
cd apr-util-1.5.4
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make && make install
yum install pcre-devel #安装pcre依赖包
2.编译安装Apache
tar -zxvf httpd-2.4.23.tar.gz
[root@localhost ~]# cd httpd-2.4.23
./configure --prefix=/usr/local/apache \
--enable-mods-shared=all \
--enable-modules=most \
--sysconfdir=/etc/httpd \
--enable-deflate \
--enable-speling \
--enable-cache \
--enable-file-cache \
--enable-disk-cache \
--enable-mem-cache \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util \
--enable-mpms-shared=all \
--with-mpm=event
make && make install
3.编辑配置文件
vim /etc/httpd/httpd.conf
更改如下两项配置:
ServerAdmin localhost #主机名
ServerName www.test.com #域名
4.启动测试
启动: /usr/local/apache/bin/apachectl start
[root@localhost ~]# netstat -tnlp | grep 80
tcp 0 0 :::80 :::* LISTEN 36766/httpd
5.添加httpd到系统服务
[root@localhost ~]# 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
而后为此脚本赋予执行权限:
[root@localhost ~]# chmod +x /etc/init.d/httpd
[root@localhost apache]# service httpd status
httpd (pid 37252) 正在运行...
确保启动脚本中的pid路径和conf配置中的路径是一致的,否则会导致启动或停止服务异常。
如果conf文件中没有配置pid的路径。可以手动添加:PidFile /var/run/httpd.pid
6.配置环境变量:
vim /etc/profile
PATH=/usr/local/apache/bin/:$PATH export PATH
[root@localhost apache]# source /etc/profile
[root@localhost apache]# httpd -V #查看Apache版本
Server version: Apache/2.4.23 (Unix)
Server built: Nov 9 2016 16:33:13
Server's Module Magic Number: 20120211:61
Server loaded: APR 1.5.2, APR-UTIL 1.5.4
Compiled using: APR 1.5.2, APR-UTIL 1.5.4
Architecture: 64-bit
Server MPM: event
threaded: yes (fixed thread count)
forked: yes (variable process count)
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT="/usr/local/apache"
-D SUEXEC_BIN="/usr/local/apache/bin/suexec"
-D DEFAULT_PIDLOG="logs/httpd.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="/etc/httpd/mime.types"
-D SERVER_CONFIG_FILE="/etc/httpd/httpd.conf"
(二)安装mysql
1.软件准备
注意:官方现在提供mysql-5.7.16.tar.gz和mysql-boost-5.7.16.tar.gz两个包,建议下载后者,因为
改版本包含了mysql-5.7需要的boost库文件,不用再单独下载boots库。
[root@localhost ~]# wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.16.tar.gz
2.安装MySQL
1>创建数据库用户
groupadd mysql
useradd -g mysql -s /sbin/nologin mysql #非登录用户
2>建立mysql安装目录及数据存放目录
mkdir /usr/local/mysql #在本机安装可不用创建该目录,make install时会自动创建
mkdir -p /data/mysql
chown mysql:mysql -R /data/mysql/
3>解决依赖关系
yum install cmake
yum install ncurses-devel
yum install bison
yum install git openssl-devel
yum -y install gcc gcc-c++ ncurses ncurses-devel cmake bison git openssl-devel
4>编译安装
tar -zxvf mysql-boost-5.7.16.tar.gz
[root@localhost ~]# cd mysql-5.7.16
# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data/mysql \
-DMYSQL_TCP_PORT=3306 \
-DSYSCONFDIR=/etc \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DWITH_LIBWRAP=0 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_BOOST=./boost/boost_1_59_0 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DENABLED_LOCAL_INFILE=1 \
-DENABLE_DTRACE=0 \
-DWITH_EMBEDDED_SERVER=1
make
make install
cp support-files/my-medium.cnf /etc/my.cnf
/etc/my.cnf,仅供参考
[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8
[mysqld]
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /data/mysql
pid-file = /data/mysql/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1
init-connect = 'SET NAMES utf8'
character-set-server = utf8
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 = 0
query_cache_size = 0
#query_cache_limit = 2M
ft_min_word_len = 4
log_bin = mysql-bin
#binlog_format = mixed
expire_logs_days = 30
log_error = /data/mysql/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/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]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
5>数据库初始化
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
注:
之前版本mysql_install_db是在mysql_basedir/script下,5.7放在了mysql_install_db/bin目录下,且已被废弃
“–initialize”会生成一个随机密码(~/.mysql_secret),而”–initialize-insecure”不会生成密码
–datadir目标目录下不能有数据文件
启动数据库
service mysqld start
6>添加系统启动脚本
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql
chkconfig --add mysql
chkconfig mysql on
修改mysql否则启动会报错:
Starting MySQL..... ERROR! The server quit without updating PID file.
错误日志:/mysql-bin.index' not found (Errcode: 13
原因分析:mysql的数据路径为默认路径,不是编译时指定的路径。
解决方法:vim /etc/init.d/mysql
更改:datadir=/data/mysql/data
或者 vim /etc/my.cnf
在[mysqld]部分添加:datadir=/data/mysql/data
service mysql start
7>添加环境
echo 'PATH=/usr/local/mysql/bin:$PATH export PATH' >>/etc/profile
source /etc/profile
8>删除匿名用户和设置用户密码
mysql -u root -p
mysql> select User,Host,Password from mysql.user;
mysql> delete from mysql.user where user='' && password='';
mysql> set password for 'root'@'localhost'=password('123456');
mysql> flush privileges;
mysql> select User,Host,authentication_string from mysql.user;
+-----------+-----------+-------------------------------------------+
| User | Host | authentication_string |
+-----------+-----------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+-----------+-----------+-------------------------------------------+
mysql5.7增加了sys 系统数据库,通过这个库可以快速的了解系统的元数据信息
这个库确实可以方便DBA发现数据库的很多信息,解决性能瓶颈都提供了巨大帮助
(三)php安装
1.解决依赖关系
yum install gd-devel.x86_64 libxml2-devel bzip2-devel.x86_64 libcurl-devel libmcrypt-devel
2.编译安装
tar -zxvf php-5.5.36.tar.gz
cd php-5.5.36
编译参数
注意:php中数据库连接方式有pdo和mysqli两种,pdo支持多种数据库连接,而mysqli仅支持mysql。
下面给出两种数据库扩展的编译参数:
pdo-mysql扩展方式:
./configure --prefix=/usr/local/php --with-pdo-mysql --with-openssl --with-mysql-sock=/tmp/mysql.sock --enable-mbstring --enable-opcache --with-freetype-dir --with-jpeg-dir --with-png-dir --with-gd --with-zlib --with-libxml-dir --enable-xml --with-mhash --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 --enable-sysvshm --enable-ftp --with-curl
mysqli扩展方式:
./configure --prefix=/usr/local/php --with-mysql --with-mysqli --with-openssl --enable-mbstring --enable-opcache --with-freetype-dir --with-jpeg-dir --with-png-dir --with-gd --with-zlib --with-libxml-dir --enable-xml --with-mhash --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 --enable-sysvshm --enable-ftp --with-curl
[root@localhost php-5.5.36]# make && make install
Build complete.
Don't forget to run 'make test'.
Installing PHP SAPI module: apache2handler
/usr/local/apache/build/instdso.sh SH_LIBTOOL='/usr/local/apr/build-1/libtool' libphp5.la /usr/local/apache/modules
/usr/local/apr/build-1/libtool --mode=install install libphp5.la /usr/local/apache/modules/
libtool: install: install .libs/libphp5.so /usr/local/apache/modules/libphp5.so
libtool: install: install .libs/libphp5.lai /usr/local/apache/modules/libphp5.la
libtool: install: warning: remember to run `libtool --finish /root/php-5.5.36/libs'
chmod 755 /usr/local/apache/modules/libphp5.so
[activating module `php5' in /etc/httpd/httpd.conf]
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20121212/
Installing PHP CLI binary: /usr/local/php/bin/
Installing PHP CLI man page: /usr/local/php/php/man/man1/
Installing PHP CGI binary: /usr/local/php/bin/
Installing PHP CGI man page: /usr/local/php/php/man/man1/
Installing build environment: /usr/local/php/lib/php/build/
Installing header files: /usr/local/php/include/php/
Installing helper programs: /usr/local/php/bin/
program: phpize
program: php-config
Installing man pages: /usr/local/php/php/man/man1/
page: phpize.1
page: php-config.1
Installing PEAR environment: /usr/local/php/lib/php/
[PEAR] Archive_Tar - installed: 1.4.0
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util - installed: 1.3.0
[PEAR] PEAR - installed: 1.10.1
Wrote PEAR system config file at: /usr/local/php/etc/pear.conf
You may want to add: /usr/local/php/lib/php to your php.ini include_path
/root/php-5.5.36/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin
ln -s -f phar.phar /usr/local/php/bin/phar
Installing PDO headers: /usr/local/php/include/php/ext/pdo/
3.生产php配置文件
[root@localhost php-5.5.36]cp php.ini-production /etc/php.ini
4.整合Apache和php
# 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时配置生效
5.测试
测试PHP环境是否可以正常运行,在/usr/local/apache/htdocs目录下建一个test.php或test.phtml的文件,内容如下示:
<?php
phpinfo();
?>
最后在浏览器测试php页面
(责任编辑:liangzh) |