N25-第16周博客作业

1、源码编译安装LNMP架构环境;

    安装nginx:

     1)安装依赖包

]# yum groupinstall "Development Tools" "Development Libraries" -y
]# yum install wget openssl-devel ncurses-devel cmake pcre-devel libxml2-devel bzip2-devel libcurl-devel libmcrypt-devel -y

     2)关闭防火墙和selinux

]# iptables -F
]# systemctl stop firewalld.service
]# systemctl disable firewalld.service
]# setenforce 0
]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config

     3)创建nginx用户和组

]# groupadd -r nginx
]# useradd -r -g nginx nginx

     4)编译安装nginx

]# tar xf nginx-1.6.1.tar.gz
]# cd nginx-1.6.1
]# ./configure \
  --prefix=/usr/local/nginx \
  --sbin-path=/usr/local/nginx/sbin/nginx \
  --conf-path=/etc/nginx/nginx.conf \
  --error-log-path=/var/log/nginx/error.log \
  --http-log-path=/var/log/nginx/access.log \
  --pid-path=/var/run/nginx/nginx.pid  \
  --lock-path=/var/lock/nginx.lock \
  --user=nginx \
  --group=nginx \
  --with-http_ssl_module \
  --with-http_flv_module \
  --with-http_stub_status_module \
  --with-http_gzip_static_module \
  --http-client-body-temp-path=/var/tmp/nginx/client/ \
  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
  --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
  --http-scgi-temp-path=/var/tmp/nginx/scgi \
  --with-pcre
]# make && make install

     5)启动nginx

]# mkdir /var/tmp/nginx/client -p
]# echo "export PATH=/usr/local/nginx/sbin:$PATH" >/etc/profile.d/nginx.sh
]# source /etc/profile
]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
]# nginx

     安装mysql:

    6)创建mysql用户和组

]# groupadd -r mysql
]# useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql

     7)二进制方式安装mysql

]# tar xf mysql-5.5.32.tar.gz
]# cd mysql-5.5.32
]# cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/mydata/data \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DMYSQL_USER=mysql \
-DWITH_DEBUG=0 \
-DWITH_SSL=system

     8)初始化数据库

]# mkdir /mydata/data
]# chown -R mysql.mysql /mydata/data
]# cd /usr/local/mysql
]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data/

     9)创建mysql参数文件

]# cp support-files/my-large.cnf /etc/my.cnf
]# vim /etc/my.cnf
[mysqld]
...
datadir= /mydata/data
innodb_file_per_table= ON
skip_name_resolve= ON
...

     10)创建启动脚本

]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld

     11)输出mysql的头文件至系统头文件路径/usr/include

]# ln -s /usr/local/mysql/include/ /usr/include/mysql

     12)输出mysql的库文件给系统库查找路径

]# echo "/usr/local/mysql/lib/" >/etc/ld.so.conf.d/mysql.conf
]# ldconfig 

    13)启动mysql

]# echo "export PATH=/usr/local/mysql/bin:$PATH" >/etc/profile.d/mysql.sh
]# source /etc/profile
]# /etc/init.d/mysqld start

     安装php:

    14)打补丁

]# tar xf php-5.4.4.tar.bz2
]# cd php-5.4.4
]# curl -o php-5.x.x.patch https://mail.gnome.org/archives/xml/2012-August/txtbgxGXAvz4N.txt
]# patch -p0 -b < ./php-5.x.x.patch

    15)编译安装php

./configure --prefix=/usr/local/php \
--with-mysql=/usr/local/mysql \
--with-openssl \
--enable-fpm \
--enable-sockets \
--enable-sysvshm \ 
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--enable-mbstring \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-libxml-dir=/usr \
--enable-xml \
--with-mhash \
--with-mcrypt \
--with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d \
--with-bz2 \
--with-curl 
]# make && make install

     16)创建php-fpm启动脚本

]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm 
]# chmod +x /etc/rc.d/init.d/php-fpm 
]# chkconfig --add php-fpm
]# chkconfig php-fpm on
]# /etc/init.d/php-fpm start

     17)创建并修改php配置文件

]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
]# vim /usr/local/php/etc/php-fpm.conf
pm.max_children = 150
pm.start_servers = 8
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pid = /usr/local/php/var/run/php-fpm.pid

]# echo "fastcgi_param  SCRIPT_FILENAME    \$document_root\$fastcgi_script_name;" >> /etc/nginx/fastcgi_params

]# vim /etc/nginx/nginx.conf
location / {
            root   html;
            index  index.php index.html index.htm;
        } location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }
]# nginx -s reload

     测试:

    18)测试nginx与php的联动

]# cat > /usr/local/nginx/html/index.php << EOF
<?php
phpinfo();
?>
EOF

