LNMP架构介绍

LNMP架构

LLNMP和·LAMP唯一不同·的是指提供web服务的是Nginx,
在Apache中,PHP是作为一个模块存在的。而在Nginx中,PHP是作为一个独立服务存在的,这个服务叫做php-fpm。
Nginx直接处理静态请求,动态请求会转发给php-fpm.
这里写图片描述

mysql安装

由于之前做LAMP时安装过这里想要安装MySQL,就要先将前面的MySQL删除。

删掉一下文件:

[root@shuai-01 src]# rm -rf /usr/local/mysql

删除启动脚本:

[root@shuai-01 src]# rm -rf /etc/init.d/mysqld

删除/data/mysql:

[root@shuai-01 mysql]# rm -rf /data/mysql/*

MySQL的配置文件(/etc/my.cnf)可以不用删

在/usr/local/src/目录下,下载MySQL的包。

[root@shuai-01 ~]# cd /usr/local/src/
[root@shuai-01 src]# ls
apr-1.6.3               mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz
apr-1.6.3.tar.gz        mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
apr-util-1.6.1          php-5.6.30
apr-util-1.6.1.tar.bz2  php-5.6.30.tar.bz2
httpd-2.2.34            php-7.1.6
httpd-2.2.34.tar.gz     php-7.1.6.tar.bz2
httpd-2.4.29            phpredis-develop
httpd-2.4.29.tar.gz     phpredis-develop.zip

下载完后解压:

[root@shuai-01 src]# tar zxvf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz

解压完后,将其移动并重命名:

[root@shuai-01 src]# mv mysql-5.6.36-linux-glibc2.5-x86_64 /usr/local/mysql

进入MySQL下:

[root@shuai-01 src]# cd /usr/local/mysql/
[root@shuai-01 mysql]# ls
bin      data  include  man         README   share      support-files
COPYING  docs  lib      mysql-test  scripts  sql-bench

创建MySQL的用户,创建/data/mysql时,属组属主是MySQL创建的用户。

初始化:

