Nginx网站服务

本章节主要目的是配置Nginx成为一个网站server

1、安装Nginx

YUM安装或者编译安装均可以

2、调整配置文件

在Nginx服务器的主配置文件/usr/local/nginx/conf/nginx.conf中,包括全局配置、 I/O事件配置和HTTP配置这三大块内容,配置语句的格式为”指令 值;”(末尾以分号表示结束),以“#”开始的部分表示注释。

2.1 全局配置

由各种配置语句组成,不使用特定的界定标记。全局配置部分包括Nginx服务的运行用户、工作进程数、错误日志、PID存放位置等基本设置。

  1. user nginx nginx; #运行用户和组
  2. worker_processes auto; #工作进程数量
  3. worker_cpu_affinity auto; #工作进程自动绑定到特定CPU
  4. worker_rlimit_nofile 65535; #工作进程打开的最大文件数
  5. error_log /var/log/nginx/error.log warn; #错误日志位置
  6. pid /var/run/nginx.pid; #PID文件位置

2.2 I/O事件配置

使用“events { }” 界定标记,用来指定Nginx进程的I/O响应模型、每个进程的连接数等设置。对于2.6及以上版本的内核,建议使用epoll模型以提高性能;每个进程的连接数应根据实际需要来定,一般在10000以下(默认为1024)。

  1. events {
  2. use epoll; #使用epoll模型
  3. worker_connections 8192; #每进程处理8192个连接
  4. }

若工作进程数为4,每个进程处理8192个连接,则允许Nginx正常提供服务的连接数已超过3万个(8192×4=32768),当然具体还要看服务器硬件、网络带宽等物理条件的性能表现。

2.3 HTTP配置

使用“http { }”界定标记,包括访问日志、HTTP端口、网页目录、默认字符集、连接保持,以及后面要讲到的虚拟Web主机、PHP解析等一系列设置,其中大部分配置语句都包含在子界定标记“server{ }”内。

  1. http {
  2. include mime.types;
  3. default_type application/octet-stream;
  4. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  5. '$status $body_bytes_sent "$http_referer" '
  6. '"$http_user_agent" "$http_x_forwarded_for"';
  7. access_log logs/access.log main; #访问日志位置
  8. sendfile on; #静态服务器时,开启大大提升性能
  9. #tcp_nopush on;
  10. #keepalive_timeout 0;
  11. keepalive_timeout 65; #连接保持超时时间
  12. #gzip on;
  13. server {
  14. listen 80; #侦听地址
  15. server_name www.shengzhe.com; #网站域名
  16. charset utf-8; #网页的默认字符集
  17. location / { #根目录配置
  18. root html; #根目录位置
  19. index index.html index.htm; #默认首页
  20. }
  21. error_page 500 502 503 504 /50x.html; #内部错误的反馈页面
  22. location = /50x.html { #错误页面的配置
  23. root html;
  24. }
  25. }
  26. }

上述配置中,listen语句允许同时限定IP地址,采用“IP地址:端口”形式; root 语句用来设置特定访问位置(如“location /”表示根目录)的网页文档路径,默认为 Nginx 安装目录下的 html/子目录,根据需要可改为/var/www/html等其他路径。


3、访问状态统计

Nginx内置了HTTP_STUB_STATUS状态统计模块,用来反馈当前的Web访问情况,
配置编译参数时可添加–with-http_stub_status_module来启用此模块支持, 可以使用命令/usr/local/nginx/sbin/nginx –V 查看已安装的Nginx是否包含 HTTP_STUB_STATUS模块。

要使用Nginx的状态统计功能,除了启用内建模块以外,还需要修改nginx.conf配置文件,指定访问位置并添加stub_status配置代码。

  1. server {
  2. listen 80;
  3. server_name www.shengzhe.com;
  4. charset utf-8;
  5. #access_log logs/host.access.log main;
  6. location / {
  7. root html;
  8. index index.html index.htm;
  9. }
  10. error_page 500 502 503 504 /50x.html;
  11. location = /50x.html {
  12. root html;
  13. }
  14. location /status { #访问位置为/status
  15. stub_status; #打开状态统计功能
  16. access_log off; #关闭此位置的日志记录
  17. }
  18. }

