12.1 LNMP架构介绍
● 和LAMP不同的是,提供web服务的Nginx
● 并且PHP是作为一个独立服务存在的,这个服务叫 PHP-fpm
● Nginx直接处理静态请求,动态请求会转发给PHP-fpm
● ps Nginx处理静态的请求要比Apache快很多,这是底层设计形成的逻辑是不一样的。单纯访问PHP的网站的话,感觉不出来,Apache多么不好,Nginx有多快,但是当你访问静态为主的网站,全部都是静态文件(图片, html)的网站,使用Nginx很明显,它的用户并发,支持会很大,可以上好几万,而Apache做不到.
12.2 MySQL安装
1.1进入src目录下,下载MySQL&解压
[root@root-01 ~]# cd /usr/local/src[root@root-01 src]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz[root@root-01 src]# tar -zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
1.2 将下载的包移动到/usr/local/下(会自动创建一个mysql目录)
[root@root-01 src]# mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql
1.3 进入mysql目录
[root@root-01 src]# mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql[root@root-01 src]# cd /usr/local/mysql/[root@root-01 mysql]# lsbin COPYING data docs include lib man mysql-test README scripts share sql-bench support-files
1.4 创建mysql用户和创建/data/目录
[root@root-01 mysql]# useradd mysql[root@root-01 mysql]# mkdir /data/
1.5初始化
[root@root-01 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysqldInstalling MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory# 初始化报错缺少libaio.so.1 这个库 解决安装libaioyum install -y libaio
1.6 拷贝配置文件到/etc/下 & 拷贝启动脚本到/etc/init.d下
[root@root-01 mysql]# cp support-files/my-default.cnf /etc/my.cnfcp:是否覆盖"/etc/my.cnf"? y[root@root-01 mysql]# cp support-files/mysql.server /etc/init.d/mysqldcp:是否覆盖"/etc/init.d/mysqld"? y1.7 编辑配置文件
[root@root-01 mysql]# vim /etc/init.d/mysqld找到:basedir=datadir=修改为:basedir=/usr/local/mysqldatadir=/data/mysql1.8 启动mysql
[root@root-01 mysql]# /etc/init.d/mysqld startStarting MySQL.Logging to '/data/mysqld/root-01.err'. SUCCESS! # 查看mysql启动是否成功[root@root-01 mysql]# ps aux |grep mysqlroot 5454 0.0 0.1 113252 1616 pts/0 S 13:02 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysqld --pid-file=/data/mysqld/root-01.pidmysql 5586 0.4 45.2 973040 453484 pts/0 Sl 13:02 0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysqld --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysqld/root-01.err --pid-file=/data/mysqld/root-01.pidroot 5613 0.0 0.0 112660 960 pts/0 S+ 13:04 0:00 grep --color=auto mysql#监听3306端口[root@root-01 mysql]# netstat -nlpt |grep mysqldtcp6 0 0 :::3306 :::* LISTEN 5586/mysqld #把mysql服务加到启动服务列表里去[root@root-01 mysql]# chkconfig --add mysqld#开机启动[root@root-01 mysql]# chkconfig mysqld on
12.3/12.4 PHP安装
1.1 进入src目录下,下载和解压php
[root@root-01 ~]# cd /usr/local/src[root@root-01 src]# wget http://cn2.php.net/distributions/php-5.6.30.tar.gz[root@root-01 src]# tar -zxvf php-5.6.30.tar.gz
1.2 进入PHP目录,编译和安装
[root@root-01 src]# cd php-5.6.30#编译:[root@root-01 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 --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-tty --enable-ftp --enable-mbstring --enable-exif--with-pear --with-curl --with-openssl#报错提示:configure: error: mcrypt.h not found. Please reinstall libmcrypt#解决:安装libmcrypt第1步:下载&解压[root@root-01 src]# wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz [root@root-01 src]# tar -zxvf libmcrypt-2.5.7.tar.gz 第2步:进入libmcrypt目录,编译&安装[root@root-01 src]# cd libmcrypt-2.5.7[root@root-01 libmcrypt-2.5.7]# ./configure --prefix=/usr/local/libmcrypt [root@root-01 libmcrypt-2.5.7]# make && make install进入php目录重新编译ps: 原参数:--with-mcrypt 修改为:--with-libmcrypt=/usr/local/libmcrypt[root@root-01 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 --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-libmcrypt=/usr/local/libmcrypt --enable-soap --enable-gd-native-tty --enable-ftp --enable-mbstring --enable-exif--with-pear --with-curl --with-openssl[root@root-01 php-5.6.30]# echo $?0[root@root-01 php-5.6.30]# make && make install
1.3 检查语法的命令/usr/local/php-fpm/sbin/php-fpm -t
[root@root-01 php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -t[03-Sep-2017 13:32:44] ERROR: failed to open configuration file '/usr/local/php-fpm/etc/php-fpm.conf': No such file or directory (2)[03-Sep-2017 13:32:44] ERROR: failed to load configuration file '/usr/local/php-fpm/etc/php-fpm.conf'[03-Sep-2017 13:32:44] ERROR: FPM initialization failed
1.4 拷贝配置文件
[root@root-01 php-5.6.30]# cp php.ini-production /usr/local/php-fpm/etc/php.ini#拷贝到/etc/下,并重命名
1.5 拷贝启动脚本
[root@root-01 php-5.6.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpmcp:是否覆盖"/etc/init.d/php-fpm"? y# /etc/init.d下,重命名为php-fpm,并给它设置权限为755[root@root-01 php-5.6.30]# chmod 755 /etc/init.d/php-fpm
1.6 进入/usr/local/php-fpm/etc/目录,创建php-fpm.conf文件,并写入配置
[root@root-01 php-5.6.30]# cd /usr/local/php-fpm/etc/[root@root-01 etc]# vim php-fpm.conf[global]pid = /usr/local/php-fpm/var/run/php-fpm.piderror_log = /usr/local/php-fpm/var/log/php-fpm.log[www]#listen = /tmp/php-fcgi.socklisten = 127.0.0.1:9000listen.mode = 666user = php-fpmgroup = php-fpmpm = dynamicpm.max_children = 50pm.start_servers = 20pm.min_spare_servers = 5pm.max_spare_servers = 35pm.max_requests = 500rlimit_files = 10241.7 php-fpm加入服务列表里面去,并开机启动
[root@root-01 etc]# chkconfig --add php-fpm[root@root-01 etc]# chkconfig php-fpm on1.8 启动PHP
[root@root-01 etc]# service php-fpm startStarting php-fpm done#查看PHP服务是否启动[root@root-01 etc]# ps aux |grep php-fpmroot 6739 0.0 0.4 120756 4904 ? Ss 13:42 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)php-fpm 6740 0.0 0.4 120756 4628 ? S 13:42 0:00 php-fpm: pool wwwphp-fpm 6741 0.0 0.4 120756 4628 ? S 13:42 0:00 php-fpm: pool wwwphp-fpm 6742 0.0 0.4 120756 4628 ? S 13:42 0:00 php-fpm: pool wwwphp-fpm 6743 0.0 0.4 120756 4628 ? S 13:42 0:00 php-fpm: pool wwwphp-fpm 6744 0.0 0.4 120756 4632 ? S 13:42 0:00 php-fpm: pool wwwphp-fpm 6745 0.0 0.4 120756 4632 ? S 13:42 0:00 php-fpm: pool wwwphp-fpm 6746 0.0 0.4 120756 4632 ? S 13:42 0:00 php-fpm: pool wwwphp-fpm 6747 0.0 0.4 120756 4632 ? S 13:42 0:00 php-fpm: pool wwwphp-fpm 6748 0.0 0.4 120756 4632 ? S 13:42 0:00 php-fpm: pool wwwphp-fpm 6749 0.0 0.4 120756 4632 ? S 13:42 0:00 php-fpm: pool wwwphp-fpm 6750 0.0 0.4 120756 4632 ? S 13:42 0:00 php-fpm: pool wwwphp-fpm 6751 0.0 0.4 120756 4636 ? S 13:42 0:00 php-fpm: pool wwwphp-fpm 6752 0.0 0.4 120756 4636 ? S 13:42 0:00 php-fpm: pool wwwphp-fpm 6753 0.0 0.4 120756 4636 ? S 13:42 0:00 php-fpm: pool wwwphp-fpm 6754 0.0 0.4 120756 4636 ? S 13:42 0:00 php-fpm: pool wwwphp-fpm 6755 0.0 0.4 120756 4636 ? S 13:42 0:00 php-fpm: pool wwwphp-fpm 6756 0.0 0.4 120756 4636 ? S 13:42 0:00 php-fpm: pool wwwphp-fpm 6757 0.0 0.4 120756 4636 ? S 13:42 0:00 php-fpm: pool wwwphp-fpm 6758 0.0 0.4 120756 4636 ? S 13:42 0:00 php-fpm: pool wwwphp-fpm 6759 0.0 0.4 120756 4636 ? S 13:42 0:00 php-fpm: pool wwwroot 6761 0.0 0.0 112660 964 pts/0 S+ 13:43 0:00 grep --color=auto php-fpm
12.5 Nginx介绍
●Nginx官网:Nginx.org,最新版本1.13,最稳定版本1.12
●Nginx应用场景:web服务、反向代理、负载均衡
●Nginx著名分支,淘宝基于Nginx开发的Tengine,使用上和Nginx一致,服务名,配置文件名都一样,和Nginx最大的区别在于Tengine增加了一些定制化模块,在安全限速方面表现突出,另外他支持js,css合并
●Nginx核心+lua相关的组件和模块组成了一个支持lua的高性能web容器openresty
扩展 Nginx为什么比Apache Httpd高效:
原理篇 apache和nginx工作原理比较 mod_php 和 mod_fastcgi以及php-fpm的比较 概念了解:CGI,FastCGI,PHP-CGI与PHP-FPM12.6 Nginx安装
1.1 下载和解压Nginx
[root@root-01 src]# wget http://nginx.org/download/nginx-1.12.1.tar.gz[root@root-01 src]# tar -zxvf nginx-1.12.1.tar.gz1.2 进入Nginx目录,编译&安装
[root@root-01 src]# cd nginx-1.12.1[root@root-01 nginx-1.12.1]# lsauto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src[root@root-01 nginx-1.12.1]# ./configure --prefix=/usr/local/nginx[root@root-01 nginx-1.12.1]# make && make install给Nginx创建启动脚本
ps:vim /etc/init.d/nginx -->
这是一个空文件,脚本内容需要: 这里面去复制.[root@root-01 nginx-1.12.1]# vim /etc/init.d/nginx #!/bin/bash# chkconfig: - 30 21# description: http service.# Source Function Library. /etc/init.d/functions# Nginx SettingsNGINX_SBIN="/usr/local/nginx/sbin/nginx"NGINX_CONF="/usr/local/nginx/conf/nginx.conf"NGINX_PID="/usr/local/nginx/logs/nginx.pid"RETVAL=0prog="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=1esacexit $RETVAL
给/etc/init.d/nginx 设置权限为755
[root@root-01 nginx-1.12.1]# chmod 755 /etc/init.d/nginx1.3 把Nginx加入服务列表并开机启动
[root@root-01 nginx-1.12.1]# chkconfig --add nginx[root@root-01 nginx-1.12.1]# chkconfig nginx on1.4 启动Nginx
[root@root-01 ~]# /etc/init.d/nginx startStarting nginx (via systemctl): [ 确定 ]