第二十课预习任务

12.1 LNMP架构介绍
12.2 MySQL安装
12.3/12.4 PHP安装
12.5 Nginx介绍
12.6 Nginx安装

1.LNMP架构介绍

LNMP(Linux-Nginx-MySQL-PHP)网站架构是目前国际流行的Web框架,该框架包括:Linux操作系统,Nginx网络服务器,MySQL数据库,PHP编程语言,所有组成产品均是免费开源软件,这四种软件组合到一起,成为一个免费、高效的网站服务系统。

2.MySQL安装

2.1 这里我们还是来安装MySQL-5.6,先下载并解压到指定目录

//这个下载目录可以到http://r.aminglinux.com这里去下载
[root@knightlai src]# wget http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz

[root@knightlai src]# tar xzvf mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz

2.2 创建用户并创建数据目录

//增加mysql用户
[root@knightlai mysql]# adduser mysql
//创建数据目录
[root@knightlai mysql]# mkdir /data

2.3初始化mysql,拷贝配置文件和启动文件到指定目录

//初始化mysql
[root@knightlai mysql]# ./scripts/mysql_install_db  --user=mysql --datatdir=/data/mysql
Installing MySQL system tables...2018-09-09 15:46:08 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-09-09 15:46:08 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2018-09-09 15:46:08 0 [Note] ./bin/mysqld (mysqld 5.6.39) starting as process 15699 ...
2018-09-09 15:46:08 15699 [Note] InnoDB: Using atomics to ref count buffer pool pages
2018-09-09 15:46:08 15699 [Note] InnoDB: The InnoDB memory heap is disabled
2018-09-09 15:46:08 15699 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2018-09-09 15:46:08 15699 [Note] InnoDB: Memory barrier is not used
2018-09-09 15:46:08 15699 [Note] InnoDB: Compressed tables use zlib 1.2.3
2018-09-09 15:46:08 15699 [Note] InnoDB: Using Linux native AIO
......................................................................
2018-09-09 15:46:14 15699 [Note] Binlog end
2018-09-09 15:46:14 15699 [Note] InnoDB: FTS optimize thread exiting.
2018-09-09 15:46:14 15699 [Note] InnoDB: Starting shutdown...
2018-09-09 15:46:16 15699 [Note] InnoDB: Shutdown completed; log sequence number 1600607
2018-09-09 15:46:16 15699 [Note] ./bin/mysqld: Shutdown complete

[root@knightlai mysql]# cp support-files/my-default.cnf  /etc/my.cnf
[root@knightlai mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@knightlai mysql]# chmod 755 /etc/init.d/mysqld

2.4 编辑配置文件datadir,设置开机启动mysqld

[root@knightlai mysql]# vi /etc/init.d/mysqld
# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.

basedir=/usr/local/mysql
datadir=/data/mysql

[root@knightlai mysql]# chkconfig --add mysqld
[root@knightlai mysql]# chkconfig mysqld on

2.5 测试mysqld是否配置成功,启动并在进程和端口号中查看

启动mysqld
[root@knightlai mysql]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS! 

//查看进程
[root@knightlai mysql]# ps aux|grep mysqld
root      16929  0.0  0.1 113304  1616 pts/1    S    16:05   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/mysql.pid
mysql     17103  1.6 45.0 1302728 449780 pts/1  Sl   16:05   0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/error.log --pid-file=/data/mysql/mysql.pid --socket=/tmp/mysql.sock
root      17127  0.0  0.0 112704   960 pts/1    S+   16:05   0:00 grep --color=auto mysqld

//查看监听端口为3306
[root@knightlai mysql]# netstat -lnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      810/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      898/master          
tcp6       0      0 :::3306                 :::*                    LISTEN      17103/mysqld 

3.PHP安装

3.1 我们先把要安装 的Php下载到/usr/local/src目录下,并解压出来


[root@knightlai src]# wget http://cn2.php.net/distributions/php-5.6.32.tar.bz2
[root@knightlai src]# ls
apr-1.6.3.tar.gz       httpd-2.4.34         mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz  php-5.6.32.tar.bz2      php-5.6.32

3.2创建一个php用户

[root@knightlai src]# adduser php-fpm

3.3进入Php目录编译安装

//编译安装Php
[root@knightlai php-5.6.32]# ./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

//--prefix 指定路径
//--with-config-file-path 指定配置文件所在路径
//--enable-fpm 要加上这个,如果不加就不会有php-fpm执行文件生成,更不能启动php-fpm服务。
//--with-fpm-user=php-fpm --with-fpm-group 指定用户和组
//--with-mysql 指定mysql路径,后面的mysqli,pdo-mysql,mysql-sock也一样

