Centos7-Nginx1.8.1 搭建LNMP环境部署

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以便可以命令补全

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

    $ yum install vim wget net-tools curl -y
    

2.系统初始化配置

  • Linux 系统资源调配

    $ cat /etc/security/limits.conf 
    *               soft    nofile            65535
    *               soft    nofile            65535
    
  • 修改主机名字

    $ hostnamectl set-hostname www.lnmp.com
    
  • 配置网络。设置固定IP地址,网关及DNS服务器地址

    $ nmcli connection modify ens33 ipv4.addresses 192.168.154.137/24 ipv4.gateway 192.168.154.2 ipv4.dns 192.168.154.2 autoconnect yes
    $ nmcli connection up ens33
    Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
    
  • 关闭Selinux

    $ vi /etc/selinux/config
    SELINUX=disabled
    
  • yum源的配置,可以根据业务需求来安装epel、 nginx、 remi源,且修改配置参数。

    $ yum install epel-release -y
    $ rpm -ivh http://rpms.remirepo.net/enterprise/remi-release-7.rpm
    $ vi remi.repo
    [epel]
    enabled=1
    
  • DNS 解析域名设置

    $ cat /etc/resolve.conf 
    nameserver 192.168.154.2
    nameserver 202.106.0.20
    

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

3.1 安装nginx服务

  • 设置nginx yum源

    $ yum install yum-utils -y
    $ cat /etc/yum.repos.d/nginx.repo 
    [nginx-stable]
    name=nginx stable repo
    baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    gpgcheck=1
    enabled=1
    gpgkey=https://nginx.org/keys/nginx_signing.key
    
    [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
    
  • 安装nginx服务,会安装1.16.0版本

    $ yum install nginx -y
    
  • 设置nginx开机启动并开启nginx服务

    $ systemctl enable nginx
    Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
    $ systemctl start nginx
    $ systemctl status nginx
    ● nginx.service - nginx - high performance web server
       Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
       Active: active (running) since Tue 2020-04-14 12:01:46 EDT; 6s ago
         Docs: http://nginx.org/en/docs/
      Process: 20355 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
     Main PID: 20356 (nginx)
       CGroup: /system.slice/nginx.service
               ├─20356 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
               └─20357 nginx: worker process
    
    Apr 14 12:01:46 www.lnmp.com systemd[1]: Starting nginx - high performance web server...
    Apr 14 12:01:46 www.lnmp.com systemd[1]: Started nginx - high performance web server.
    
  • 防火墙放行http服务

    $ firewall-cmd --add-service=http --permanent
    success
    $ firewall-cmd --add-service=http
    success
    
  • 测试是够可以访问nginx

    $ curl 192.168.101.171
    

    或者找个客户端,使用浏览器访问服务器地址

3.2 调整nginx配置文件

(文件夹有default.conf和nginx.conf)

$ vim /etc/nginx/nginx.conf
user  nginx;
worker_processes  auto;
worker_cpu_affinity auto;

# worker_processes 4;
# worker_cpu_affinity 00000001 00000010 00000100 00001000;
worker_rlimit_nofile 65535;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    use epoll;
    worker_connections  10240;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 2k;
    large_client_header_buffers 4 4k;
    client_max_body_size 8m;

    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  /var/log/nginx/access.log  main;

    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay on;
    keepalive_timeout  65;
    fastcgi_cache_path /etc/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m;
    fastcgi_cache_key http://$host$request_uri;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 4k;
    fastcgi_buffers 8 4k;
    fastcgi_busy_buffers_size 8k;
    fastcgi_temp_file_write_size 8k;
    fastcgi_cache TEST;
    fastcgi_cache_valid 200 302 1h;
    fastcgi_cache_valid 301 1d;
    fastcgi_cache_valid any 1m;
    fastcgi_cache_min_uses 1;
    fastcgi_cache_use_stale error timeout invalid_header http_500;
    open_file_cache max=204800 inactive=20s;
    open_file_cache_min_uses 1;
    open_file_cache_valid 30s;

    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;

    include /etc/nginx/conf.d/*.conf;
}
$ vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  test.cn;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /opt/nginx/html;
        index  index.html index.htm;
    }
    # 加入
    location /status
    {
        stub_status on;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /opt/nginx/html;
    }
    location ~ .*/.(gif|jpg|gpeg|png|bmp|swf|js|css)$
    {
        expires 1d;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
  • 由于更改了根目录,所以需要创建新的根目录

    $ mkdir /opt/nginx/html -p
    $ cp /usr/share/nginx/html/* /opt/nginx/html/
    $ ll /opt/nginx/html/
    total 8
    -rw-r--r--. 1 root root 494 Apr 14 12:48 50x.html
    -rw-r--r--. 1 root root 612 Apr 14 12:48 index.html
    
  • 改变了服务配置文件,所以需要重新启动服务

    $ nginx -t #检测配置文件语法是否有错误
    $ systemctl restart nginx
    $ systemctl status nginx
    

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

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

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

4.1 安装mysql源

  • 下载MySQL YUM 仓库:

    $ wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
    
  • 安装MySQL YUM 仓库:

    $ rpm -Uvh mysql80-community-release-el7-3.noarch.rpm
    

4.2 安装mysql数据库

  • 默认安装最新GA版MySQL

  • 可以通过运行以下命令并检查其输出来验证是否已启用和禁用了正确的子存储库

    $ yum repolist enabled | grep mysql
    Repository 'epel' is missing name in configuration, using id
    Repository epel is listed more than once in the configuration
    mysql-connectors-community/x86_64 MySQL Connectors Community                 141
    mysql-tools-community/x86_64      MySQL Tools Community                      105
    mysql80-community/x86_64          MySQL 8.0 Community Server                 161
    
  • 安装MySQL

    $ yum remove mariadb-libs -y
    $ yum install mysql-community-server -y
    $ systemctl start mysqld
    $ systemctl status mysqld
    ● mysqld.service - MySQL Server
       Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
       Active: active (running) since Tue 2020-04-14 13:18:59 EDT; 16s ago
         Docs: man:mysqld(8)
               http://dev.mysql.com/doc/refman/en/using-systemd.html
     Main PID: 21009 (mysqld)
       Status: "Server is operational"
       CGroup: /system.slice/mysqld.service
               └─21009 /usr/sbin/mysqld
    
    Apr 14 13:18:55 www.lnmp.com systemd[1]: Starting MySQL Server...
    Apr 14 13:18:59 www.lnmp.com systemd[1]: Started MySQL Server.
    

4.3 MySQL数据库初始化(从MySQL 5.7开始)

在服务器初始启动时,如果服务器的数据目录为空,则会发生以下情况:

  • 服务器已初始化。

  • 在数据目录中生成SSL证书和密钥文件。

  • 该validate_ password插件安装并启用。

  • 将’root’@'localhost 创建一个超级用户帐户。 设置超级用户的密码并将其存储在错误日志文件中。要显示它,请使用以下命令:

    $ grep 'temporary password' /var/log/mysqld.log
    2020-04-14T17:18:57.123362Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: tqlbNLQ*v5j1
    

    ○ 通过使用生成的临时密码登录并为超级用户帐户设置自定义密码,尽快更改root密码:

    $ mysql -uroot -ptqlbNLQ*v5j1
    # 进入数据库后更改密码
    > alter user 'root'@'localhost' identified by 'Com.123456';
    # 注意: MySQL的validate_ password 插件默认安装。这将要求密码包含至少一个大写字母,一个小写字母,一个数字和一个特殊字符,并且密码总长度至少为8个字符。
    

4.4 配置开机启动mysql服务

$ systemctl enable mysqld
$ systemctl restart mysqld
$ systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2020-04-15 07:38:02 EDT; 6s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 21314 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 21338 (mysqld)
   Status: "Server is operational"
   CGroup: /system.slice/mysqld.service
           └─21338 /usr/sbin/mysqld

Apr 15 07:38:01 www.lnmp.com systemd[1]: Starting MySQL Server...
Apr 15 07:38:02 www.lnmp.com systemd[1]: Started MySQL Server.

5. 安装配置PHP环境

5.1 安装php7的YUM源

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

5.2 安装PHP7.2

$ yum install php72w php72w-cli php72w-common php72w-gd php72w-ldap php72w-mbstring php72w-mcrypt php72w-mysql php72w-pdo -y

5.3 安装php-fpm并启动

$ yum install php72w-fpm php72w-opcache -y
$ systemctl enable php-fpm
$ systemctl start php-fpm
$ systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2020-04-15 08:23:23 EDT; 4s ago
 Main PID: 21961 (php-fpm)
   Status: "Ready to handle connections"
   CGroup: /system.slice/php-fpm.service
           ├─21961 php-fpm: master process (/etc/php-fpm.conf)
           ├─21962 php-fpm: pool www
           ├─21963 php-fpm: pool www
           ├─21964 php-fpm: pool www
           ├─21965 php-fpm: pool www
           └─21966 php-fpm: pool www

Apr 15 08:23:23 www.lnmp.com systemd[1]: Starting The PHP FastCGI Process Manager...
Apr 15 08:23:23 www.lnmp.com systemd[1]: Started The PHP FastCGI Process Manager.

5.4 调整php-fpm 配置文件

$ vi /etc/php-fpm.d/www.conf
# 修改
[www]
user = nginx
group = nginx

5.5 调整nginx配置文件

$ vi /etc/nginx/conf.d/default.conf 
location / {
        root   /opt/nginx/html;
        index  index.php index.html index.htm;
     }
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
# 开启
    location ~ \.php$ {
        root           /opt/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
    #   fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    	fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
	fastcgi_cache TEST;
	fastcgi_cache_valid 300 302 1h;
	fastcgi_cache_valid 301 1d;
	fastcgi_cache_valid any 1m;
    }

5.6 创建MySQL数据库管理员

认证机制必须是mysql_native_ password;默认mysql8使用caching_sha2_ password的身份验证机制

> create user 'dbadmin'@'%' identified with mysql_native_password by '123456Abc!';
> grant all on *.* to 'dbadmin'@'%';
> grant grant option on *.* to 'dbadmin'@'%';

6.LNMP环境测试

6.1 测试LNMP环境

$ cat test.php 
<?php
phpinfo();
?>
  • 在客户端访问 http://192.168.101.171/test.php

Centos7-Nginx1.8.1 搭建LNMP环境部署插图

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

$ cat mysql_test.php 
<?PHP
    $conn=mysqli_connect("localhost","dbadmin","Ytl@2032734117");
    if($conn){
        echo"ok";
    }else{
    echo"error";
    }
?>
  • 在客户端访问
$ curl http://192.168.101.171/mysql_test.php
ok

7. 部署开源论坛Discuz

7.1 下载Discuz论坛文件

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

  • 下载文件

    $ wget https://gitee.com/ComsenzDiscuz/DiscuzX/repository/archive/master.zip
    
  • 解压并上传upload目录到网站根目录

    $ unzip master.zip 
    $ cd DiscuzX/
    $ ls
    readme  README.md  upload  utility
    $ mv upload/ /opt/nginx/html/
    

7.2 建立Discuz论坛所用数据库

$ mysql -uroot -p123456Abc!
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> create database discuz;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| discuz             |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

7.3 安装Discuz论坛

  • 在客户端使用浏览器打开网站地址:http://192.168.154.137/upload/install ,会显示以下安装界面:

Centos7-Nginx1.8.1 搭建LNMP环境部署插图(1)

  • 点击我同意

Centos7-Nginx1.8.1 搭建LNMP环境部署插图(2)

  • 解决所有文件权限,需要可写权限

    # $ cd /opt/nginx/html/upload/
    # 尽量不要给777权限
    # $ chmod -R 777 ./config/ ./data/ ./uc_client/ ./uc_server/
    
    # $ chown -R 755 ./config/ ./data/ ./uc_server/ ./uc_client/
    
    chown -R nginx:nginx upload/
    
  • 然后刷新界面

Centos7-Nginx1.8.1 搭建LNMP环境部署插图(3)

  • 选择全新安装,点击下一步

Centos7-Nginx1.8.1 搭建LNMP环境部署插图(4)

  • 添加安装数据库有关信息必须要按照实际情况添加。

Centos7-Nginx1.8.1 搭建LNMP环境部署插图(5)

  • 会开始安装论坛,安装完毕后就可以访问论坛了,可以把upload文件夹重命名

    $ mv upload/ discuz
    

访问论坛

Centos7-Nginx1.8.1 搭建LNMP环境部署插图(6)

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

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

没有账号? 忘记密码?

社交账号快速登录