2019-05-06day46

LNMP Web服务搭建

L代表:Linux

N代表:Nginx

M代表:MySQL

P代表:PHP

四个环境:

1、 JAVA环境(企业更多)

[Tomcat(jvm)]、resin、jboss、Weblogic

2、 Python环境

配合Nginx uwsgi_pass代理功能

3、 PHP Web环境(也会用,二三四五线用的多)

配合nginx fastcgi_pass代理功能

4、 GO语言环境

工作原理:

当LNMP组合工作时,首先是用户通过浏览器输入域名请求Nginx Web服务如果请求的是静态资源 ,则由Nginx解析返回给用户,如果是动态请求(.php结尾),那么Nginx就会把它通过Fastcgi接口发送给PHP引擎服务进行解析,如果这个动态请求要读取数据库数据,那么PHP就会即系向后请求MySQL数据库,以读取需要的数据,并最终通过Nginx服务把获取的数据返回给用户,这就是LNMP环境的基本请求顺序流程。


image.png
数据库产品:MySQL(mariadb)、Oracle、SQL SERVER、PGSQL

MySQL优点:

(1) 性能卓越,服务稳定,很少出现异常宕机。
(2) 开放源代码且无版权制约,自主性强,使用成本低。
(3) 历史悠久,社区及用户非常活跃,遇到问题,可以很快获取到帮助。
(4) 软件体积小,安装使用简单,并且易于维护,安装及维护成本低。
(5) 支持多种操作系统,提供多种API接口,支持多种开发语言,特别对PHP语言无缝支持。
(6) 品牌口碑小于,使得企业无需考虑就直接用之。

访问量大的时候可以在每一台机器上安装。

MySQL软件下载地址:

http://mirrors.163.com/mysql/Downloads/MySQL-5.7/

初始化数据库参数详解:

--user=mysql #<==mysql用户。
--basedir=/application/mysql/ #<==MySQL根目录。
--datadir=/application/mysql/data #<==MySQL数据文件目录。
--initialize-insecure #<==关闭MySQL安全策略,本书选择了关闭策略。
--initialize #<==开启MySQL安全策略,高安全环境采用。

FastCGI介绍

1、什么是CGI?

CGI的全称为“通用网关接口”(Common Gateway Interface),为HTTP服务器与其他机器上的程序服务通信交流的一种工具,CGI程序须运行在网络服务器上。

传统的CGI接口方式的主要是缺点是性能较差,因为每次HTTP服务器遇到动态程序时都需要通过重新启动解析器来执行解析,之后结果才会被返回给HTTP服务器。这在处理高并发访问时几乎是不可能的,因此就诞生了F阿斯钢CGI。另外,传统的CGI接口方式安全性也很差,故而现在很少被使用了。

2、什么是FastCGI?

FastCGI是一个可伸缩地、高速地在HTTP服务器和动态脚本语言间通信的接口(在Linux下,FastCGI接口即为socket,这个socket可以是文件socket,也可以是IP socket),主要优点是把动态语言和HTTP服务器分离开来。多数流行的HTTP服务器都支持FastCGI,包括Apache、Nginx和Lighttpd等。
同时FastCGI也被许多脚本语言所支持,比较流行的脚本语言之一为PHP。FastCGI接口采用的是C/S架构,它可以将HTTP服务器和脚本解析服务器分开,同时还能再脚本解析服务器上启动一个或多个脚本来解析守护进程。当HTTP服务器遇到动态程序时,可以将其直接交付给FastCGI进程来执行,然后将得到的结果返回给浏览器。这种方式可以让HTTP服务器专一的处理静态请求,或者将动态脚本服务器的结果返回给客户端,这在很大程度上提高了真个应用系统的性能。

3、FastCGI的重要特点如下:

(1) HTTP服务器和动态脚本语言间通信的接口或者工具!
(2) 可把动态语言解析和HTTP服务器分离开来!
(3) Nginx、Apache、Lighttpd以及多数动态语言都支持FastCGI。
(4) FastCGI接口方式采用C/S结构,分为客户端(HTTP服务器)和服务端(动态语言解析服务器)。
(5) PHP动态语言服务端可以启动多个FastCGI的守护进程。
(6) http服务器通过FastCGI客户端和动态语言FastCGI服务端通信。

4、Nginx FastCGI的运行原理:

Nginx不支持对外部动态程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用。FastCGI接口在Linux下是socket,为了调用CGI程序,还需要一个FastCGI的wrapper(可以理解为用于启动另一个程序的程序),这个wrapper绑定在某个固定的socket上,如端口或文件socket。当Nginx将CGI请求发送给这个socket的时候,通过FastCGI接口,wrapper接收到请求,然后派生出一个新的线程,这个线程调用解释器或外部程序处理脚本来读取返回的数据;接着,wrapper再将返回的数据通过FastCGI接口,沿着固定的socket传递给Nginx;最后,Nginx将返回的数据发送给客户端,这就是Nginx+FastCGI的整个运作过程。


