第十二章LNMP架构上预习笔记

12.1 LNMP架构介绍

12.2 MySQL安装

下载二进制免编译包,并解压

cd /usr/local/src 

wget http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz

tar xvf mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz

安装

./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

更改配置文件

拷贝启动脚本

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

更改启动脚本文件,定义 basedir datadir

vim /etc/init.d/mysqld

添加到服务列表里面,并设置为开机启动

实验中的报错

之前安装过mariadb,把服务killall  

12.3 PHP安装(上)

 useradd -s /sbin/nologin php-fpm

wget http://cn2.php.net/distributions/php-5.6.32.tar.bz2

tar xvf php-5.6.32.tar.bz2

yum -y install libxml2-devel.x86_64,openssl-devel,curl-devel,libjpeg-devel,libpng-devel,freetype-devel,libmcrypt-devel,

./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --with-pear --with-curl  --with-openssl

make && make install
cp php.ini-production /usr/local/php-fpm/etc/php.ini

vim /usr/local/php-fpm/etc/php-fpm.conf

[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024

cd /usr/local/src/php-5.6.32

cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

chmod 755 /etc/init.d/php-fpm

安装时报的错

12.4 PHP安装(下)

12.5 Nginx介绍

12.6 Nginx安装

cd /usr/local/src

wget http://nginx.org/download/nginx-1.14.0.tar.gz

tar xvf nginx-1.14.0.tar.gz

make && make install

检查配置文件是否有误

编辑启动脚本

vim /etc/init.d/nginx

#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"

start() 
{
    echo -n $"Starting $prog: "
    mkdir -p /dev/shm/nginx_temp
    daemon $NGINX_SBIN -c $NGINX_CONF
    RETVAL=$?
    echo
    return $RETVAL
}

stop() 
{
    echo -n $"Stopping $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -TERM
    rm -rf /dev/shm/nginx_temp
    RETVAL=$?
    echo
    return $RETVAL
}

reload()
{
    echo -n $"Reloading $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -HUP
    RETVAL=$?
    echo
    return $RETVAL
}

restart()
{
    stop
    start
}

configtest()
{
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac

exit $RETVAL
 

添加服务和开机启动

启动nginx 服务

测试php解析

vim /usr/local/nginx/html/1.php

LNMP架构介绍

和LAMP不同的是,提供web服务的是Nginx.

并且php是作为一个独立服务存在的,这个服务叫做php-fpm

Nginx直接处理静态请求,动态请求会转发给php-fpm

MySQL安装

下载、解压MySQL二进制免编译包

cd /usr/local/src

wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz 

tar zxf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz

安装MySQL二进制免编译包

#将解压出来的目录移动到/usr/local/目录下重命名为mysql
mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql

#创建mysql用户
useradd mysql -s /usr/bin/nologin -M

#创建存放数据的目录
mkdir /data/mysql -p

#将放数据存放目录和mysql程序目录的用户和组修改为mysql
chown -R mysql.mysql /data/mysql  /usr/local/mysql

#初始化数据库
cd /usr/local/mysql
yum -y install autoconf  #需要一个依赖包
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

修改mysql配置文件

#将下面的命令直到最后一个EOF全部复制,粘贴到命令行执行,就会将配置内容写入/etc/my.cnf文件
echo >/etc/my.cnf<<EOF
[mysqld]
port = 3306
basedir=/usr/local/mysql
datadir=/data/mysql/
socket=/tmp/mysql.sock
user=mysql
default-time-zone=system
default-storage-engine=InnoDB
log-error=/var/log/mysqld.log
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
EOF
#复制mysql启动脚本到/etc/init.d/目录下更名为mysqld
cp support-files/mysql.server /etc/init.d/mysqld

#修改/etc/init.d/mysqld文件中的basedir
sed -i s'#^basedir=$#basedir=/usr/local/mysql#' /etc/init.d/mysqld

#修改/etc/init.d/mysqld文件中的datadir
sed -i s'#^datadir=$#datadir=/data/mysql#' /etc/init.d/mysqld

#启动mysql
/etc/init.d/mysqld start
#将mysqld加入系统管理
chkconfig --add mysqld
#设置开机自启动
chkconfig --level 35 mysqld on

PHP安装

和LAMP安装PHP方法有差别,需要开启php-fpm服务

下载、解压源码包

cd /usr/local/src/

wget http://mirrors.sohu.com/php/php-5.6.30.tar.gz

tar zxf php-5.6.30.tar.gz

编译安装PHP

#先创建php-fpm用户
useradd -Ms /sbin/nologin php-fpm

#安装依赖包
yum -y install gcc libcurl-devel libxml2-devel openssl-devel bzip2-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel

mkdir -p /usr/local/php-fpm/etc
#configure设置安装参数
cd php-5.6.30
./configure --prefix=/usr/local/php-fpm \
--with-config-file-path=/usr/local/php-fpm/etc \
--enable-fpm \
--with-fpm-user=php-fpm \
--with-fpm-group=php-fpm \
--with-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-pdo-mysql=/usr/local/mysql \
--with-mysql-sock=/tmp/mysql.sock \
--enable-mysqlnd \
--with-libxml-dir \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-iconv-dir \
--with-zlib-dir \
--with-mcrypt \
--enable-soap \
--enable-gd-native-ttf \
--enable-ftp \
--enable-mbstring \
--enable-exif \
--with-pear \
--with-curl \
--with-openssl

##编译、安装
make -j4 && make install

##检查是否出错
echo $? 

参数说明:

--prefix=/usr/local/php-fpm #设定安装目录        
--with-config-file-path=/usr/local/php-fpm/etc #谁定php-fpm配置文件所在目录
--enable-fpm     #开启php-fpm模式  
--with-fpm-user=php-fpm #设定php-fpm服务的用户
--with-fpm-group=php-fpm #设定php-fpm服务的用户组
--with-mysql=/usr/local/mysql #设定mysql库,这种方式基本已经废弃
--with-mysqli=/usr/local/mysql/bin/mysql_config #最新的mysqli连接库
--with-pdo-mysql=/usr/local/mysql #mysqli连接库
--with-mysql-sock=/tmp/mysql.sock #指定mysql.sock文件路径
#其他参数都和LAMP相同

安装完成后的基本配置

#复制初始配置文件到configure指定的配置文件存放目录下改名为php.ini
cp php.ini-production /usr/local/php-fpm/etc/php.ini

#配置/usr/local/php-fpm/etc/php-fpm.conf配置文件

cat >/usr/local/php-fpm/etc/php-fpm.conf<<EOF
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
EOF

其他配置

#复制启动脚本到/etc/init.d/目录下重命名为php-fpm
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

#修改/etc/init.d/php-fpm文件的权限
chmod 755 /etc/init.d/php-fpm

#加入chkconfig管理
chkconfig --add php-fpm

#设置开机启动
chkconfig php-fpm on

#启动php-fpm服务
service php-fpm start

#查看php-fpm服务进程是否正常开启。
ps aux |grep php-fpm

Nginx

Nginx介绍

Nginx官网:nginx.org

Nginx应用场景:web服务、反向代理、负载均衡

Nginx著名分支,淘宝基于Nginx开发的Tengine,使用上和Nginx一致,服务名,配置文件名都一样, 
和Nginx的最大区别在于Tenging增加了一些定制化模块,在安全限速方面表现突出,另外它支持对js,css合并

Nginx核心+lua相关的组件和模块组成了一个支持lua的高性能web容器openresty,【参考链接】

下载、解压源码包

cd /usr/local/src

wget http://mirrors.sohu.com/nginx/nginx-1.12.1.tar.gz

tar zxf nginx-1.12.1.tar.gz

配置参数、编译、安装

cd nginx-1.12.1

#配置安装参数,一般只需要指定安装路径prefix即可
./configure --prefix=/usr/local/nginx

#编译、安装
make -j4 &&  make install

#检查是否出错
echo $? 

编辑nginx启动脚本

======nginx启动脚本========
cat >/etc/init.d/nginx<<EOF
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start() 
{
    echo -n \$"Starting \$prog: "
    mkdir -p /dev/shm/nginx_temp
    daemon \$NGINX_SBIN -c \$NGINX_CONF
    RETVAL=\$?
    echo
    return \$RETVAL
}
stop() 
{
    echo -n \$"Stopping \$prog: "
    killproc -p \$NGINX_PID \$NGINX_SBIN -TERM
    rm -rf /dev/shm/nginx_temp
    RETVAL=\$?
    echo
    return \$RETVAL
}
reload()
{
    echo -n \$"Reloading \$prog: "
    killproc -p \$NGINX_PID \$NGINX_SBIN -HUP
    RETVAL=\$?
    echo
    return \$RETVAL
}
restart()
{
    stop
    start
}
configtest()
{
    \$NGINX_SBIN -c \$NGINX_CONF -t
    return 0
}
case "\$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo \$"Usage: \$0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac
exit \$RETVAL
EOF
======nginx启动脚本========

chmod 755 /etc/init.d/nginx

chkconfig --add nginx 


chkconfig nginx on 

cd /usr/local/nginx/conf/; mv nginx.conf nginx.conf.bak
  •  

修改nginx配置文件

cat >nginx.conf<<EOF 
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
    use epoll;
    worker_connections 6000;
}
http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '\$remote_addr \$http_x_forwarded_for [\$time_local]'
    ' \$host "\$request_uri" \$status'
    ' "\$http_referer" "\$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm 
    application/xml;
    server
    {
        listen 80;
        server_name localhost;
        index index.html index.htm index.php;
        root /usr/local/nginx/html;
        location ~ \.php\$ 
        {
            include fastcgi_params;
            fastcgi_pass unix:/tmp/php-fcgi.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html\$fastcgi_script_name;
        }    
    }
}
EOF
  •  

nginx.conf配置文件详解:【参考链接】

启动nginx

#检查配置文件是否有错误
/usr/local/nginx/sbin/nginx -t

#启动nginx服务
/etc/init.d/nginx  start

#检查80端口是否开始监听
netstat -lntp |grep 80

#防火墙允许80端口访问
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload

测试访问

[root@server-lnmp conf]# curl -I -x10.1.1.28:80 localhost
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Mon, 02 Jul 2018 17:05:10 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 02 Jul 2018 16:28:28 GMT
Connection: keep-alive
ETag: "5b3a52ac-264"
Accept-Ranges: bytes

浏览器访问测试 

测试PHP


#在/usr/local/nginx/html/目录下创建一个文件test.php 并写入PHP测试代码

echo -e '<?php\n  echo "test";\n?>' > test.php

在Windows下用浏览器访问这个文件

猜你喜欢

转载自blog.csdn.net/weixin_37817498/article/details/82723077
今日推荐