第十六周

1、源码编译安装LNMP架构环境;
  安装必要的包组和依赖的包
yum groupinstall “Development Tools” “Development Libraries” 
yum install wget openssl-devel ncurses-devel cmake pcre-devel libxml2-devel bzip2-devel libcurl-devel libmcrypt-devel 
   关闭防火墙和selinux

安装编译Nginx  (gcc openssl-devel pcre-devel zlib-devel)
创建nginx用户和用户组
[root@CentOS7 sources]# groupadd -r nginx
[root@CentOS7 sources]# useradd -r -g nginx nginx
编译
[root@CentOS7 sources]# tar xf nginx-1.6.1.tar.gz
[root@CentOS7 nginx-1.6.1]# ./configure  
 –prefix=/usr   
–sbin-path=/usr/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
[root@CentOS7 nginx-1.6.1]# make && make install
[root@CentOS7 nginx-1.6.1]# mkdir -p /var/tmp/nginx/client
  命令生效
[root@CentOS7 profile.d]# vim /etc/profile.d/nginx.sh
export PATH=/usr/local/nginx/sbin:$PATH
[root@CentOS7 profile.d]#  .  /etc/profile.d/nginx.sh
 启动nginx
[root@CentOS7 profile.d]# nginx –t  检查
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@CentOS7 profile.d]# nginx
编译安装mysql
创建mysql用户与用户组,创建数据库目录
[root@CentOS7 sources]# groupadd -r mysql
[root@CentOS7 sources]# useradd -r -g mysql mysql
[root@CentOS7 /]# mkdir -p /data/mysql
[root@CentOS7 /]# chown mysql:mysql /data/mysql
  
编译安装
[root@CentOS7 sources]# tar fx mariadb-5.5.46-linux-x86_64.tar.gz -C /usr/local
[root@CentOS7 sources]# cd /usr/local
[root@CentOS7 sources]# cd mariadb-5.5.46-linux-x86_64/
[root@CentOS7 local]# ln -sv mariadb-5.5.46-linux-x86_64 mysql”mysql” -> “mariadb-5.5.46-linux-x86_64”
[root@CentOS7 local]# cd mysql/
[root@CentOS7 mysql]# chown root.mysql ./
[root@CentOS7 mysql]# scripts/mysql_install_db –user=mysql –datadir=/data/mysql
  创建服务
[root@CentOS7 mysql]#  cp support-files/mysql.server /etc/init.d/mysqld
[root@CentOS7 mysql]# chkconfig –add mysqld  
[root@CentOS7 etc]# cp my.cnf{,.bak}
   mysql创建配置文件并修改
[root@CentOS7 mysql]# cp support-files/my-large.cnf  /etc/my.cnf

[root@CentOS7 /]# ln -s /tmp/mysql.sock /var/mysql/mysql.sock  确保php的socket路径一直

[root@CentOS7 mysql]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
innodb_file_per_table= ON
skip_name_resolve= ON
  启动服务
[root@CentOS7 mysql]# service mysqld start
Starting MySQL… SUCCESS! 
[root@CentOS7 mysql]# ss –tnl
State       Recv-Q Send-Q                     Local Address:Port                                    Peer Address:Port              
LISTEN      0      50                                     *:3306                                               *:

编译安装php

[root@CentOS7 sources]# tar xf php-5.6.31.tar.gz 
[root@CentOS7 sources]# cd php-5.6.31/
[root@CentOS7 php-5.6.31]# ./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
[root@CentOS7 php-5.6.31]# make && make install
    创建服务
[root@CentOS7 php-5.6.31]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@CentOS7 php-5.6.31]# chmod +x /etc/init.d/php-fpm
   为php-fpm提供配置文件
[root@CentOS7 sources]#cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@CentOS7html]# echo “fastcgi_param  SCRIPT_FILENAME    \$document_root\$fastcgi_script_name;” >> /etc/nginx/fastcgi_params 与nginx有关
   为php提供配置文件:
[root@CentOS7 php-5.6.31]# cp /sources/php-5.6.31/php.ini-development /usr/local/php/php.ini
   修改php-fpm配置
[root@CentOS7 php-5.6.31]# 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
开启服务
[root@CentOS7 sources]# /etc/init.d/php-fpm start
  
   编辑nginx配置文件,添加php
[root@CentOS7 mysql]# vim /etc/nginx/nginx.conf
location / {
            root   /usr/local/nginx/html;
            index  index.php index.html index.htm;
        }
location ~ .php$ {
            root           /usr/local/nginx/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }
[root@CentOS7 www]# nginx -s reload

建立测试文件,进行测试
[root@CentOS7 local]# vim /usr/local/nginx/html/index.php
<?php
    $conn = mysql_connect(‘127.0.0.1′,’root’,”);
    if ($conn)
        echo “OK”;
    else
        echo “Failure”;
?>

<?php
  phpinfo();
?>

2、编写一个脚本完成以下功能:
   (1)、一键搭建LNMP源码编译环境;
   (2)、可通过在脚本后面跟上一些参数来自定义安装目录等其他选项。”

#!/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

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

(0)
ning407631632ning407631632
上一篇 2017-08-15 17:59
下一篇 2017-08-15 19:06

相关推荐

  • Linux bash中命令执行状态返回值

    Linux bash中命令执行状态返回值 在操作系统中,命令的执行后输出的内容为命令执行结果输出,而这个命令本身是否执行成功,它是通过命令执行状态返回值来标识的。 常用的值: 0 表示命令执行成功非0 表示命令执行失败 bash中获取命令执行状态返回值的方法 在刚执行完一条指令后,使用echo $?取得上一条指令的命令执行状态返回值,示例如下:  …

    Linux干货 2016-11-06
  • 马哥教育网络班20期+第五周博客作业

    1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行; ]# grep  "^[[:space:]]\+" /boot/grub/grub.conf 2、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行; ]#…

    Linux干货 2016-07-12
  • Linux基础知识之cp mv rm

    该博文以CentOS6.8_x86_64系统为基础,Xshell 5远程连接CentOS系统,以root身份登录系统和sjsir普通用户身份登录系统。 为什么要学习cp、mv、rm命令?     cp、mv、rm命令为Linux系统使用最长使用的三个命令之一,复制、移动和删除是我们平时处理一些文件必须要学会的命令,应该用于熟练掌握和使用的…

    Linux干货 2016-07-29
  • linux程序包管理

         首先linux软件程序包和windows中的程序包是不兼容的,所以要在linux上使用windows上的程序,需要使用虚拟化工具。 库级别的虚拟化:     linux : WinE     windows: Cywin 而程序开发语言有多…

    Linux干货 2016-08-24
  • M22 使用光盘修复Centos实验初探

    实验目的: 服务器由于文件丢失等原因造成无法启动,可以使用光盘引导启动服务器,然后对服务器进行修复。 实验环境: VMware12安装Centos6.8虚拟机 Centos6.8的光盘镜像 实验原理: 手动删除虚拟机上的rpm程序文件,使用光盘镜像恢复安装rpm程序。 实验过程: 1、     执行命令删除rpm程序,…

    2017-03-06
  • 条件选择if语句

    单分支的if语句 if 判断条件; then 条件为真的分支代码 fi       单分支if结构的执行流程:首先判断条件测试操作的结果,如果返回值为0表示条件成立,则执行then后面的命令序列,一直到遇见fi为止表示结束,继续执行其他脚本代码;如果返回不为0,则忽略then后面的命令序列,直接跳至fi行以后执行其他脚…

    Linux干货 2016-08-16