zoukankan      html  css  js  c++  java
  • LNMP架构

    一、预备环境

    关闭防火墙,关闭安全机制

    [root@localhost ~]# systemctl stop firewalld
    [root@localhost ~]# iptables -F
    [root@localhost ~]# setenforce 0

    二、搭建Nginx服务

    1、支持的软件包pcre-devel、zlib-devel、openssl-devel(支持加密)、gcc、gcc-c++、make

    devel包主要是供开发用,包含普通包,devel包中包含头文件和链接库,如果是需要动态链接库的话,两种包都需要有。

    [root@localhost ~]# yum -y install pcre-devel zlib-devel openssl-devel

    2、创建nginx用户和组

    [root@localhost ~]# useradd -M -s /sbin/nologin nginx

    3、解压安装包并指定位置

    [root@localhost ~]# tar -xf nginx-1.16.0.tar.gz -C /usr/src/

    4、编译安装

    [root@localhost ~]# cd /usr/src/nginx-1.16.0/ 
    [root@localhost nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module && make && make install
    --prefix //设定nginx的安装目录
    --user和--group //指定nginx运行用户和组
    --with-http_stub_status_module //启用模块支持状态统计
    --with-http_ssl_module //启用ssl模块
    --with-http_flv_module //提供寻求内存使用基于时间的偏移量文件

    5、为主程序nginx创建链接文件

    [root@localhost ~]# ls /usr/local/nginx/
      logs        sbin       conf              html          
    [root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
    [root@localhost ~]# ll /usr/local/sbin/nginx

    6、nginx的运行控制

    nginx -t    检测配置文件的语法

    [root@localhost ~]# 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 ~]# nginx
    [root@localhost ~]# netstat -anpt | grep :80
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      13134/nginx: master 

    重启服务

    [root@localhost ~]# killall -HUP nginx

    三、nginx配置文件(nginx.conf)分析

    在nginx服务器的主配置文件nginx.conf中,包括全局配置、I/O时间配置、HTTP配置三大内容

    配置格式:关键字 值;    //末尾以分号表示结束,以#开始的部分表示注释

    1、全局配置

    常用配置项:

    user nginx [nginx];    //nginx的程序用户及程序组,若编译时未指定则默认为nobody

    worker_processes 2;     //指定nginx要指定的进程数量,每个进程要耗费10M-20M内存,一般与CPU核心数相等

    worker_cpu_affinity 01 10;   //双核CPU(0001 0010 0100 1000;四核) CPU的分配问题

    worker_rlimit_nofile 102400;    //一个进程可以打开的最多文件数目,数值与ulimit -n(系统最多打开的文件数量)保持一致就好

    error_log logs/error.log;    //错误日志的记录级别

    pid logs/nginx.pid;      //指定PID文件的位置

    2、I/O事件配置

    使用"events{ }"界定标记,指定nginx进程的I/O响应模式,每个进程的连接数等

    events {
    
          use  epoll;      //用来设定nginx的工作模式
          worker_connections 4096//用于定义nginx的最大连接数,默认1024
    
    }

    3、HTTP配置

    用"http{ }"界定,设置访问日志、HTTP端口、网页目录、默认字符集、连接保持、虚拟web主机、PHP解析,server{ }配置段设置特定网络

    http {
        include       mime.types;
    default_type  application/octet-stream;
    
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
    access_log  logs/access.log  main;     //客户端访问日志记录位置
    sendfile        on;                    //支持文件发送(上传下载)
    keepalive_timeout  65;                 //连接保持的时间
    
        server {                                //web服务的监听配置
            listen       80;                    //定义虚拟主机的服务器接口(192.168.200.111:80)
    server_name  www.crushlinux.com;            //网站名称(FQDN)
            charset utf-8;                       //网页的默认字符集
    
            location / {                        //跟目录配置
                root   html;                    //网站根目录的位置安装位置的html中
                index  index.html index.htm;    //默认首页(索引页)
            }
    
    error_page   500 502 503 504  /50x.html;    //内部错误的反馈页面
            location = /50x.html {               //错误页面配置
                root   html;
            }
        }
    }

    4、状态统计模块

    [root@nginx~]# vim /usr/local/nginx/conf/nginx.conf
    //在http中写
    location ~ /status {
    stub_status on;        //打开状态统计功能
    access_log off;        //关闭此位置的日志记录
    }
    [root@nginxconf]#killall -HUP nginx     //重启nginx服务

    四、创建mysql数据库

    1、查看所需的软件

    [root@localhost ~]# rpm -q ncurses-devel   //检测安装包

    2、若没有安装

    [root@localhost ~]# yum -y install ncurses-devel  //用yum安装软件包

    3、新建一个mysql用户和组

    [root@localhost ~]# useradd -M -s /sbin/nologin mysql    //创建一个用户组

    4、安装cmake

    [root@localhost ~]# yum -y install cmake    //安装编译命令

    5、编译安装mysql数据库

    [root@localhost ~]# tar -xf mysql-5.7.24.tar.gz -C /usr/src/    //解压Msyql软件包并指定解压位置
    [root@localhost ~]# cd /usr/src/mysql-5.7.24/      //进入解压后路径内
    //编译安装
    [root@localhost mysql-5.7.24]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc && make && make install 

    在执行时如果报一个boost软件的错误,进行如下操作

    [root@localhost ~]# mkdir /usr/local/boost/       //创建一个文件夹boost
    [root@localhost ~]# cd /usr/local/boost/            //进入文件夹
    [root@localhost boost]# tar -xf boost_1_59_0.tar.gz         //在当前位置解压boost软件包
    [root@localhost boost]# cd /usr/src/mysql-5.7.24/          //回到Mysql安装路径
    [root@localhost mysql-5.7.24]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc -DWITH_BOOST=/usr/local/boost/ && make && make install   

    6、修改用户和组

    [root@localhost mysql-5.7.24]# chown -R mysql:mysql /usr/local/mysql/  

    7、建立配置文件

    [root@localhost ~]# vim /etc/my.cnf           //修改Mysql主配置文件
    [mysqld]
    datadir=/usr/local/mysql/data
    socket=/tmp/mysql.sock
    
    [mysqld_safe]
    log-error=/usr/local/mysql/data/mysql.log
    pid-file=/usr/local/mysql/data/mysql.pid

    8、初始化数据库

    [root@localhost ~]# /usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize   //初始化并留下密码(后面会用密码要记住)     
    
    2019-09-10T16:36:34.384265Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
    2019-09-10T16:36:34.631107Z 0 [Warning] InnoDB: New log files created, LSN=45790
    2019-09-10T16:36:34.673027Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
    2019-09-10T16:36:34.733541Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 270a4236-d3e9-11e9-96bf-000c299b5cf0.
    2019-09-10T16:36:34.734738Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
    2019-09-10T16:36:34.735658Z 1 [Note] A temporary password is generated for root@localhost: yR1jAfi)xCd%

    9、修改文件、添加变量

    [root@localhost ~]# vim /etc/profile      //开机启动加载文件
    PATH=$PATH:/usr/local/mysql/bin         //设置变量

    [root@localhost ~]# source /etc/profile    //重新加载文件

    10、复制服务脚本

    [root@localhost ~]# cp /usr/src/mysql-5.7.24/support-files/mysql.server /etc/init.d/mysqld 

    11、加权限,开启数据库

    [root@localhost ~]# chmod +x /etc/init.d/mysqld      //给脚本执行权限
    [root@localhost ~]# /etc/init.d/mysqld start          //开启数据库
    
    [root@localhost ~]# /etc/init.d/mysqld restart        //重新启动服务

    12、为数据库修改密码

    [root@localhost ~]# mysql -u root -p'yR1jAfi)xCd%'     //使用原始密码进入数据库
    [root@localhost ~]# mysqladmin -u root -p'yR1jAfi)xCd%' password '123123'   //更改数据库密码
    [root@localhost ~]# mysql -uroot -p123123            //用新密码进入数据库

    五、PHP安装

    1、安装支持软件

    [root@localhost ~]# yum -y install gd libxml2-devel libjpeg-devel libpng-devel 

    2、编译安装PHP

    [root@localhost ~]# tar -xf php-5.6.39.tar.gz -C /usr/src/      //解压软件包
    [root@localhost ~]# cd /usr/src/php-5.6.39/      //进入解压后路径
    [root@localhost php-5.6.39]# ./configure --prefix=/usr/local/php5 --with-gd --with-zlib --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-config-file-path=/usr/local/php5 --enable-mbstring --enable-fpm --with-jpeg-dir=/usr/lib && make && make install  //编译安装

    3、安装后优化

    [root@localhost php-5.6.39]# cp -p /usr/src/php-5.6.39/php.ini-production /usr/local/php5/php.ini   //复制配置文件
    [root@localhost php-5.6.39]# ln -s /usr/local/php5/sbin/* /usr/local/sbin/          //链接命令文件
    [root@localhost php-5.6.39]# ln -s /usr/local/php5/bin/* /usr/local/sbin/           //链接命令文件

    4、安装ZendGuardLoader(PHP的优化模块)

    [root@localhost ~]# tar -xf zend-loader-php5.6-linux-x86_64_update1.tar.gz -C /usr/src/      //安装优化软件包
    [root@localhost ~]# cd /usr/src/zend-loader-php5.6-linux-x86_64/          //进入优化软件包路径
    [root@localhost zend-loader-php5.6-linux-x86_64]# cp ZendGuardLoader.so /usr/local/php5/lib/php/      //复制文件
    [root@localhost ~]# echo -e "zend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so
    zend_loader.enable=1" >> /usr/local/php5/php.ini   //在文件最后添加内容
    [root@localhost ~]# tail -2 /usr/local/php5/php.ini   //查看添加内容详情
    zend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so
    zend_loader.enable=1

    5、启用php-fpm进程

    [root@localhost ~]# useradd -M -s /sbin/nologin php     //创建一个所属用户组
    [root@localhost ~]# cd /usr/local/php5/etc/                     
    [root@localhost etc]# cp -p php-fpm.conf.default php-fpm.conf   //修改文件名
    [root@localhost etc]# vim php-fpm.conf      //修改主配置文件
    25 pid = run/php-fpm.pid
    149 user = php
    150 group = php
    241 pm.max_children = 50
    246 pm.start_servers = 20
    251 pm.min_spare_servers = 5
    256 pm.max_spare_servers = 35
    [root@localhost etc]# php-fpm        //启动优化服务
    [root@localhost etc]# netstat -anpt | grep php-fpm   //查看服务是否开启

    6、配置nginx支持PHP解析

    [root@ns1 ~]# cat /usr/local/nginx/conf/nginx.conf      //修改nginx主配置文件
    
    user  nginx nginx;
    worker_processes  1;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    error_log  logs/error.log  info;
    
    pid        logs/nginx.pid;
    
    
    events {
     use epoll;   
     worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
        #access_log  logs/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        #gzip  on;
    
        server {
            listen       80;
           server_name  localhost;
    
            charset utf-8;
    
    
            location / {
                root   html;
                index  index.html index.htm    index.php;
            }
    
            location ~ .php$ {
                root           html;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                include        fastcgi.conf;
            }
    }
    }

    7、检测配置的语法,重启

    [root@localhost ~]# 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@nginxetc]#killall -HUP nginx   //重启

    8、PHP页面访问测试

    [root@localhost ~]# vim /usr/local/nginx/html/mysql.php
    <?php
    $link=mysql_connect('localhost','root','123123');
    if($link)echo"<h1>succeccful!</h1>";
    mysql_close();
    ?>
    [root@localhost ~]# vim /usr/local/nginx/html/php.php 
    <?php
    phpinfo();
    ?>
  • 相关阅读:
    struts2简介
    HDU 2842 Chinese Rings(矩阵高速功率+递归)
    Cocos2d-X中国象棋的发展《五岁以下儿童》摆棋
    【Python注意事项】如何理解python中间generator functions和yield表情
    [CSS] Design for Mobile First with Tachyons
    [Angular] Configurable NgModules
    [Angular] Using useExisting provider
    [Angular] Providers and useFactory
    [Angular] Using InjectionToken
    [Angular] Test Directive
  • 原文地址:https://www.cnblogs.com/tanxiaojuncom/p/11508817.html
Copyright © 2011-2022 走看看