• Nginx网站服务-LNMP


    Nginx网站服务-编译安装LNMP

    1.简介

    1.1概述

    ■ 一款高性能、轻量级Web服务软件

    ● 稳定性高

    ● 系统资源消耗低

    ● 对HTTP并发连接的处理能力高

    ◆ 单台物理服务器可支持30 000 ~ 50 000个并发请求

    NG并发连接能力由2个因素的影响:

    ① CPU的个数

    ② 本地物理服务器系统的最大文件打开数

    1.2Nginx和Apache的差异

    ● Nginx是一个基于事件的Web服务器,Apache是一个基于流程的服务器
    ● Nginx所有请求都由一个线程处理,Apache单个线程处理单个请求
    ● Nginx避免子进程的概念,Apache是基于子进程的
    ● Nginx在内存消耗和连接方面更好,Apache在内存消耗和连接方面一般
    ● Nginx的性能和可伸缩性不依赖于硬件,Apache依赖于CPU和内存等硬件
    ● Nginx支持热部署,Apache不支持热部署
    ● Nginx对于静态文件处理具有更高效率,Apache相对一般
    ● Nginx在反向代理场景具有明显优势,Apache相对一般

    2.编译安装Ngnix服务

    #关闭防火墙
    [root@liwenbin ~]# systemctl stop firewalld
    [root@liwenbin ~]# systemctl disable firewalld
    [root@liwenbin ~]# setenforce 0
    
    #将安装Ngnix所需软件包传到/opt目录下
    [root@liwenbin opt]# ls
    nginx-1.12.2.tar.gz  rh
    
    #安装依赖包
    #nginx的配置及运行需要pcre、zlib等软件包的支持,因此需要安装这些软件的开发包,以便提供相应的库和头文件
    [root@liwenbin opt]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    #创建运行用户、组(Nginx 服务程序默认以 nobody 身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限)
    [root@liwenbin opt]# useradd -M -s /sbin/nologin nginx
    
    • 1
    • 2

    编译安装Nginx

    [root@liwenbin opt]# tar zxvf nginx-1.12.2.tar.gz -C /opt/
    [root@liwenbin opt]# cd nginx-1.12.2
    [root@liwenbin nginx-1.12.2]# ./configure \
    --prefix=/usr/local/nginx \							#指定nginx的安装路径
    --user=nginx \										#指定用户名
    --group=nginx \										#指定组名
    --with-http_stub_status_module						#启用http_stub_status_module 模块以支持状态统计 
    [root@liwenbin nginx-1.12.2]# make && make install
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    #让系统识别nginx的操作命令
    [root@liwenbin nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
    
    • 1
    • 2
    [root@liwenbin nginx-1.12.2]# 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@liwenbin nginx-1.12.2]# nginx                 #启动
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在网页中输入ip地址,192.168.131.129查看结果

    请添加图片描述

    #查看index.html文件
    [root@liwenbin nginx-1.12.2]# cd html
    [root@liwenbin html]# cat index.html
    
    
    
    Welcome to nginx!
    
    
    
    

    Welcome to nginx!

    If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

    For online documentation and support please refer to nginx.org.
    Commercial support is available at nginx.com.

    Thank you for using nginx.

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    [root@liwenbin html]# vim /lib/systemd/system/nginx.service     #添加 Nginx 系统服务
    [Unit]
    Description=nginx
    After=network.target
    [Service]
    Type=forking
    PIDFile=/usr/local/nginx/logs/nginx.pid
    ExecStart=/usr/local/nginx/sbin/nginx
    ExecReload=/bin/kill -s HUP $MAINPID
    ExecStop=/bin/kill -s QUIT $MAINPID
    PrivateTmp=true
    [Install]
    WantedBy=multi-user.target
    
    [root@liwenbin html]# chmod 754 /lib/systemd/system/nginx.service
    [root@liwenbin html]# systemctl start nginx.service
    [root@liwenbin html]# systemctl enable nginx.service
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    3.新版本升级

    #将新的Ngnix软件包传到/opt目录下进行升级操作
    [root@liwenbin opt]# ls
    nginx-1.12.2  nginx-1.12.2.tar.gz  nginx-1.23.0.tar.gz  rh
    
    • 1
    • 2
    • 3
    [root@liwenbin ~]# vim /etc/init.d/nginx         #创建/etc/init.d/nginx文件,输入以下内容
    #!/bin/bash
    #chkconfig: 35 99 20
    #description:Nginx Service Control Script
    COM="/usr/local/nginx/sbin/nginx" 
    PID="/usr/local/nginx/logs/nginx.pid"
    case "$1" in
    start)
      $COM
    ;;
    
    stop)
      kill -s QUIT $(cat $PID)
    ;;
    
    restart)
      $0 stop
      $0 start
    ;;
    
    reload)
      kill -s HUP $(cat $PID)
    ;;
    
    *)
    echo "Usage: $0 {start|stop|restart|reload}"
    exit 1
    
    esac
    exit 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    [root@liwenbin ~]# exit         #xshll连接的话退出重新连接,如果还是报错,可以重启
    
    [root@liwenbin ~]# chmod +x /etc/init.d/nginx
    [root@liwenbin ~]# chkconfig --add nginx
    [root@liwenbin ~]# systemctl stop nginx
    [root@liwenbin ~]# systemctl start nginx
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    [root@liwenbin opt]# tar zxvf nginx-1.23.0.tar.gz -C /opt/
    [root@liwenbin opt]# cd nginx-1.23.0
    [root@liwenbin nginx-1.23.0]# yum install -y openssl openssl-devel           #需要提前安装ssl服务,否则后面一步会报错
    [root@liwenbin nginx-1.23.0]# ./configure \
    --prefix=/usr/local/nginx \
    --user=nginx \
    --group=nginx \
    --with-http_stub_status_module \
    --with-http_ssl_module
    [root@liwenbin nginx-1.23.0]# make
    
    [root@liwenbin nginx-1.23.0]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_old     #备份
    [root@liwenbin nginx-1.23.0]# cp objs/nginx /usr/local/nginx/sbin/nginx
    [root@liwenbin nginx-1.23.0]# systemctl restart nginx
    
    [root@liwenbin nginx-1.23.0]# nginx -V
    nginx version: nginx/1.23.0
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
    built with OpenSSL 1.0.2k-fips  26 Jan 2017
    TLS SNI support enabled
    configure arguments: --prefix=/usr/local/nginx --user=nginx --group=ngnix --with-http_stub_status_module --with-http_ssl_module
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    再到网页输入 192.168.36.129 查看结果

    请添加图片描述

    4.安装MYSQL

    详见上一篇博客内容

    源码编译安装LAMP_LEE_九月的博客-CSDN博客

    5.安装PHP

    [root@liwenbin ~]# yum -y install \
    > gd \
    > libjpeg libjpeg-devel \
    > libpng libpng-devel \
    > freetype freetype-devel \
    > libxml2 libxml2-devel \
    > zlib zlib-devel \
    > curl curl-devel \
    > openssl openssl-devel
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    [root@liwenbin ~]# cd /opt
    [root@liwenbin opt]# tar zxvf php-7.1.10.tar.gz
    [root@liwenbin opt]# cd php-7.1.10
    [root@liwenbin php-7.1.10]# ./configure \
    > --prefix=/usr/local/php \
    > --with-mysql-sock=/usr/local/mysql/mysql.sock \
    > --with-mysqli \
    > --with-zlib \
    > --with-curl \
    > --with-gd \
    > --with-jpeg-dir \
    > --with-png-dir \
    > --with-freetype-dir \
    > --with-openssl \
    > --enable-fpm \
    > --enable-mbstring \
    > --enable-xml \
    > --enable-session \
    > --enable-ftp \
    > --enable-pdo \
    > --enable-tokenizer \
    > --enable-zip
    
    [root@liwenbin php-7.1.10]# make && make install
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    [root@liwenbin php-7.1.10]# cp /opt/php-7.1.10/php.ini-development /usr/local/php/lib/php.ini
    [root@liwenbin php-7.1.10]# vim /usr/local/php/lib/php.ini
    mysqli.default_socket = /usr/local/mysql/mysql.sock    #修改1170行
    date.timezone = Asia/Shanghai                          #修改939行,取消注释
    
    [root@liwenbin php-7.1.10]# php -m 			           #验证安装的模块
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    [root@liwenbin php-7.1.10]# cd /usr/local/php/etc/
    [root@liwenbin etc]# cp php-fpm.conf.default php-fpm.conf
    [root@liwenbin etc]# vim php-fpm.conf
    pid = run/php-fpm.pid        #17行取消注释
    
    • 1
    • 2
    • 3
    • 4
    #调整扩展配置文件
    [root@liwenbin etc]# cd /usr/local/php/etc/php-fpm.d/
    [root@liwenbin php-fpm.d]# ls
    www.conf.default
    [root@liwenbin php-fpm.d]# cp www.conf.default www.conf
    
    • 1
    • 2
    • 3
    • 4
    • 5
    #启动php-fpm
    [root@liwenbin php-fpm.d]# /usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
    [root@liwenbin php-fpm.d]# netstat -anpt | grep 9000
    tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      34447/php-fpm: mast 
    
    [root@liwenbin php-fpm.d]# cd /opt/php-7.1.10/sapi/fpm
    [root@liwenbin fpm]# cp php-fpm.service /usr/lib/systemd/system/php-fpm.service
    [root@liwenbin fpm]# systemctl restart php-fpm.service
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    [root@liwenbin fpm]# vim /usr/local/nginx/conf/nginx.conf
    #65行开始取消注释并修改,如下
    location ~ \.php$ {
    	root           html;
    	fastcgi_pass   127.0.0.1:9000;
    	fastcgi_index  index.php;
    	fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;	
    	#将 /scripts 修改为nginx的工作目录
    	
       #fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;  
       #$document_root 代表当前请求在root指令中指定的值
       
    	include        fastcgi_params;
        }
        
    [root@liwenbin fpm]# systemctl restart nginx.service
    #此处我有报错,突然找不到nginx服务,发现/lib/systemd/system/nginx.service文件丢失,重新添加一遍即可
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    [root@liwenbin fpm]# vim /usr/local/nginx/html/index.php
    
    
    • 1
    • 2
    • 3
    • 4

    网页输入 192.168.36.129/index.php

    请添加图片描述

    #验证数据库工作是否正常
    [root@liwenbin ~]# mysql -u root -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 6
    Server version: 5.7.17 Source distribution
    
    Copyright (c) 2000, 2016, 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.
    
    mysql> CREATE DATABASE bbs;
    Query OK, 1 row affected (0.01 sec)
    
    mysql> GRANT all ON bbs.* TO 'bbsuser'@'%' IDENTIFIED BY 'admin123';
    Query OK, 0 rows affected, 1 warning (0.01 sec)
    
    mysql> GRANT all ON bbs.* TO 'bbsuser'@'localhost' IDENTIFIED BY 'admin123';
    Query OK, 0 rows affected, 2 warnings (0.00 sec)
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> exit
    Bye
    
    
    [root@liwenbin ~]# vim /usr/local/nginx/html/index.php
    #替换原来的测试页内容
    Success!!";
    else echo "Fail!!";
    ?>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    请添加图片描述

    6.安装论坛

    [root@liwenbin ~]# cd /opt
    [root@liwenbin opt]# unzip Discuz_X3.4_SC_UTF8.zip  -d /opt/dis
    [root@liwenbin opt]# cd /opt/dis/dir_SC_UTF8/
    [root@liwenbin dir_SC_UTF8]# cp -r upload/ /usr/local/nginx/html/bbs/
    [root@liwenbin dir_SC_UTF8]# cd /usr/local/nginx/html/bbs/
    [root@liwenbin bbs]# chown -R 777 ./config/
    [root@liwenbin bbs]# chown -R 777 ./data/
    [root@liwenbin bbs]# chown -R 777 ./uc_client/
    [root@liwenbin bbs]# chown -R 777 ./uc_server/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    网页输入 192.168.36.129/bbs/install/index.php

    安装论坛

    请添加图片描述

    安装完成后,网页重新输入 192.168.36.129/bbs/index.php
    请添加图片描述

  • 相关阅读:
    PX4实战之旅(五):利用T265实现室内定点飞行
    基于VSR-GUI的视频去字幕水印
    基于JavaWeb的食品团购网的设计与实现
    logging 彩色日志 封装类(直接使用即可)
    Crystal Ball—甲骨文水晶球风险管理软件(概念以及实战——中级案例篇)
    语音人工智能的简单介绍
    数据挖掘与分析应用:数据挖掘解决什么?流程是?Python数据挖掘的三方库,环境依赖准备
    Centos7 docker 容器内root身份应用自启动 & /usr/sbin/init 问题
    pytest多进程/多线程执行测试用例
    无人直播间带货还能做吗?
  • 原文地址:https://blog.csdn.net/liwenbin19920922/article/details/125900326