LNMP环境配置 LNMP架构介绍 MySQL PHP Nginx介绍 安装

LNMP架构介绍

LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。Nginx中的PHP是以fastcgi的方式结合Nginx的,可以理解为Nginx代理了PHP的fastcgi。

  • 和LAMP不同的是,LNMP中提供web服务的是Nginx。
  • 在LNMP架构中PHP是作为一个独立的服务存在的,这个服务叫做php-fpm。
  • Nginx直接处理静态请求(支持的并发更高,速度比Apache快),动态请求转发给php-fpm处理 LNMP处理请求过程:

1、用户浏览器发起访问请求。

2、服务器收到请求后,Nginx服务处理静态请求,如图片、css等。

3、Nginx把动态请求发给php-fpm服务,由他进行处理。

4、php-fpm与mysql进行数据库交互。

5、Nginx把静态动态请求处理结果返回给用户。

Nginx比Apache在静态处理性能方面强很多,特别是图片多的网站用户并发可以上几万。Apache做不到这点性能

1、MySQL安装

  1. cd /usr/local/src //切换到下载源码包的目录
  2. wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
  3. tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz //解压
  4. mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql //改名,并放到/usr/local/mysql
  5. cd /usr/local/mysql //进入到该目录下
  6. useradd mysql //创建一个mysql用户
  7. mkdir /data/ //创建一个目录,是为了存放mysql的数据,把数据目录放到这个目录下
  8. ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql //初始化,所谓初始化就是要生成/data/mysql ,因为mysql要想启动,首先要有一个自带的库,自带的库叫mysql
  9. cp support-files/my-default.cnf /etc/my.cnf //拷贝配置文件到/etc/并改名my.cnf
  10. cp support-files/mysql.server /etc/init.d/mysqld //拷贝启动脚本到启动目录
  11. 权限变更:chmod 755 /etc/init.d/mysqld
  12. chkconfig --add mysqld //添加到系统服务使其开机启动 也可以命令启动服务:/etc/init.d/mysqld start 或service mysqld start
  13. vi /etc/init.d/mysqld //编辑定义vi /etc/init.d/mysqld // -定义basedir和datadir路径

2、PHP安装

  • 和LAMP安装PHP有区别,需要开启php-fpm服务
  1. 进入目录:cd /usr/local/src/

  2. 下载PHP5.6包:wget http://cn2.php.net/distributions/php-5.6.30.tar.gz

  3. 解压:tar zxvf php-5.6.30.tar.gz

  4. 创建账号:useradd -s /sbin/nologin php-fpm

  5. 进入目录:cd php-5.6.30

  6. 编译PHP5.6:[root@localhost src]# cd php-5.6.30 [root@localhost 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

  7. 然后:make && make install

PHP安装可能会遇到缺少的包

  • yum install -y libxml2-devel //XML2
  • yum install -y openssl-devel //缺少OpenSSL
  • yum install -y bzip2-devel //缺少BZip2
  • yum install -y libjpeg-devel //缺少libjpeg
  • yum install -y libpng-devel //缺少libpng-devel
  • yum install -y freetype-devel //缺少freetype-devel
  • yum install -y libcurl-devel //缺少libcurl-devel
  • yum install -y epel-release && yum install -y libmcrypt-devel //缺少libmcrypt-devel

make clean:清除其原有make配置,因之前安装过PHP

  • 检查php-fpm核心文件及安装目录结构
[root@localhost ~]# ls /usr/local/php-fpm/
bin  etc  include  lib  php  sbin  var 
[root@localhost ~]# ls /usr/local/php-fpm/sbin/
php-fpm       //php-fpm的核心二进制文件
[root@localhost ~]# ls /usr/local/php-fpm/var/
log  run     //log 存放日志的目录、run存放pid的目录
[root@localhost ~]# 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,本地监听,也可以监听其他IP:port
#此处格式会影响配置Nginx和PHP结合时Nginx寻址PHP的路径
listen.mode = 666
#当监听的为socket文件时该部分才生效,用于指定.sock文件的权限
user = php-fpm
group = php-fpm
#定义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@localhost php-5.6.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
更改文件权限:
[root@localhost php-5.6.30]# chmod 755 /etc/init.d/php-fpm
添加到系统服务:
[root@localhost php-5.6.30]# chkconfig --add php-fpm
设置开机启动:
[root@localhost php-5.6.30]# chkconfig php-fpm on
启动php-fpm服务:
[root@localhost php-5.6.30]# service php-fpm start
Starting php-fpm  done