image.png
进程配置文件php-fpm.conf
解析器配置文件php.ini
安装PHP:

yum安装:简单、方便、高效。
我们用编译安装PHP

工作中如何选择PHP版本,取决于开发人员使用什么版本的PHP。

原则上Nginx和PHP用户要统一。

PHP的生产环境和测试环境使用的文件:
image.png

实践:

一、先装数据库:
1、创建用户
[root@web02 ~]# useradd mysql -s /sbin/nologin -M
[root@web02 ~]# id mysql
uid=1002(mysql) gid=1002(mysql) 组=1002(mysql)
2、上传软件到指定的目录
[root@web02 ~]# cd /server/tools/
[root@web02 /server/tools]# rz
[root@web02 /server/tools]# ls -lsh
总用量 616M
 615M -rw-r--r-- 1 root root  615M 5月   4 20:48 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
    0 drwxr-xr-x 9 www  www    204 4月  30 12:04 nginx-1.16.0
1012K -rw-r--r-- 1 root root 1009K 4月  23 21:58 nginx-1.16.0.tar.gz

[root@web02 /server/tools]# tar xf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
[root@web02 /server/tools]# mv mysql-5.7.26-linux-glibc2.12-x86_64 /application/mysql-5.7.26
[root@web02 /server/tools]# ln -s /application/mysql-5.7.26/  /application/mysql
相当于完成以下三步了。
./configure;make;make install

[root@web02 /server/tools]# ls /application/mysql/
bin  COPYING  docs  include  lib  man  README  share  support-files
3、配置配置文件/etc/my.cnf
[root@web02 /server/tools]# ls -l /etc/my.cnf #yum安装mariadb的默认的my.cnf
-rw-r--r--. 1 root root 570 8月  16 2018 /etc/my.cnf
rpm -e --nodeps mariadb-libs
#yum remove mariadb 卸载依赖包。

[root@web02 /server/tools]# rpm -e --nodeps mariadb-libs
[root@web02 /server/tools]# ls -l /etc/my.cnf
ls: 无法访问/etc/my.cnf: 没有那个文件或目录

[root@web02 /application/nginx/html/blog]# cat /etc/my.cnf 
[mysqld]
basedir = /application/mysql/
datadir = /application/mysql/data
socket = /tmp/mysql.sock
server_id = 1
port = 3306
log_error = /application/mysql/data/oldboy_mysql.err

[mysql]
socket = /tmp/mysql.sock
prompt = oldboy [\\d]>
解决依赖包:
yum install libmcrypt-devel -y 
yum install mhash -y
yum install mcrypt -y
4、初始化数据库
[root@web02 /server/tools]# rpm -qa mariadb-libs
[root@web02 /server/tools]# yum install libaio-devel -y

[root@web02 /server/tools]# mkdir -p /application/mysql/data
[root@web02 /server/tools]# chown -R mysql.mysql /application/mysql/

[root@web02 /server/tools]# /application/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/application/mysql/ --datadir=/application/mysql/data
5、配置启动服务
[root@web02 /application/mysql/support-files]# cat /etc/systemd/system/mysqld.service
[Unit]
Description=MySQL Server by oldboy
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000