[root@shuai-01 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

初始化完成(要出现两个OK,或者echo $? 为0)

更改配置文件(/etc/my.cnf)

[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock

[外链图片转存失败(img-wndB0I6n-1562386816909)(https://img-blog.csdn.net/20180104211117262?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYW9saV9zaHVhaQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

拷贝一个启动脚本过来:

[root@shuai-01 mysql]# cp support-files/mysql.server /etc/init.d/mysql

编辑启动脚本:

[root@shuai-01 mysql]# vim /etc/init.d/mysql

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

这里写图片描述
这个时候就可以启动了。

[root@shuai-01 mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/shuai-01.err'.
... SUCCESS! 

设定开机自启动:

[root@shuai-01 mysql]# chkconfig --add mysqld
[root@shuai-01 mysql]# chkconfig mysqld on

##PHP安装

PHP在LAMP和LNMP安装是有差别的。

删掉PHP:
进入到目录下(PHP源码包下):

[root@shuai-01 src]# cd php-5.6.30/

删除之前编译过的文件:

[root@shuai-01 php-5.6.30]# make clean
find . -name \*.gcno -o -name \*.gcda | xargs rm -f
find . -name \*.lo -o -name \*.o | xargs rm -f
find . -name \*.la -o -name \*.a | xargs rm -f 
find . -name \*.so | xargs rm -f
find . -name .libs -a -type d|xargs rm -rf
rm -f libphp5.la sapi/cli/php sapi/cgi/php-cgi    libphp5.la modules/* libs/*

删掉之后。PHP就像是一个刚刚解压的文件
取名为php-fpm:

编译:

[root@shuai-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-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也一样

编译完成后就开始make && make install

拷贝配置文件,php.ini是PHP运行的核心文件

[root@shuai-01 php-5.6.30]# cp php.ini-production /usr/local/php-fpm/etc/php.ini

添加php-fpm用户

[root@shuai-01 php-5.6.30]# useradd php-fpm

编辑php-fpm.conf配置文件:

vi /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@shuai-01 php-5.6.30]# cd /usr/local/src/php-5.6.30/

[root@shuai-01 php-5.6.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

把它加入服务列表里去,先改权限:

[root@shuai-01 php-5.6.30]# chmod 755 /etc/init.d/php-fpm 
[root@shuai-01 php-5.6.30]# chkconfig --add php-fpm
chkconf[root@shuai-01 php-5.6.30]# chkconfig php-fpm on

问题1:

checking for cURL in default path... not found
configure: error: Please reinstall the libcurl distribution -
    easy.h should be in <curl-dir>/include/curl/

这里写图片描述

缺少cURL这个包,安装

[root@shuai-01 php-5.6.30]# yum install libcurl-devel

安装完成再编译

##nginx介绍

Nginx应用场景:web服务,反向代理,负载均衡。
官网:nginx.org ,有最新版,稳定版(stable)。
淘宝基于Nginx开发的Tengine,跟Nginx一致,服务名,配置文件都一样,最大的区别是增加了一些定制化的模块,在安全方面。支持js,css这些静态元素的合并。

Nginx安装

进入/usr/local/src目录下,下载一个Nginx的稳定版。

[root@shuai-01 ~]# cd /usr/local/src
[root@shuai-01 src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz

解压压缩包:

[root@shuai-01 src]# tar zxvf nginx-1.12.2.tar.gz

编译:
进入Nginx目录下
编译根据自己的要求来编译,选择一些模块。
我这里只指定文件路径:

[root@shuai-01 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx

接着make:

[root@shuai-01 nginx-1.12.2]# make

make install:

[root@shuai-01 nginx-1.12.2]# make install

[root@shuai-01 nginx-1.12.2]# ls /usr/local/nginx/
conf  html  logs  sbin

conf : 配置文件目录
html : 网页样例
logs: 日志
sbin: 进程,核心文件

给配置文件做一个启动脚本(/etc/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

更改配置文件权限:

[root@shuai-01 nginx-1.12.2]# chmod 755 /etc/init.d/nginx 

添加为系统服务,并设置为开机自启动:

[root@shuai-01 nginx-1.12.2]# chkconfig --add nginx
[root@shuai-01 nginx-1.12.2]# chkconfig nginx on

配置Nginx的配置文件:
Nginx的conf目录下有nginx.conf这个文件了。

[root@shuai-01 nginx-1.12.2]# cd /usr/local/nginx/conf
[root@shuai-01 conf]# 
[root@shuai-01 conf]# ls
fastcgi.conf            koi-win             scgi_params
fastcgi.conf.default    mime.types          scgi_params.default
fastcgi_params          mime.types.default  uwsgi_params
fastcgi_params.default  nginx.conf          uwsgi_params.default
koi-utf                 nginx.conf.default  win-utf

不用,用自己创建的配置文件。
先将nginx.conf改名

[root@shuai-01 conf]# mv nginx.conf nginx.conf.1

自己创建

[root@shuai-01 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;
        }    
    }
}

编辑配置文件后,检查语法:

[root@shuai-01 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

[root@shuai-01 conf]# /etc/init.d/nginx start
Starting nginx (via systemctl):                            [  确定  ]

user 用来定义启动nginx服务的是那个用户。
worker_processes 2; //子进程有两个

error_log /usr/local/nginx/logs/nginx_error.log crit; //错误日志

worker_rlimit_nofile 51200;//nginx最多打开多少个文件
events
{
use epoll;//使用epoll模式
worker_connections 6000;//最多的连接数
}

server 和Apache配置文件中的virtual hosts 一样,对应虚拟主机。

    server
    {
        listen 80; //监听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; //指定PHP-fpm的监听端口或者监听sock
	            监听127.0.0.1:9000
	            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
        } // 配置解析PHP的部分
    }

nginx配置详解:
http://www.ha97.com/5194.html
https://my.oschina.net/duxuefeng/blog/34880

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值