3、Nginx介绍

  • Nginx是俄国人开发的一个小巧而强大的web服务,可以到官网下载: nginx.org 最新的版本是1.15,稳定版本是1.12 stable
  • nginx有很多的应用场景:web服务、反向代理、负载均衡等。
  • nginx的静态文件处理性能比Apache强很多。因为淘宝网站用nginx+tomcat来搭建java环境
  • nginx有一个著名的分支:淘宝网基于nginx定制开发的Tengine,他的配置文件名和服务名基本与nginx一致,是在nginx的基础上增加了一些定制化的模块,在安全限速方面有比较突出的性能,另外还支持js、css的合并,因为淘宝网站是大型的购物平台,很多商品展示的页面,含有大量的静态文件,支持这些文件的合并,能够大大减少资源的请求,提高服务器的处理效率。
  • nginx的核心组件+lua相关模块还能够组成一个支持lua的高性能的web容器openresty

4、Nginx安装

  • 下载并解压安装包
  1. cd /usr/local/src/ //进入目录
  2. wget http://nginx.org/download/nginx-1.12.1.tar.gz //下载Nginx包
  3. tar zxvf nginx-1.12.1.tar.gz 解压包
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# wget http://nginx.org/download/nginx-1.12.1.tar.gz
--2018-07-01 16:01:23--  

[root@localhost src]# ls
apr-1.6.3               httpd-2.4.29.tar.gz                           php-5.6.30.tar.gz
apr-1.6.3.tar.gz        mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz  php-7.1.6
apr-util-1.6.1          mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz     php-7.1.6.tar.bz2
apr-util-1.6.1.tar.bz2  nginx-1.12.1.tar.gz                           phpredis-develop
httpd-2.4.29            php-5.6.30                                    phpredis-develop.zip
[root@localhost src]# tar zxf nginx-1.12.1.tar.gz
  • 配置编译选项
  1. cd nginx-1.12.1
  2. ./configure --prefix=/usr/local/nginx
[root@localhost src]# cd nginx-1.12.1/
[root@localhost nginx-1.12.1]# ./configure --prefix=/usr/local/nginx
  • 编译和安装
  1. make && make install
[root@localhost nginx-1.12.1]# make && make install
make -f objs/Makefile
[root@localhost nginx-1.12.1]# echo $?
0
  • 安装成功,查看nginx目录结构及核心文件
[root@localhost nginx-1.12.1]# cd /usr/local/nginx/
[root@localhost nginx]# ls
conf  html  logs  sbin  //分别是配置文件目录、网站目录、日志目录、进程目录
[root@localhost nginx]# ls -l sbin/nginx  //核心二进制文件
-rwxr-xr-x. 1 root root 3659680 71 16:49 sbin/nginx
[root@localhost nginx]# vim /etc/init.d/nginx

复制参考脚本保存退出。

  • 编辑启动脚本,修改为755权限;加入开机启动列表,设置开机启动:启动nginx服务
[root@localhost nginx]# chmod 755 /etc/init.d/nginx  //修改为755权限
[root@localhost nginx]# ls -l /etc/init.d/nginx
-rwxr-xr-x. 1 root root 1133 71 17:02 /etc/init.d/nginx
[root@localhost nginx]# chkconfig --add nginx //加入开机启动列表
[root@localhost nginx]# chkconfig nginx on  //设置开机启动
[root@localhost nginx]# /etc/init.d/nginx start //启动nginx服务
Starting nginx (via systemctl):
[root@localhost conf]# vim nginx.conf
[root@localhost 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
[root@localhost conf]# /etc/init.d/nginx start  //启动服务

查看进程

[root@localhost conf]# ps aux |grep nginx
root     13170  0.0  0.0  20540   620 ?        Ss   17:20   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody   13171  0.0  0.3  22984  3204 ?        S    17:20   0:00 nginx: worker process
nobody   13172  0.0  0.3  22984  3204 ?        S    17:20   0:00 nginx: worker process
root     13175  0.0  0.0 112720   980 pts/0    S+   17:21   0:00 grep --color=auto nginx

测试:

Nginx测试:
[root@localhost conf]# curl localhost -I
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Sun, 01 Jul 2018 09:28:32 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sun, 01 Jul 2018 08:49:36 GMT
Connection: keep-alive
ETag: "5b3895a0-264"
Accept-Ranges: bytes
PHP测试:
[root@localhost conf]# vim /usr/local/nginx/html/1.php //创建php
[root@localhost conf]# curl localhost/1.php
this is nginx test page 
[root@localhost conf]#

猜你喜欢

转载自blog.csdn.net/xou6363/article/details/80875824