[root@knightlai php-5.6.32]# make &&make install
Installing PEAR environment:      /usr/local/php-fpm/lib/php/
[PEAR] Archive_Tar    - installed: 1.4.3
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.4.2
[PEAR] PEAR           - installed: 1.10.5
Wrote PEAR system config file at: /usr/local/php-fpm/etc/pear.conf
You may want to add: /usr/local/php-fpm/lib/php to your php.ini include_path
/usr/local/src/php-5.6.32/build/shtool install -c ext/phar/phar.phar /usr/local/php-fpm/bin
ln -s -f phar.phar /usr/local/php-fpm/bin/phar
Installing PDO headers:           /usr/local/php-fpm/include/php/ext/pdo/
[root@knightlai php-5.6.32]# echo $?
0

3.4 拷贝配置文件,并配置它

[root@knightlai php-5.6.32]# cp php.ini-production /usr/local/php-fpm/etc/php.ini

[root@knightlai php-5.6.32]# 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 = 127.0.0.1:9000 //内部监听,一般PHP和Nginx在一台机器上
listen.mode = 666 //监听的是sock的这行才会生效,用来定义sock文件的权限是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
//拷贝自启动文件
[root@knightlai php-5.6.32]# cp sapi/fpm/init.d.php-fpm  /etc/init.d/php-fpm

3.5 测试php,将php加入开机启动

[root@knightlai php-5.6.32]# cp sapi/fpm/init.d.php-fpm  /etc/init.d/php-fpm
[root@knightlai php-5.6.32]# chmod 755 /etc/init.d/php-fpm 
//将php加入启动服务中
[root@knightlai php-5.6.32]# chkconfig --add php-fpm
[root@knightlai php-5.6.32]# chkconfig php-fpm on
//启动php
[root@knightlai php-5.6.32]# /etc/init.d/php-fpm start
Starting php-fpm  done
//查看进程中是否php-fpm
[root@knightlai php-5.6.32]# ps aux |grep php-fpm
root      42690  0.1  0.4 227272  4912 ?        Ss   18:43   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm   42691  0.0  0.4 227272  4708 ?        S    18:43   0:00 php-fpm: pool www
php-fpm   42692  0.0  0.4 227272  4708 ?        S    18:43   0:00 php-fpm: pool www
php-fpm   42693  0.0  0.4 227272  4708 ?        S    18:43   0:00 php-fpm: pool www
php-fpm   42694  0.0  0.4 227272  4708 ?        S    18:43   0:00 php-fpm: pool www
php-fpm   42695  0.0  0.4 227272  4712 ?        S    18:43   0:00 php-fpm: pool www
php-fpm   42696  0.0  0.4 227272  4712 ?        S    18:43   0:00 php-fpm: pool www
php-fpm   42697  0.0  0.4 227272  4712 ?        S    18:43   0:00 php-fpm: pool www
php-fpm   42698  0.0  0.4 227272  4712 ?        S    18:43   0:00 php-fpm: pool www
php-fpm   42699  0.0  0.4 227272  4712 ?        S    18:43   0:00 php-fpm: pool www
php-fpm   42700  0.0  0.4 227272  4712 ?        S    18:43   0:00 php-fpm: pool www
php-fpm   42701  0.0  0.4 227272  4716 ?        S    18:43   0:00 php-fpm: pool www
php-fpm   42702  0.0  0.4 227272  4716 ?        S    18:43   0:00 php-fpm: pool www
php-fpm   42703  0.0  0.4 227272  4716 ?        S    18:43   0:00 php-fpm: pool www
php-fpm   42704  0.0  0.4 227272  4716 ?        S    18:43   0:00 php-fpm: pool www
php-fpm   42705  0.0  0.4 227272  4716 ?        S    18:43   0:00 php-fpm: pool www
php-fpm   42706  0.0  0.4 227272  4716 ?        S    18:43   0:00 php-fpm: pool www
php-fpm   42707  0.0  0.4 227272  4716 ?        S    18:43   0:00 php-fpm: pool www
php-fpm   42708  0.0  0.4 227272  4716 ?        S    18:43   0:00 php-fpm: pool www
php-fpm   42709  0.0  0.4 227272  4716 ?        S    18:43   0:00 php-fpm: pool www
php-fpm   42710  0.0  0.4 227272  4716 ?        S    18:43   0:00 php-fpm: pool www
root      42713  0.0  0.0 112708   960 pts/1    S+   18:44   0:00 grep --color=auto php-fpm

4.Nginx介绍

4.1Nginx概述:很多人对apache非常熟悉,Nginx与Apache类似,属于WEB容器,同时也是一款高性能的HTTP和反向代理软件,它们之间最大的差别是Apache的处理速              度很慢,而且占用很多内存资源,而Nginx却恰恰相反,在功能实现上,Apache的所有模块都是支持动静态编译,而Nginx模块都是静态编译的,同时Apache对 Fcgi的支持不好,而Nginx对Fcgi支持非常好,在处理连接方式上,Nginx支持epoll,而Apache却不支持,Nginx安装包只有几百K,而Apache绝对是个庞然大         物;   