###启动:
[root@web02 /application/mysql/support-files]# systemctl start mysqld
[root@web02 /application/mysql/support-files]# systemctl enable mysqld
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /etc/systemd/system/mysqld.service.
[root@web02 /application/mysql/support-files]# systemctl status mysqld
● mysqld.service - MySQL Server by oldboy
   Loaded: loaded (/etc/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 一 2019-05-06 09:41:25 CST; 10s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 7285 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─7285 /application/mysql/bin/mysqld --defaults-file=/etc/my.cnf

5月 06 09:41:25 web02 systemd[1]: Started MySQL Server by oldboy.
[root@web02 /application/mysql/support-files]# netstat -lntup|grep mysql
tcp6       0      0 :::3306                 :::*                    LISTEN      7285/mysqld         
[root@web02 /application/mysql/support-files]# ps -ef|grep mysql|grep -v grep
mysql      7285      1  0 09:41 ?        00:00:00 /application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
6、配置环境变量登录
[root@web02 /application/mysql/support-files]# echo 'export PATH=/application/mysql/bin:$PATH' >>/etc/profile
[root@web02 /application/mysql/support-files]# tail -1 /etc/profile
export PATH=/application/mysql/bin:$PATH
[root@web02 /application/mysql/support-files]# . /etc/profile
[root@web02 /application/mysql/support-files]# echo $PATH
/application/mysql/bin:/application/nginx/sbin:/application/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

[root@web02 /application/mysql/support-files]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
oldboy [(none)]>
成功登录。
oldboy [(none)]>quit
Bye
如果出错就看错误日志:
cat /application/mysql/data/oldboy_mysql.err 
7、修改密码
mysqladmin -u root password 'oldboy123'

重新登录:

交互式登录:
[root@web02 ~]# mysql -uroot -p
    Enter password:
非交互登录:
mysql -uroot -poldboy123

二、安装PHP:

1、安装PHP调用的库
yum install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel -y
yum install freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel libxslt-devel -y
cd /server/tools/
上传libiconv-1.16.tar.gz

tar zxf libiconv-1.16.tar.gz
cd libiconv-1.16
./configure --prefix=/application/libiconv
make
make install
cd ../

yum install libmcrypt-devel -y 
yum install mhash -y
yum install mcrypt -y
2、安装PHP
cd /server/tools/
tar xf php-7.3.5.tar.gz
cd php-7.3.5/

./configure \
--prefix=/application/php-7.3.5 \
--enable-mysqlnd  \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir=/application/libiconv \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--with-gd \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--enable-short-tags \
--enable-static \
--with-xsl \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-ftp \
--enable-opcache=no

make 
make install


[root@web02 /server/tools/php-7.3.5]# echo $?
0
将nginx的用户和PHP的用户统一:nginx
[root@web02 /server/tools/php-7.3.5]# useradd nginx -u 1111 -s /sbin/nologin -M
[root@web02 /server/tools/php-7.3.5]# id nginx
uid=1111(nginx) gid=1111(nginx) 组=1111(nginx)


[root@web02 /server/tools/php-7.3.5]# vim /application/nginx/conf/nginx.conf
worker_processes  1;
user  nginx nginx;
(编译的时候就改用nginx)


[root@web02 ~]# ln -s /application/php-7.3.5/ /application/php
[root@web02 ~]# ls /application/php/
bin  etc  include  lib  php  sbin  var
3、配置php.ini(PHP解析器配置文件)
[root@web02 /application/php]# cd /server/tools/php-7.3.5/
[root@web02 /server/tools/php-7.3.5]# ls php.ini-*
php.ini-development  php.ini-production

[root@web02 /server/tools/php-7.3.5]# cp php.ini-development /application/php/lib/php.ini
[root@web02 /server/tools/php-7.3.5]# ls -l /application/php/lib/php.ini
-rw-r--r-- 1 root root 71648 5月   6 11:51 /application/php/lib/php.ini
4、配置PHP FPM
[root@web02 /server/tools/php-7.3.5]# cd /application/php/etc/
[root@web02 /application/php/etc]# ls
pear.conf  php-fpm.conf.default  php-fpm.d
[root@web02 /application/php/etc]# cp php-fpm.conf.default php-fpm.conf
[root@web02 /application/php/etc]# cd php-fpm.d/
[root@web02 /application/php/etc/php-fpm.d]# ls
www.conf.default
[root@web02 /application/php/etc/php-fpm.d]# cp www.conf.default www.conf
[root@web02 /application/php/etc/php-fpm.d]# ls
www.conf  www.conf.default
5、启动PHP服务
[root@web02 /application/php/etc/php-fpm.d]# /application/php/sbin/php-fpm 
[root@web02 /application/php/etc/php-fpm.d]# netstat -lntup|grep php-fpm
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      12214/php-fpm: mast 
6、开机自启动
[root@web02 /application/php/etc/php-fpm.d]# tail -2 /etc/rc.local
/application/nginx/sbin/nginx
/application/php/sbin/php-fpm
7、配置nginx转发PHP请求
 location ~ .*\.(php|php5)?$ {
            root html/blog;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi.conf;
        }

[root@web02 /application/nginx/conf]# cat extra/03_blog.conf 
    server {
        listen       80;
        server_name  blog.etiantian.org;
        root   html/blog;
        location / {
            index  index.html index.htm;
        }
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }

    }
编写测试文件:
[root@web02 /application/nginx/conf]# echo "" > ../html/blog/test_info.php
[root@web02 /application/nginx/conf]# cat ../html/blog/test_info.php


[root@web02 /application/nginx/conf]# /application/php/bin/php /application/nginx/html/blog/test_info.php
成功的配置:
[root@web02 /application/nginx/conf/extra]# cat 03_blog.conf
server {
        listen       80;
        server_name  blog.etiantian.org;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
        location ~ .*\.(php|php5)?$ {
            root html/blog;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi.conf;
        }
    }
重启Nginx:
[root@web02 /application/nginx/conf]# nginx -t
nginx: the configuration file /application/nginx-1.16.0//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.16.0//conf/nginx.conf test is successful
[root@web02 /application/nginx/conf]# nginx -s reload
8、测试PHP连接mysql
[root@web02 /application/nginx/html/blog]# cat  /application/nginx/html/blog/test_mysql.php 


[root@web02 /application/nginx/html/blog]# /application/php/bin/php /application/nginx/html/blog/test_mysql.php 
mysql successful by oldboy.

你可能感兴趣的:(2019-05-06day46)