新的配置生效以后,在浏览器中访问Nginx服务器的/status网站位置,可以看到当前的状态统计信息:

LNMP环境 - 图1


4、Nginx访问控制

4.1 基于授权的访问控制

Nginx可以实现基于用户授权的访问控制,当客户端想要访问相应网站或者目录时,要求用户输入用户名和密码才能正常访问,配置步骤如下:

  1. 生成用户密码认证文件。
  2. 修改主配置文件相对应目录,添加认证配置项。
  3. 重启服务,访问测试
  • 使用htpasswd生成用户认证文件,如果没有该命令,可使用yum安装httpd-tools软件包。
  1. [root@localhost ~]# yum install httpd-tools -y
  2. [root@localhost ~]# cd /usr/local/nginx1.16/conf/
  3. [root@localhost conf]# htpasswd -c passwd.db test
  4. New password:
  5. Re-type new password:
  6. Adding password for user test
  7. [root@localhost conf]# cat passwd.db
  8. test:$apr1$//KTSBT3$FRgMmcNe.gPqvHMhdsqGy/
  • 修改密码文件的权限为400,将所有者改为www,设置nginx的运行用户能够读取
  1. [root@localhost conf]# chmod 400 passwd.db
  2. [root@localhost conf]# chown www passwd.db
  3. [root@localhost conf]# ls -l passwd.db
  4. -r-------- 1 www root 43 Jul 3 23:36 passwd.db
  • 修改主配置文件nginx.conf,添加相应认证配置项
  1. location /status {
  2. stub_status;
  3. access_log off;
  4. auth_basic "admin area";
  5. auth_basic_user_file /usr/local/nginx1.16/conf/passwd.db
  6. }
  • 检查语法,重启服务
  1. [root@localhost conf]# nginx -t
  2. nginx: the configuration file /usr/local/nginx1.16/conf/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/nginx1.16/conf/nginx.conf test is successful
  4. [root@localhost conf]# systemctl restart nginx.service
  • 用浏览器访问网址,检验控制效果,如图所示:

LNMP环境 - 图2

需要输入用户名和密码进行访问,验证通过才能访问到页面。

4.2 基于客户端的访问控制

基于客户端的访问控制是通过客户端IP地址,决定是否允许对页面访问。Nginx基于客户端的访问控制规则如下:

  1. denyIP/IP段:拒绝某个IP或IP段的客户端访问。
  2. allowIP/IP段:允许某个IP或IP段的客户端访问。
  3. 规则从上往下执行,如匹配则停止,不再往下匹配。
  • 修改主配置文件nginx.conf,添加相应配置项
  1. location = /basic_status {
  2. stub_status;
  3. deny 192.168.154.1; #拒绝这个地址
  4. allow 192.168.154.0/24; #允许这个网段
  5. deny all; #拒绝所有其它地址
  6. }
  • 重启服务
  1. [root@localhost conf]# nginx -t
  2. nginx: the configuration file /usr/local/nginx1.16/conf/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/nginx1.16/conf/nginx.conf test is successful
  4. [root@localhost conf]# systemctl restart nginx.service
  • 使用192.168.154.1地址访问,不能访问,其它地址可以

LNMP环境 - 图3


5、虚拟主机

利用虚拟主机,不用为每个要运行的网站提供一台单独的Nginx服务器或单独运行一组Nginx进程,虚拟主机提供了在同一台服务器,同一组Nginx进程上运行多个网站的功能。Nginx可以配置多种类型的虚拟主机,分别是基于IP的虚拟主机、基于域名的虚拟主机、基于端口的虚拟主机。

使用Nginx搭建虚拟主机服务器时,每个虚拟Web站点拥有独立的“server{}”配置段,各自监听的IP地址、端口号可以单独指定,当然网站名称也是不同的。

5.1 基于域名的虚拟主机(使用的最多)

