0514--LNMP(上)

任务列表

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

扩展
Nginx为什么比Apache Httpd高效:原理篇 http://www.toxingwang.com/linux-unix/linux-basic/1712.html
https://www.zhihu.com/question/64727674
apache和nginx工作原理比较 http://www.server110.com/nginx/201402/6543.html
概念了解:CGI,FastCGI,PHP-CGI与PHP-FPM https://www.awaimai.com/371.html

预习笔记

LNMP架构介绍

LNMP=linux+nginx+mysql+php
LAMP中,PHP是在Apache中的一个模块,但在LNMP中,PHP是一个单独服务,当用户请求的时候nginx会把它交给php 然后对mysql进行交互。像这种静态的,例如图片或者html,nginx会直接处理,从而加快访问速度。 谈到速度,其实如果一个普通的站点你是看不到什么效果的,但是如果要是访问一个纯静态站点,nginx就能体现出它的优势。nginx的另外一个优势得益于它的高并发支持。
在这里插入图片描述

MySQL安装

进入src目录
1.下载mysql二进制免编译包
wget http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz

2.解包下载下来的文件
tar -zxvf mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz

3.将文件移动到指定的位置并改名
mv mysql-5.6.43-linux-glibc2.12-x86_64 /usr/local/mysql

4.查看mysql目录下的文件
在这里插入图片描述

5.创建mysql用户
useradd mysql

6.创建data目录,如果已存在,则不需要创建
mkdir /data

7.mysql初始化
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

8.修改配置文件,将support-files/my-default.cnf复制到/etc下并重命名为my.cnf,或者直接配置/etc/my.conf文件
在这里插入图片描述

9.复制启动文件support-files/mysql.server至/etc/init.d/目录下,并改名为mysqld
cp support-files/mysql.server /etc/init.d/mysqld
配置启动文件,定义basedir和datadir
在这里插入图片描述
修改启动文件权限为755
chmod 755 /etc/init.d/mysqld

10.将服务添加到启动列表中,设置开机启动并启动服务
[root@linux-01 mysql]# chkconfig --add mysqld
[root@linux-01 mysql]# chkconfig mysqld on
[root@linux-01 mysql]# service mysqld start
在这里插入图片描述

PHP安装

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

1.下载源码包
wget http://cn2.php.net/distributions/php-5.6.32.tar.bz2

2.解压文件
tar -xjvf php-5.6.32.tar.bz2

3.进入php-5.6.32目录,通过**./configure**进行定制相关功能,使其生成makefile;
./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=启动fpm服务
--with-fpm-user=指定用户
--with-fpm-group=指定用户组
--with-mysql=指定mysql的路径
make

[pdo_mysql.lo] Error 1 或者 [php_mysql.lo] Error 1
make: *** [pdo_mysql.lo] Error 1
make: *** [php_mysql.lo] Error 1

这是因为这是因为在编译时需要 MySQL 的头的文件。而它按默认搜索找不到头文件的位置,所以才出现这个问题。通过软连接把MySQL头文件对应到/usr/local/include/下就好
比如你的MySQL安装文件位于/usr/local/mysql,那么就执行以下命令:

