• LNMP环境搭建


    php安装

    • 安装php压缩包 wget http://cn2.php.net/distributions/php-5.6.30.tar.gz

    • tar -zxvf

    • yum install -y install gcc libxml2 libxml2-devel openssl openssl-devel libcurl curl-devel libjpeg-devel libpng libpng-devel freetype-devel epel-release

    • yum install -y php-mcrypt libmcrypt libmcrypt-devel

    • useradd -s /sbin/nologin php-fpm 添加php用户

    • cd 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 指定安装目录 为编译做准备

    • make && make install 编译安装

    • cp /usr/local/src/php-5.6.30/php.ini-production /usr/local/php-fpm/etc/php.ini

    • 编辑配置文件 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
        include = etc/php-fpm.d/*.conf
        [www]
        user = php-fpm
        group = php-fpm
        listen = /tmp/php-fcgi.sock
        listen.mode = 666
        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
        
    • /usr/local/php-fpm/sbin/php-fpm -t

    image-20210310150324559

    • cd /usr/local/src/php-5.6.30
    • cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
    • chmod 755 /etc/init.d/php-fpm 赋予权限
    • chkconfig --add php-fpm 将php添加到服务列表
    • chkconfig php-fpm on 开启服务
    • cd /usr/local/php-fpm/
    • service php-fpm start 开启进程

    image-20210310150803880

    安装Nginx

    • cd /usr/local/src/

    • wget http://nginx.org/download/nginx-1.16.1.tar.gz 获取压缩包

    • tar -axvf

    • cd nginx-1.16.1/

    • ./configure --prefix=/usr/local/nginx 指定安装目录 为编译做准备

    • make && make install

    • vi /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
        
    • chmod 755 /etc//init.d/nginx

    • chkconfig --add nginx将nginx添加到服务列表

    image-20210310152041450

    • chkconfig nginx on

    • > /usr/local/nginx/conf/nginx.conf

    • vim /usr/local/nginx/conf/nginx.conf

      user  nobody;
    worker_processes  2;
    error_log /usr/local/nginx/logs/nginx_error.log crit;
    pid /usr/local/nginx/logs/nginx.pid;
    events {
        use epoll;
        worker_connections  6000;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        server_names_hash_max_size 4096;
        server_names_hash_bucket_size 3526;
        log_format combined_relip '$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/clent_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_http_version 1.1;
        gzip_comp_level   5;
        gzip_types        text/plain application/x-javascript text/css application/xml text/htm;
    
        server 
    {
            listen       8080;
            server_name  localhost;
            client_max_body_size 100M;
            index index.html index.htm index.php;
            root /usr/local/nginx/html;
    
         location ~ .php$
         {
             include fastcgi_params;
             fastcgi_pass 127.0.0.1:9000;
             fastcgi_index index.php;
             fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;           
          }
           }
        include vhost/*.conf;
    }
    
    
    • /usr/local/nginx/sbin/nginx -t

    image-20210310152532194

    • service nginx start

    • 创建2.php文件 查看解析是否正确 vi /usr/local/nginx/html/2.php

      • <?php
        echo "test php scripts";
        ?>
        
      • curl localhost/2.php

      • test php scripts //证明解析成功

    Nginx配置

    默认虚拟主机

    • 首先修改配置文件 vi /usr/local/nginx/conf/nginx.conf

      • 在最后一行添加include vhost/*.conf;
      • 意思就是/usr/local/nginx/conf/host下面的所有以.conf结尾的文件都会被加载
    • mkdir /usr/local/nginx/conf/vhost创建vhost目录

    • cd /usr/local/nginx/conf/vhost

    • vim default.conf

      • server
        {
          listen 80 default_server;
          server_name aaa.com;
          index index.html index.htm index.php;
          root /data/nginx/default;
        }
        
        
      • 编辑为默认服务

    • /usr/local/nginx/sbin/nginx -s reload 重启nginx

    • mkdir -p /data/nginx/default

    • echo "default_server" > /data/nginx/default/index.html

    • 测试 curl -x127.0.0.1:80 aaa.com

    image-20210310154228334

    • curl -x127.0.0.1:80 1212.com 访问其他域名也可以得到响应信息 因为aaa.com是默认服务

    • 如果修改为不是默认的为:

    image-20210310154633743

    image-20210310154654067

    用户认证

    • 创建一个虚拟主机 cd /usr/local/nginx/conf/vhost

    • vi test.com.conf

      • server
        {
            listen 80;
            server_name test.com;
            index index.html index.htm index.php;
            root /data/nginx/test.com;
            location / {
        		auth_basic "Auth";
            	//打开认证
        		auth_basic_user_file /usr/local/nginx/conf/htpasswd;
            	//指定用户密码文件
            }
        }
        
    • yum install -y httpd

    • htpasswd -c /usr/local/nginx/conf/htpasswd dongying 创建密码和用户

    image-20210310155146108

    • /usr/local/nginx/sbin/nginx -s reload
    • mkdir /data/nginx/test.com 创建 test.com文件
    • echo "test.com" > /data/nginx/test.com/index.html 写入文件内容
    • curl -udongying:1 -x127.0.0.1:80 test.com 测试

    image-20210310162134708

    • curl -I -x127.0.0.1:80 test.com 报 401错误

    • systemctl stop firewalld 关闭防火墙

    • 本地浏览器测试:

    image-20210310163147203

    域名重定向

    • nginx地址重写
    server
    {
        listen 80;
    server_name test.com test1.com test2.com;
        index index.html index.htm index.php;
        root /data/nginx/test.com;
    
        if ($host != 'test.com' ){
           rewrite ^(.*)$ http://test.com/$1 permanent;
        }
        access_log /tmp/1.log combined_realip;
    }
    
    
    • parmanent表示永久重定向

    image-20210311100916970

    • 测试

    image-20210310165616475

    日志搭建

    • grep -A2 log_format /usr/local/nginx/conf/nginx.conf 抓取日志格式

      • //combined_realip为日志格式名字,$remote_addr为网站的用户的出口IP;

        //$http_x_forwarded_for 为代理服务器的IP,如果使用了代理,则会记录IP

        //$time_local为当前时间;$host为主机名;$request_uri为访问的URL地址

        //$status为状态码,$http_referer为referer地址,$http_user_agent为user_agent

    image-20210311101031617

    • vi /usr/local/nginx/conf/vhost/test.com.conf

      • server
        {
                listen 80;
                server_name test.com;
                index index.html index.htm index.php;
                root /data/nginx/test.com;
                if ($host != 'test.com' ){
                        rewrite ^(.*)$ http://test.com/$1 permanent;
                }
                access_log /tmp/1.log combined_realip;
        }
        //使用access_log来指定日志的存储路径,最后面为日志的格式名字
        
    • /usr/local/nginx/sbin/nginx -t

    • /usr/local/nginx/sbin/nginx -s reload

    • 测试 curl -x127.0.0.1:80 test.com/111

    image-20210310170258428

    • cat /tmp/1.log

    image-20210310170310682

  • 相关阅读:
    ubuntu16.04以及各个版本镜像网址汇总
    linux0.11的进程1的创建和执行
    linux0.11的0号进程详解
    Linux命令
    Vim命令
    CentOS7静态IP设置
    Fiddler常用设置
    Python日期计算
    Python完全平方数
    pip
  • 原文地址:https://www.cnblogs.com/zgrey/p/14516193.html
Copyright © 2020-2023  润新知