N25-第16周博客作业

    19)测试php与mysql的联动

]# cat >/usr/local/nginx/html/index.php <<EOF
<?php
    \$conn = mysql_connect('127.0.0.1','root','');
    if (\$conn)
        echo "OK";
    else
        echo "Failure";
?>
EOF

N25-第16周博客作业

2、编写一个脚本完成以下功能:
   (1)、一键搭建LNMP源码编译环境;

   (2)、可通过在脚本后面跟上一些参数来自定义安装目录等其他选项。

脚本执行前提:

1)操作系统版本为centos7

2)部署环境能够访问外网

3)源码安装包应提前准备好并与该脚本放在同一目录下

#!/bin/bash
lnmp() {
echo "安装前环境准备..."
sleep 3
yum remove nginx mysql mariadb php -y
rpm -ivh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum groupinstall "Development Tools" "Development Libraries" -y
yum install openssl-devel \
ncurses-devel \
cmake \
pcre-devel \
libxml2-devel \
bzip2-devel \
libcurl-devel \
libmcrypt-devel -y

iptables -F
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
sed -i '/^SELINUX\>/d' /etc/selinux/config
echo "SELINUX=disabled" >>/etc/selinux/config

echo "开始安装nginx..."
sleep 3

#编译安装nginx
id nginx &>/dev/null && userdel -r nginx
groupdel nginx
groupadd -r nginx
useradd -r -g nginx nginx
tar xf $PWD/nginx-1.6.1.tar.gz
cd nginx-1.6.1
./configure \
--prefix=$dir/nginx \
--sbin-path=$dir/nginx/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid  \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre
make && make install

mkdir -p /var/tmp/nginx/client

#添加对php的支持
sed -i '65,71s/^[[:space:]]\+#//g' /etc/nginx/nginx.conf
sed -i '45s/index.html/index.php index.html/g' /etc/nginx/nginx.conf
echo "fastcgi_param  SCRIPT_FILENAME    \$document_root\$fastcgi_script_name;" >> /etc/nginx/fastcgi_params

#添加环境变量
echo "export PATH=$dir/nginx/sbin:$PATH" >/etc/profile.d/nginx.sh
source /etc/profile

nginx

#Nginx测试
if curl 127.0.0.1 &>/dev/null;then
    echo "Nginx安装成功!"
else
    echo "Nginx安装失败!"
fi

echo "开始安装MySQL..."
sleep 3
#编译安装MySQL
id mysql &>/dev/null && userdel -r mysql
groupadd -r mysql
useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql
tar xf $PWD/mysql-5.5.32.tar.gz
cd mysql-5.5.32
cmake \
-DCMAKE_INSTALL_PREFIX=$dir/mysql \
-DMYSQL_DATADIR=/mydata/data \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DMYSQL_USER=mysql \
-DWITH_DEBUG=0 \
-DWITH_SSL=system
make && make install

#初始化数据库
cd $dir/mysql
chown -R root.mysql ./*
[ ! -d /mydata/data ] && mkdir -p /mydata/data
scripts/mysql_install_db --user=mysql --datadir=/mydata/data/

#修改MySQL参数文件
/usr/bin/cp support-files/my-large.cnf /etc/my.cnf
sed -i '/\[mysqld\]/a datadir= /mydata/data\ninnodb_file_per_table= ON\nskip_name_resolve= ON' /etc/my.cnf

#生成MySQL启动文件
/usr/bin/cp support-files/mysql.server /etc/rc.d/init.d/mysqld

ln -s $dir/mysql/include/ /usr/include/mysql
echo "$dir/mysql/lib/" >/etc/ld.so.conf.d/mysql.conf
ldconfig

#添加MySQL环境变量
echo "export PATH=$dir/mysql/bin:$PATH" >/etc/profile.d/mysql.sh
source /etc/profile

#启动MySQL
/etc/init.d/mysqld start

echo "开始安装php..."
sleep 3
tar xf $PWD/php-5.4.4.tar.bz2
cd php-5.4.4
#打补丁,解决编译安装过程中的报错
curl -o php-5.x.x.patch https://mail.gnome.org/archives/xml/2012-August/txtbgxGXAvz4N.txt
patch -p0 -b < ./php-5.x.x.patch
./configure --prefix=$dir/php \
--with-mysql=$dir/mysql \
--with-openssl \
--enable-fpm \
--enable-sockets \
--enable-sysvshm \
--with-mysqli=$dir/mysql/bin/mysql_config \
--enable-mbstring \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-libxml-dir=/usr/include/libxml2/libxml \
--enable-xml \
--with-mhash \
--with-mcrypt \
--with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d \
--with-bz2 \
--with-curl 
make && make install

#生成php-fpm启动文件
/usr/bin/cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm 
chmod +x /etc/rc.d/init.d/php-fpm

#修改php参数文件
/usr/bin/cp $dir/php/etc/php-fpm.conf.default $dir/php/etc/php-fpm.conf
sed -i -e '/pm.max_children/d' -e \
'/\<pm.start_servers\>/d' -e \
'/\<pm.min_spare_servers\>/d' -e \
'/\<pm.max_spare_servers\>/d' -e \
'/pid = run\/php-fpm.pid/s/^;//g' $dir/php/etc/php-fpm.conf
cat >>$dir/php/etc/php-fpm.conf <<EOF
pm.max_children = 150
pm.start_servers = 8
pm.min_spare_servers = 5
pm.max_spare_servers = 10
EOF

/etc/init.d/php-fpm start
echo "php安装完毕!"
}

#主程序
PWD=$(pwd)
if [ ! -f $PWD/mysql-5.5.32.tar.gz ] || [ ! -f $PWD/nginx-1.6.1.tar.gz ] || [ ! -f $PWD/php-5.4.4.tar.bz2 ];then
    echo "请将安装文件与脚本放在同一目录下!"
    exit 1
fi

if [ $# -eq 0 ];then
    dir=/usr/local
elif [ $# -eq 1 ];then
    dir=$1
    if [ ! -d $dir ];then 
    mkdir -p $dir
    fi
else
    echo "参数无效,请重新执行!"
    exit 1
fi

lnmp

原创文章,作者:oranix,如若转载,请注明出处:http://www.178linux.com/76626

(0)
oranixoranix
上一篇 2017-05-21 20:53
下一篇 2017-05-21

相关推荐

  • 马哥教育网络班22期第五周课程练习1

    1、cat /etc/passwd  | egrep "^root|^fedora|^user1"| cut -d":" -f1,7  2、egrep -o  "[^[:space:]]+\(\)" /etc/rc.d/init.d/functions   3…

    Linux干货 2016-09-15
  • LAMP分离环境的搭建(最新版本的mysql+php+http源码编译安装)

    实验者:FrankStar      实验日期:20150628 任务:主要是实现在Linux环境下配置LAMP环境,利用VMware+centos6.4实现,将LAMP分离为多台主机; 附注:由于大家的环境不一样,可能有些人已经安装了这个包或者那个软件,所以调试的环境结果也不一样,为了尽量能模拟到 可能出现的所有问题,在编译会…

    Linux干货 2015-06-30
  • N25_第二周作业

      1、Linux上的文件管理命令都有哪些,其常用的使用方法及其相关示例演示。  cp命令:                                  &nbsp…

    Linux干货 2016-12-13
  • RAID实现原理及常用级别

    RAID(Redundant Array of Independent Disks): 定义:独立硬盘冗余阵列,旧称廉价磁盘冗余阵列(Redundant Array of Independent Disks),简称磁盘阵列。 原理:把多个相对便宜的硬盘组合起来,成为一个硬盘阵列组,使其性能达到甚至超过价格昂贵,容量巨大的硬盘。 优势: RAID在容量和管理上…

    Linux干货 2016-07-22
  • 马哥教育网络班21期+第一周课程练习

    一、计算机的组成及其功能 自上个世纪40年代开始截止到目前,我们所有的计算机包括手持的智能终端设备,它们整个组织体系设备都是遵循冯诺依曼体系结构。 现代计算机设备的组成部分: 运算器、控制器、存储器、输入设备、输出设备 控制器:控制器是整个计算机的枢纽,一般是控制计算机整个部件之间协调的,比如运算器要想运算的话,首先得从存储器中取出数值。或者输入设备输入数。…

    Linux干货 2016-07-07
  • 用户 组

    用户 组    在linux系统中,用户管理是基于用户名和密码的方式进行资源的分配,linux上用户Username/UID分为以下类别:    管理员: root,0    普通用户: 1-65535 普通用户分为:系统用户和登录用户两种 系统用户:1-499(cen…

    系统运维 2016-08-04