ln -s /usr/local/mysql/include/* /usr/local/include/

make install
在这里插入图片描述

4.查看目录php-fpm
在这里插入图片描述
安装之后我们发现比之前安装PHP要多了 sbin 和 var目录
sbin:启动php-fpm服务的目录;
var:存放PHP日志的;
log: php日志目录;
run:进程PID目录
/usr/local/php-fpm/sbin/php-fpm用法
-i 查看PHP信息
-m 查看PHP模块
-t 测试PHP配置语法是否正确

php配置文件

php-fpm.conf是PHP-FPM特有的配置文件
php.ini是所以php模式中必须的配置文件
两者的区别是,php-fpm.conf是PHP-FPM进程管理器的配置文件,php.ini是PHP解析器的配置文件
有的PHP版本的配置文件路径中还有/fpm.d/www.conf配置文件
这是php-fpm.conf配置的文件的扩展文件,可以打开php-fpm.conf文件查看 
include = etc/php-fpm.d/*.conf

将源码包中php.ini-production文件复制到mv php.ini-production /usr/local/php-fpm/etc/目录中,并且重命名为php.ini
cp /usr/local/src/php-5.6.32/php.ini-production /usr/local/php-fpm/etc/php.ini
在/usr/local/php-fpm/etc/创建php-fpm.conf配置文件并写入以下内容
在这里插入图片描述
相关名词释义
[global]=定义全局参数
[www]=自定义模块
listen = 监听可以配置成以下两种方式
listen = 127.0.0.1:9000 (port默认为9000,也可以更改)
listen.mode = 666 用来定义listen = /tmp/php-fcgi.sock的权限,只有这个sock打开的情况下才生效!

配置启动文件,将/usr/local/src/php-5.6.32/sapi/fpm/init.d.php-fpm复制到/etc/init.d/下,并将文件改名为php-fpm权限改为755
[root@linux-01 src]# cp /usr/local/src/php-5.6.32/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@linux-01 src]# chmod 755 /etc/init.d/php-fpm
创建启动用户 useradd php-fpm

增加到开机服务中,并启动服务
在这里插入图片描述

Nginx介绍

因为nginx处理静态文件的能力要比apache好很多,所以很多企业在建站的时候一般都是用java写的,然后会选择tomcat,但是tomcat处理静态文件的能力不是太好就会叠加选择nginx。
nginx特点:体积小、处理能力强、并发高、可扩展性好
Nginx应用场景:web服务、反向代理、负载均衡
Nginx著名分支,淘宝基于Nginx开发的Tengine,使用上和Nginx一致,服务名,配置文件名都一样,和Nginx的最大区别在于Tenging增加了一些定制化模块,在安全限速方面表现突出,另外它支持对js,css合并 Nginx核心+lua(开发语言)相关的组件和模块组成了一个支持lua的高性能web容器openresty,参考http://jinnianshilongnian.iteye.com/blog/2280928

Nginx安装

下载nginx
wget http://nginx.org/download/nginx-1.4.7.tar.gz

解压下载的文件
tar -xzvf nginx-1.4.7.tar.gz

进入文件目录进行安装,生成make file ,make进行编译,make install进行安装
./configure --prefix=/usr/local/nginx
make
make install
在这里插入图片描述

nginx目录文件
在这里插入图片描述
conf nginx配置文件
html 主页样例文件
logs 站点日志
sbin 核心进程文件

检查配置文件语法是否正确 /usr/local/nginx/sbin/nginx -t
在这里插入图片描述

Nginx配置,配置启动文件
在init.d目录下,新建nginx文件

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

修改启动文件权限为755,chmod 755 /etc/init.d/nginx
将nginx加入到系统服务中,并开机启动
在这里插入图片描述

编辑配置文件
进入nginx/conf/目录下,把默认的配置文件作为备份;
新建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;
        }    
    }
}
释义:
user nobody nobody; 运行服务的用户是谁
worker_processes 2;定义子进程的数量
worker_rlimit_nofile
51200;最多可以打开多少个文件
worker_connections 6000;允许最大的连接数
server; 下面对应的就是虚拟主机配置
server_name localhost;定义网站的域名
root /usr/local/nginx/html;定义网站的根目录
location ~ .php$;配置解析PHP
fastcgi_pass unix:/tmp/php-fcgi.sock;监听端口或者监听socket,通过此命令去执行
fastcgi_pass 127.0.0.1:9000;(或者携程这种方式,服务器IP地址+端口)
可参考文章:https://blog.csdn.net/wangbin_0729/article/details/82109693

检查配置文件
[root@linux-01 conf]# /usr/local/nginx/sbin/nginx -t
在这里插入图片描述

启动服务
在这里插入图片描述
测试
在这里插入图片描述
测试解析PHP
vim /usr/local/nginx/html/1.php

<?php
echo "nginx";

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值