4.2Nginx其优势如下:

  • 作为WEB服务器, Nginx处理静态文件、索引文件,自动索引的效率非常高;
  • 作为代理服务器,Nginx可以实现无缓存的反向代理加速,提高网站运行速度;
  • 作为负载均衡服务器、Nginx可以在内部直接支持Redis和PHP,可以支持HTTP代理服务器对外进行服务,同时还支持简单的容错和利用算法进行负载;
  • 在性能方面,Nginx是专门为性能优化开发的,采用Poll模型,最大支持5万并发连接,而且占用很少一部分内存资源;
  • 在稳定方面采取了分阶资源分配技术,使CPU占用资源非常的低,官方表示Nginx保持1万个没有活动的链接,只占用2.5M的内存,DDOS攻击对Nginx来说基本上无效;
  • 在高可用性方面,Nginx支持热部署,启动速度特别迅速,因此可以不间断服务情况下,对软件版本或者配置升级,即使运行数月也无需启动,几乎可以做到7*24小时不间断运行;

4.3Nginx应用

作为Web服务器来说,相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,Nginx处理静态文件、索引文件,自动索引的效率非常高,体现更高的效率。

  • 作为代理服务器,Nginx可以实现无缓存的反向代理加速,提高网站的运行速度。
  • 作为负载均衡服务器,Nginx既可以在内部直接支持Rails和PHP,也可以支持HTTP代理服务器对外进行服务,同时还支持简单的容错和利用算法进行负载均衡;Nginx 用 C 编写, 不论是系统资源开销还是 CPU 使用效率都比 Perlbal 要好的多。
  • 作为邮件代理服务器来说, Nginx是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器)。

5.Nginx安装

5.1 首先还是我们需要安装的Nginx版本下到我们的软件安装目录下并解压出来,下载目录还是到

https://coding.net/u/aminglinux/p/resource/git 这个里面下载相对应的版本

[root@knightlai nginx-1.8.0]# wget http://nginx.org/download/nginx-1.8.0.tar.gz

[root@knightlai src]# ls
apr-1.6.3.tar.gz       httpd-2.4.34         mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz  nginx-1.8.0.tar.gz  php-5.6.32.tar.bz2
apr-util-1.6.1.tar.gz  httpd-2.4.34.tar.gz  nginx-1.8.0                                 php-5.6.32

5.2  编译安装nginx

[root@knightlai nginx-1.8.0]# ./configure --prefix=/usr/local/nginx
checking for OS
 + Linux 3.10.0-862.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
checking for gcc -pipe switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for unistd.h ... found
...............................
[root@knightlai nginx-1.8.0]# make &&make install
make -f objs/Makefile
make[1]: Entering directory `/usr/local/src/nginx-1.8.0'
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/nginx.o \
	src/core/nginx.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/ngx_log.o \
	src/core/ngx_log.c
...........................................

5.3编辑写入启动文件

[root@knightlai nginx-1.8.0]# 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

5.4 更改启动文件权限,将nginx加入自启动服务

[root@knightlai nginx-1.8.0]# chmod 755 /etc/init.d/nginx 
[root@knightlai nginx-1.8.0]# chkconfig --add nginx
[root@knightlai nginx-1.8.0]# chkconfig nginx on

5.5 重新写入配置文件

[root@knightlai nginx]# cd conf
[root@knightlai conf]# ls
fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx.conf          scgi_params          uwsgi_params          win-utf
fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf.default  scgi_params.default  uwsgi_params.default
[root@knightlai conf]# mv nginx.conf nginx.conf.bak
[root@knightlai conf]# vim nginx.conf
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;
        }    
    }
}

5.6 检查配置,并启动nginx

//检查语法没有问题
[root@knightlai conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

//启动nginx并查看端口号可以看到80端口启动是nginx服务
[root@knightlai conf]# /etc/init.d/nginx start
Starting nginx (via systemctl):                            [  OK  ]
[root@knightlai conf]# netstat -lnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      45128/nginx: master 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      810/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      898/master          
tcp6       0      0 :::3306                 :::*                    LISTEN      17103/mysqld        
tcp6       0      0 :::22                   :::*                    LISTEN      810/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      898/master          
udp        0      0 127.0.0.1:323           0.0.0.0:*                           547/chronyd         
udp6       0      0 ::1:323                 :::*                                547/chronyd

猜你喜欢

转载自blog.csdn.net/a1779078902/article/details/82711613
今日推荐