配置文件案例:

  1. server { #加入 www.bt.com 对应的站点
  2. listen 80;
  3. server_name www.bt.com; #监听域名
  4. charset utf-8;
  5. access_log logs/www.bt.access.log; #日志文件
  6. location / {
  7. root /var/www/html/btcom; #www.bt.com 的工作目录
  8. index index.html index.htm;
  9. }
  10. error_page 500 502 503 504 /50x.html;
  11. location = 50x.html{
  12. root html;
  13. }
  14. }
  15. server { #加入 www.test.com 对应的站点
  16. listen 80;
  17. server_name www.test.com; #监听域名
  18. charset utf-8;
  19. access_log logs/www.test.access.log; #日志文件
  20. location / {
  21. root /var/www/html/testcom; #www.test.com 的工作目录
  22. index index.html index.htm;
  23. }
  24. error_page 500 502 503 504 /50x.html;
  25. location = 50x.html{
  26. root html;
  27. }
  28. }
  • 首先建立2个网站的根目录
  • 修改配置文件如上
  • 客户端使用域名访问验证即可

5.2 基于IP的虚拟主机

配置文件案例:

  1. server {
  2. listen 192.168.9.158:80; #监听 192.168.9.158
  3. server_name 192.168.9.158:80;
  4. …………………………………….. #省略内容
  5. }
  6. server {
  7. listen 192.168.9.110:80; #监听 192.168.9.110
  8. server_name 192.168.9.110:80;
  9. …………………………………….. #省略内容
  10. }
  • 首先给服务器配置2个不同的IP地址
  • 修改配置文件如上
  • 客户端访问测试即可

5.3 基于端口的虚拟主机

配置文件案例:

  1. server {
  2. listen 192.168.9.158:6666; #监听 6666 端口
  3. server_name 192.168.9.158:6666; #域名也可
  4. …………………………………….. #省略内容
  5. }
  6. server {
  7. listen 192.168.9.158:8888; #监听 8888 端口
  8. server_name 192.168.9.158:8888; #域名也可
  9. …………………………………….. #省略内容
  10. }
  • 修改配置文件
  • 防火墙放行以上端口
  • 客户端使用不同的端口进行测试即可

LNMP环境部署

LNMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写。L指Linux,N指Nginx,M一般指MySQL,也可以指MariaDB,P一般指PHP,也可以指Perl或Python。可以在独立主机上轻松的安装LNMP生产环境。本案例主要介绍LNMP框架安装方法以及部署Discuz社区论坛应用。

1. 安装操作系统

  • 使用VMware workstation新建虚拟机
  • 安装CentOS7.5系统,最小化安装,记得要选中“开发工具(development tools)”
  • 安装bash-completion以便可以命令补全

    1. yum install bash-completion -y
  • 安装vim等基础包

    1. yum install vim wget net-tools curl -y

2. 系统初始化配置

  • Linux 系统资源调配
    ```linux /etc/security/limits.conf
  • soft nofile 65535
  • hard nofile 65535 ```
  • 修改主机名字

    1. [root@localhost ~]# hostnamectl set-hostname www.lnmp.com
  • 配置网络。设置固定IP地址,网关及DNS服务器地址

    1. [root@localhost ~]# nmcli connection modify ens32 ipv4.addresses 192.168.154.137/24 ipv4.gateway 192.168.154.2 ipv4.dns 192.168.154.2 autoconnect yes
    2. [root@localhost ~]# nmcli connection up ens32
  • 关闭 Selinux

    1. [root@localhost ~]# vim /etc/selinux/config
    2. SELINUX=disabled
  • yum源的配置,可以根据业务需求来安装 epel、 nginx、 remi 源, 且修改配置参数。

    1. [root@localhost yum.repos.d]# yum install epel-release -y
    2. [root@localhost yum.repos.d]# rpm -ivh http://rpms.remirepo.net/enterprise/remi-release-7.rpm
    3. [root@localhost yum.repos.d]# vim remi.repo
    4. [epel]
    5. enabled=1
  • DNS 解析域名设置

    1. vi /etc/resolve.conf
    2. nameserver 192.168.154.2
    3. nameserver 202.106.0.20

