成功最有效的方法就是向有经验的人学习,学习最重要的是心态和毅力
目录:
1.1 搭建LAMP+Wordpress … 1
1.2 Nginx负载均衡(反选代理)web… 2
1.1 搭建LAMP+Wordpress
(一)安装apache
1、安装apache依赖包
yum install gcc gcc-c++ gcc-g77 flex bison autoconf automake bzip2-devel zlib-devel ncurses-devel libjpeg-devel libpng-devel libtiff-devel freetype-devel pam-devel openssl-devel libxml2-devel
2、下载并安装apache2.2.31
wget http://mirror.bit.edu.cn/apache/httpd/httpd-2.2.31.tar.gz
tar xf httpd-2.2.31.tar.gz
cd httpd-2.2.31
./configure \
--prefix=/application/apache2.2.31 \
--enable-deflate \
--enable-expires \
--enable-headers \
--enable-modules=most \
--enable-so \
--with-mpm=worker \
--enable-rewrite
make
make install
ln -s /application/apache2.2.31/ /application/apache
ls -l /application/
总用量 4
lrwxrwxrwx 1 root root 26 6月 27 22:37 apache -> /application/apache2.2.31/
drwxr-xr-x 15 root root 4096 6月 27 22:35 apache2.2.31
3、编写apache启动脚本
#!/bin/bash
# chkconfig: 2345 85 15
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/httpd ]; then
. /etc/sysconfig/httpd
fi
INITLOG_ARGS=""
apachectl=/application/apache/bin/apachectl (修改)
httpd=/application/apache/bin/httpd (修改)
pid=$httpd/logs/httpd.pid
prog=httpd
RETVAL=0
start() {
echo -n $"Starting $prog: "
daemon $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/httpd
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/httpd $pid
}
reload() {
echo -n $"Reloading $prog: "
killproc $httpd -HUP
RETVAL=$?
echo
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart)
if [ -f $pid ] ; then
stop
start
fi
;;
reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|reload|status"
echo $"|fullstatus|graceful|help|configtest}"
exit 1
esac
exit $RETVAL
4、设置 apache开机启动
chkconfig --add httpd
chkconfig --list httpd
chmod +x /etc/init.d/httpd
/etc/init.d/httpd restart
5、配置基于域名的虚拟主机
cd /application/apache/conf/
vim httpd.conf 修改98行
ServerName 127.0.0.1:80
修改132
<Directory "/application/apache2.2.31/html">
修改378
Include conf/extra/httpd-mpm.conf
修改396
Include conf/extra/httpd-vhosts.conf
==============================
[root@web02 conf]# cd extra/
[root@web02 extra]# vim httpd-vhosts.conf
<VirtualHost *:80>
ServerAdmin oldboy@oldboyedu.com
DocumentRoot "/application/apache2.2.31/html/www"
ServerName www.etiantian.org
ServerAlias etiantian.org
ErrorLog "/app/logs/www-error_log"
CustomLog "/app/logs/www-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin oldboy@oldboyedu.com
DocumentRoot "/application/apache2.2.31/html/bbs"
ServerName bbs.etiantian.org
ErrorLog "/app/logs/bbs-error_log"
CustomLog "/app/logs/bbs-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin oldboy@oldboyedu.com
DocumentRoot "/application/apache2.2.31/html/blog"
ServerName blog.etiantian.org
ErrorLog "/app/logs/blog-error_log"
CustomLog "/app/logs/blog-access_log" common
</VirtualHost>
6、创建站点目录及测试文件
mkdir -p /application/apache2.2.31/html/{www,bbs,blog}
echo www.etiantian.org >/application/apache2.2.31/html/www/index.html
echo bbs.etiantian.org >/application/apache2.2.31/html/bbs/index.html
echo blog.etiantian.org >/application/apache2.2.31/html/blog/index.html
mkdir /app/logs -p
/application/apache/bin/apachectl -t
/application/apache/bin/apachectl graceful
7、配置客户端host解析到如下域名商,检查
curl www.etiantian.org
curl bbs.etiantian.org
curl blog.etiantian.org
到此apache基于域名的虚拟主机配置成功。
(二) 安装mysql(这里用二进制包)
1、安装mysql依赖包
yum -y install libaio*
2、下载mysql二进制安装包
wget http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.49-linux2.6-x86_64.tar.gz
3、创建mysql用户
useradd mysql -s /sbin/nologin -M
4、解压并安装mysql
tar xf mysql-5.5.49-linux2.6-x86_64.tar.gz
cp -a mysql-5.5.49-linux2.6-x86_64 /application/mysql-5.5.49
ln -s /application/mysql-5.5.49/ /application/mysql
/application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql (初始化mysql)
chown -R mysql.mysql /application/mysql/
cp /application/mysql/support-files/my-small.cnf /etc/my.cnf
cp /application/mysql-5.5.49/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
sed -i 's#/usr/local/mysql#/application/mysql#g' /application/mysql/bin/mysqld_safe /etc/init.d/mysqld (修改mysql启动脚本)
/etc/init.d/mysqld start
5、配置环境变量
echo "PATH="/application/mysql/bin:$PATH"" >> /etc/profile
source /etc/profile
6、创建mysql密码
mysqladmin -uroot password '123456'
mysqladmin -uroot -p'旧密码' password '新密码' (修改密码)
7、重启mysql
/etc/init.d/mysqld restart
到此mysql安装完成
(三) 安装PHP
1、安装php依赖包
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum install zlib-devel libxml2-devel libjpeg-devel freetype-devel libpng-devel gd-devel curl-devel libxslt-devel libmcrypt-devel mhash mhash-devel mcrypt openssl-devel -y
mkdir -p /home/oldboy/tools
cd /home/oldboy/tools
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar zxf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local/libiconv
make
make install
cd ..
2、安装php
wget http://mirrors.sohu.com/php/php-5.5.26.tar.gz
cd /home/oldboy/tools
tar xf php-5.5.26.tar.gz
cd php-5.5.26
./configure \(反斜杠后面不能有空格)
--prefix=/application/php5.5.26 \
--with-apxs2=/application/apache/bin/apxs \
--with-mysql=mysqlnd \(如果没有安装mysql则使用mysqlnd)
--with-iconv-dir=/usr/local/libiconv \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-safe-mode \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--with-curlwrappers \
--enable-mbregex \
--enable-mbstring \
--with-mcrypt \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--enable-short-tags \
--enable-zend-multibyte \
--enable-static \
--with-xsl \
--enable-ftp
make
make install
ln -s /application/php5.5.26/ /application/php
3、检查结果
[root@web02 php-5.5.26]# ll /application/apache/modules/
总用量 30568
-rw-r--r-- 1 root root 9194 11月 10 20:37 httpd.exp
-rwxr-xr-x 1 root root 31285631 11月 10 23:51 libphp5.so
[root@web02 php-5.5.26]# grep libphp5.so /application/apache/conf/httpd.conf
LoadModule php5_module modules/libphp5.so
[root@web02 php-5.5.26]# cp php.ini-production /application/php/lib/php.ini
4、配置apache整合php(配置httpd.conf)
311行下增加:
AddType application/x-httpd-php .php .phtml
AddType application/x-httpd-php-source .phps
168行下增加:
DirectoryIndex index.php index.html
67行下增加
User www
Group www
5、建立用户
useradd www -s /sbin/nologin
id www
/application/apache/bin/apachectl -t
Syntax OK
/application/apache/bin/apachectl graceful
6、检查php
<?php
phpinfo();
?>
7、检查php
<?php
$link_id=mysql_connect('localhost','root','123456') or mysql_error();
if($link_id){
echo "mysql successful by oldboy training!";
}
else{
echo mysql_error();
}
?>
到此LAMP搭建完成
(四)搭建wordpress
1、下载wordpress
wget https://cn.wordpress.org/wordpress-4.5.3-zh_CN.tar.gz
tar xf wordpress-4.5.3-zh_CN.tar.gz
mv wordpress/* /application/apache/html/blog/
chown -R www.www /application/apache/html/blog/
2、创建wordpress数据库
mysql> create database wordpress;
mysql> grant all on wordpress.* to wordpress@'172.16.1.%' identified by 'wordpress123'
mysql> flush privileges;
3、 修改hosts记录
vi /etc/hosts (修改hosts记录将mysql指向本机)
172.16.1.31 MysqlServer.com
4、访问并配置博客 http://blog.etiantian.org/index.php
5、配置wordpress伪静态
(1)在WordPress后台-设置-固定链接-自定义结构,输入下面的代码,最后保存更改即可
/archives/%post_id%.html
2)在虚拟主机增加权限控制(只需要允许blog站点被访问就可以了,不需要写rewrite)
[root@Oldboy extra]# tail -15 httpd-vhosts.conf
<VirtualHost *:80>
ServerAdmin oldboy@oldboyedu.com
DocumentRoot "/application/apache2.2.31/html/blog"
ServerName blog.etiantian.org
ErrorLog "/app/logs/blog-error_log"
CustomLog "/app/logs/blog-access_log" common
# 增加以下内容
<Directory "/application/apache2.2.31/html/blog">
Options Includes ExecCGI FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
3) 查看效果
1.2 Nginx 负载均衡(反向代理)web*
1、nginx负载均衡原理
2、配置nginx负载均衡
修改nginx.conf
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream backend { (backend:集群模块)
server 172.16.1.7:80 weight=1; (地址池)
server 172.16.1.8:80 weight=1; (地址池)
}
server {
listen 80;
server_name blog.etiantian.org;
location / {
proxy_pass http://backend; (调用集群模块)
proxy_set_header Host $host; (带主机头请求后端集群)
}
}
server {
listen 80;
server_name bbs.etiantian.org;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
}
}
server {
listen 80;
server_name www.etiantian.org;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
}
}
}
# 重启nginx并测试 略
到此部署全部完成