3. 安装配置Nginx网站服务器

3.1 安装nginx服务

[nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key

  1. - 安装nginx服务,会安装1.16.0版本
  2. ```linux
  3. [root@www ~]# yum install nginx -y
  • 设置nginx开机启动并开启nginx服务
    ```linux [root@www ~]# systemctl enable nginx.service [root@www ~]# systemctl start nginx.service [root@www ~]# systemctl status nginx.service ● nginx.service - nginx - high performance web server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2019-05-20 18:11:58 CST; 4s ago Docs: http://nginx.org/en/docs/ Process: 1881 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS) Main PID: 1882 (nginx) CGroup: /system.slice/nginx.service
    1. ├─1882 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
    2. └─1883 nginx: worker process

May 20 18:11:58 www.lnmp.com systemd[1]: Starting nginx - high performance web server… May 20 18:11:58 www.lnmp.com systemd[1]: Started nginx - high performance web server.

  1. - 防火墙放行http服务
  2. ```linux
  3. [root@www ~]# firewall-cmd --add-service=http --permanent
  4. [root@www ~]# firewall-cmd --add-service=http
  • 测试是否可以访问nginx
    或者找个客户端,使用浏览器访问服务器地址
    1. [root@www ~]# curl http://192.168.154.137

3.2 调整nginx配置文件

  1. [root@www ~]# vim /etc/nginx/nginx.conf
  2. user nginx nginx;
  3. worker_processes 4;
  4. worker_cpu_affinity 00000001 00000010 00000100 00001000;
  5. worker_rlimit_nofile 65535;
  6. error_log /var/log/nginx/error.log warn;
  7. pid /var/run/nginx.pid;
  8. events {
  9. use epoll;
  10. worker_connections 10240;
  11. }
  12. http {
  13. include /etc/nginx/mime.types;
  14. default_type application/octet-stream;
  15. server_names_hash_bucket_size 128;
  16. client_header_buffer_size 2k;
  17. large_client_header_buffers 4 4k;
  18. client_max_body_size 8m;
  19. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  20. '$status $body_bytes_sent "$http_referer" '
  21. '"$http_user_agent" "$http_x_forwarded_for"';
  22. access_log /var/log/nginx/access.log main;
  23. sendfile on;
  24. tcp_nopush on;
  25. tcp_nodelay on;
  26. keepalive_timeout 65;
  27. fastcgi_cache_path /etc/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m;
  28. fastcgi_cache_key http://$host$request_uri;
  29. fastcgi_connect_timeout 300;
  30. fastcgi_send_timeout 300;
  31. fastcgi_read_timeout 300;
  32. fastcgi_buffer_size 4k;
  33. fastcgi_buffers 8 4k;
  34. fastcgi_busy_buffers_size 8k;
  35. fastcgi_temp_file_write_size 8k;
  36. fastcgi_cache TEST;
  37. fastcgi_cache_valid 200 302 1h;
  38. fastcgi_cache_valid 301 1d;
  39. fastcgi_cache_valid any 1m;
  40. fastcgi_cache_min_uses 1;
  41. fastcgi_cache_use_stale error timeout invalid_header http_500;
  42. open_file_cache max=204800 inactive=20s;
  43. open_file_cache_min_uses 1;
  44. open_file_cache_valid 30s;
  45. gzip on;
  46. gzip_min_length 1k;
  47. gzip_buffers 4 16k;
  48. gzip_http_version 1.0;
  49. gzip_comp_level 2;
  50. gzip_types text/plain application/x-javascript text/css application/xml;
  51. gzip_vary on;
  52. include /etc/nginx/conf.d/*.conf;
  53. }
  54. [root@www nginx]# vim /etc/nginx/conf.d/default.conf
  55. server {
  56. listen 80;
  57. server_name www.lnmp.com;
  58. #charset koi8-r;
  59. #access_log /var/log/nginx/host.access.log main;
  60. location / {
  61. root /opt/nginx/html;
  62. index index.html index.htm;
  63. }
  64. location /status
  65. {
  66. stub_status on;
  67. }
  68. #error_page 404 /404.html;
  69. # redirect server error pages to the static page /50x.html
  70. #
  71. error_page 500 502 503 504 /50x.html;
  72. location = /50x.html {
  73. root /opt/nginx/html;
  74. }
  75. location ~ .*/.(gif|jpg|jpeg|png|bmp|swf|js|css)$
  76. {
  77. expires 30d;
  78. }
  79. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  80. #
  81. #location ~ \.php$ {
  82. # proxy_pass http://127.0.0.1;
  83. #}
  84. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  85. #
  86. #location ~ \.php$ {
  87. # root html;
  88. # fastcgi_pass 127.0.0.1:9000;
  89. # fastcgi_index index.php;
  90. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  91. # include fastcgi_params;
  92. #}
  93. # deny access to .htaccess files, if Apache's document root
  94. # concurs with nginx's one
  95. #
  96. #location ~ /\.ht {
  97. # deny all;
  98. #}
  99. }
  • 由于更改了根目录,所以需要创建新的根目录
  1. [root@www nginx]# mkdir /opt/nginx/html -p
  2. [root@www nginx]# cp /usr/share/nginx/html/* /opt/nginx/html/
  3. [root@www nginx]# ll /opt/nginx/html/
  4. total 8
  5. -rw-r--r-- 1 root root 494 May 22 20:33 50x.html
  6. -rw-r--r-- 1 root root 612 May 22 20:33 index.html
  • 改变了服务配置文件,所以需要重新启动服务
  1. [root@www ~]# systemctl restart nginx.service
  2. [root@www ~]# systemctl status nginx.service

4. 安装配置MySQL数据库服务器

MySQL是目前使用最受信赖和广泛使用的开源数据库平台。全球十大最受欢迎和高流量的网站中有10个依赖于MySQL。MySQL 8.0通过提供全面的改进建立在这一势头上,旨在使创新的DBA和开发人员能够在最新一代的开发框架和硬件上创建和部署下一代Web,嵌入式,移动和云/ SaaS / PaaS / DBaaS应用程序平台。MySQL 8.0亮点包括:

  • MySQL文档存储
  • 交易数据字典
  • SQL角色
  • 默认为utf8mb4
  • 公用表表达式
  • 窗口功能
  • 以及更多

4.1 安装mysql源

  • 下载MySQL YUM仓库:

    1. [root@www ~]# wget https://dev.mysql.com/get/mysql80-community-release-el7-2.noarch.rpm
  • 安装MySQL YUM仓库:

    1. [root@www ~]# rpm -Uvh mysql80-community-release-el7-2.noarch.rpm

4.2 安装mysql数据库

  • 默认安装最新GA版MySQL
  • 可以通过运行以下命令并检查其输出来验证是否已启用和禁用了正确的子存储库

    1. [root@www ~]# yum repolist enabled | grep mysql
    2. mysql-connectors-community/x86_64 MySQL Connectors Community 108
    3. mysql-tools-community/x86_64 MySQL Tools Community 90
    4. mysql80-community/x86_64 MySQL 8.0 Community Server 113
  • 安装MySQL
    ```linux [root@www ~]# yum remove mariadb-libs -y [root@www ~]# yum install mysql-community-server [root@www ~]# systemctl start mysqld.service [root@www ~]# systemctl status mysqld.service ● mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: active (running) since Sat 2019-05-25 11:30:43 CST; 2s ago Docs: man:mysqld(8)

    1. http://dev.mysql.com/doc/refman/en/using-systemd.html

    Process: 2671 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 2746 (mysqld) Status: “SERVER_OPERATING” CGroup: /system.slice/mysqld.service

    1. └─2746 /usr/sbin/mysqld

May 25 11:30:13 www.lnmp.com systemd[1]: Starting MySQL Server… May 25 11:30:43 www.lnmp.com systemd[1]: Started MySQL Server.

  1. <a name="2ee98960"></a>
  2. #### 4.3 MySQL数据库初始化(从MySQL 5.7开始):
  3. 在服务器初始启动时,如果服务器的数据目录为空,则会发生以下情况:
  4. - 服务器已初始化。
  5. - 在数据目录中生成SSL证书和密钥文件。
  6. - 该validate_password插件安装并启用。
  7. - 将’root’@‘localhost’ 创建一个超级用户帐户。设置超级用户的密码并将其存储在错误日志文件中。要显示它,请使用以下命令:
  8. ```linux
  9. [root@www ~]# grep 'temporary password' /var/log/mysqld.log
  10. 2019-05-25T03:30:25.989353Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: gw2yrdVKWy,g
  • 通过使用生成的临时密码登录并为超级用户帐户设置自定义密码,尽快更改root密码:
    1. [root@www ~]# mysql -u root -pgw2yrdVKWy,g
    2. #进入数据库后更改密码
    3. mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';
    4. #注意:MySQL的 validate_password 插件默认安装。这将要求密码包含至少一个大写字母,一个小写字母,一个数字和一个特殊字符,并且密码总长度至少为8个字符。

4.4 配置开机启动mysql服务

  1. [root@www ~]# systemctl enable mysqld.service
  2. [root@www ~]# systemctl restart mysqld.service
  3. [root@www ~]# systemctl status mysqld.service
  4. mysqld.service - MySQL Server
  5. Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
  6. Active: active (running) since Sat 2019-05-25 11:42:41 CST; 3s ago
  7. Docs: man:mysqld(8)
  8. http://dev.mysql.com/doc/refman/en/using-systemd.html
  9. Process: 3018 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
  10. Main PID: 3042 (mysqld)
  11. Status: "SERVER_OPERATING"
  12. CGroup: /system.slice/mysqld.service
  13. └─3042 /usr/sbin/mysqld
  14. May 25 11:42:39 www.lnmp.com systemd[1]: Starting MySQL Server...
  15. May 25 11:42:41 www.lnmp.com systemd[1]: Started MySQL Server.

5. 安装配置PHP环境

5.1 安装php7的YUM源

  1. rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
  2. rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

5.2 安装PHP7.2

  1. [root@www ~]# yum install php72w php72w-cli php72w-common php72w-gd php72w-ldap php72w-mbstring php72w-mcrypt php72w-mysql php72w-pdo

5.3 安装php-fpm并启动

  1. [root@www ~]# yum install php72w-fpm php72w-opcache
  2. [root@www ~]# systemctl enable php-fpm.service
  3. Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
  4. [root@www ~]# systemctl start php-fpm.service
  5. [root@www ~]# systemctl status php-fpm.service
  6. php-fpm.service - The PHP FastCGI Process Manager
  7. Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
  8. Active: active (running) since Sat 2019-05-25 22:30:23 CST; 5s ago
  9. Main PID: 2368 (php-fpm)
  10. Status: "Ready to handle connections"
  11. CGroup: /system.slice/php-fpm.service
  12. ├─2368 php-fpm: master process (/etc/php-fpm.conf)
  13. ├─2369 php-fpm: pool www
  14. ├─2370 php-fpm: pool www
  15. ├─2371 php-fpm: pool www
  16. ├─2372 php-fpm: pool www
  17. └─2373 php-fpm: pool www
  18. May 25 22:30:22 www.lnmp.com systemd[1]: Starting The PHP FastCGI Process Manager...
  19. May 25 22:30:23 www.lnmp.com systemd[1]: Started The PHP FastCGI Process Manager.

5.4 调整php-fpm配置文件

  1. [root@www ~]# vim /etc/php-fpm.d/www.conf
  2. [www]
  3. user = nginx
  4. group = nginx

5.5 调整nginx配置文件

  1. [root@www ~]# vim /etc/nginx/conf.d/default.conf
  2. location / {
  3. root /opt/nginx/html;
  4. index index.php index.html index.htm;
  5. }
  6. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  7. #
  8. location ~ \.php$ {
  9. root /opt/nginx/html;
  10. fastcgi_pass 127.0.0.1:9000;
  11. fastcgi_index index.php;
  12. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  13. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  14. include fastcgi_params;
  15. fastcgi_cache TEST;
  16. fastcgi_cache_valid 200 302 1h;
  17. fastcgi_cache_valid 301 1d;
  18. fastcgi_cache_valid any 1m;
  19. }

5.6 创建MySQL数据库管理员

  • 认证机制必须是mysql_native_password;默认mysql8使用 caching_sha2_password的身份验证机制
  1. mysql> create user 'dbadmin'@'%' identified with mysql_native_password by 'Com.123456';
  2. mysql> grant all on *.* to 'dbadmin'@'%';
  3. mysql> grant GRANT OPTION on *.* to 'dbadmin'@'%';

6. LNMP环境测试

6.1 测试LNMP环境

  1. [root@www html]# cat test.php
  2. <?php
  3. phpinfo();
  4. ?>

LNMP环境 - 图4

6.2 测试是否可以连接MySQL数据库文件

  1. [root@www html]# cat mysql_test.php
  2. <?PHP
  3. $conn=mysqli_connect("localhost","dbadmin","your_dbpasswd");
  4. if($conn){
  5. echo"ok";
  6. }else{
  7. echo"error";
  8. }
  9. ?>
  • 在客户端访问
  1. [root@www html]# curl http://192.168.154.137/mysql_test.php
  2. ok

7.部署开源论坛Discuz

7.1 下载Discuz论坛文件

Discuz现在在gitee.com进行维护,地址为:https://gitee.com/ComsenzDiscuz/DiscuzX

  • 下载文件

    1. [root@www test]# wget https://gitee.com/ComsenzDiscuz/DiscuzX/repository/archive/master.zip
  • 解压并上传upload目录到网站根目录

    1. [root@www test]# unzip master.zip
    2. [root@www test]# cd DiscuzX/
    3. [root@www DiscuzX]# ls
    4. readme README.md upload utility
    5. [root@www DiscuzX]# mv upload/ /opt/nginx/html/

7.2 建立Discuz论坛所用数据库

  1. [root@www ~]# mysql -u dbadmin -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 11
  5. Server version: 8.0.16 MySQL Community Server - GPL
  6. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql> show databases;
  12. +--------------------+
  13. | Database |
  14. +--------------------+
  15. | information_schema |
  16. | mysql |
  17. | performance_schema |
  18. | sys |
  19. +--------------------+
  20. 4 rows in set (0.00 sec)
  21. mysql> create database discuz;
  22. Query OK, 1 row affected (0.03 sec)
  23. mysql> show databases;
  24. +--------------------+
  25. | Database |
  26. +--------------------+
  27. | discuz |
  28. | information_schema |
  29. | mysql |
  30. | performance_schema |
  31. | sys |
  32. +--------------------+
  33. 5 rows in set (0.00 sec)
  34. mysql> exit
  35. Bye

7.3 安装Discuz论坛

  • 在客户端使用浏览器打开网站地址:http://192.168.154.137/upload/install/, 会显示以下安装界面:
    LNMP环境 - 图5
  • 点击我同意
    LNMP环境 - 图6
  • 解决所有文件权限,需要可写权限

    1. [root@www ~]# cd /opt/nginx/html/upload/
    2. [root@www upload]# chmod -R 777 ./config/ ./data/ ./uc_client/ ./uc_server/
  • 然后刷新界面
    LNMP环境 - 图7

  • 选择全新安装,点击下一步
    LNMP环境 - 图8
  • 添加安装数据库有关信息,必须要按照实际情况添加。
    LNMP环境 - 图9
  • 会开始安装论坛,安装完毕后就可以访问论坛了,可以把upload文件夹重命名
    访问论坛
    LNMP环境 - 图10
    1. [root@www html]# mv upload/ discuz

7.4 可以使用论坛管理员admin用户做论坛管理

使用论坛管理员账号admin登录管理中心,就可以管理论坛了。