【服务器管理】手动部署LNMP环境(CentOS 8)(非阿里云版本)

简述

如果是你是阿里云的服务器,我推荐你看引用的文章,本文也是参考了很多这篇文章的内容。

https://help.aliyun.com/document_detail/173042.htm

  • 系统版本: CentOS 8

其实CentOS 7的版本可能更好安装一点,但是我有个服务推荐使用CentOS 8。so,我只能用这个先(之后再看看能不能研究一波)

阿里云给的配置教程,我觉得已经非常好了,但是有一个问题,它中间要求把防火墙关掉,我觉得安全性不是很好。而且也不长久啊,真的要使用,应该是需要在有防火墙的情况下,再进行这些操作。而这些对新手来说确实不够友好

下面步骤很多,参考了上述教程,因此就有时候会省略输出内容

准备工作

  1. 一台云服务器。
  2. 命令行熟悉(以下操作都是在服务器的命令行下进行的)
  3. 配置好yum的源,配置教程在这里 - 【服务器管理】CentOS 8 EOL如何切换yum / dnf源?(非阿里云版本)

是否需要配置源这个,网上众说纷纭,反正有种说法是,CentOS 8不配置会有点麻烦,因此我还是切换了一下。总之,后面是在这个部分完成之后再开始的。

安装步骤

这里注意,这里我们跳过了原来的教程的关闭防火墙等的步骤,因为其实没有必要。

安装时候,绝大部分需要输入都输入y就好了,选默认配置。你自己看的时候再选一选也行。

步骤一:下载Nginx

  1. 安装Nginx
dnf -y install http://nginx.org/packages/centos/8/x86_64/RPMS/nginx-1.16.1-1.el8.ngx.x86_64.rpm
  1. 查看版本,当正确输出版本号,说明成功
nginx -v
  • 输出范例
[root@Sean ~]# nginx -v
nginx version: nginx/1.16.1

步骤二:下载Mysql

  1. 下载mysql
dnf -y install @mysql
  1. 查看版本看是否安装成功
 mysql -V
  • 输出范例
[root@Sean ~]# mysql -V
mysql  Ver 8.0.26 for Linux on x86_64 (Source distribution)

步骤三:下载PHP

  1. 更新epel源
dnf -y install epel-release
dnf update epel-release
  1. 运行以下命令删除缓存的无用软件包并更新软件源。
dnf clean all
dnf makecache
  1. 启用php:7.3模块
dnf module enable php:7.3
  1. 安装php相关模块
dnf install php php-curl php-dom php-exif php-fileinfo php-fpm php-gd php-hash php-json php-mbstring php-mysqli php-openssl php-pcre php-xml libsodium
  1. 查看版本,看安装是否成功,
php -v
  • 输出范例
[root@Sean ~]# php -v
PHP 7.3.20 (cli) (built: Jul  7 2020 07:53:49) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.20, Copyright (c) 1998-2018 Zend Technologies

步骤四:配置Nginx

  1. 先查一下Nginx服务器的默认配置文件
cat /etc/nginx/nginx.conf
  • 输出一般是下面这样,在http那个block里,最后一行的include里面就写了 include /etc/nginx/conf.d/*.conf 这个就是配置文件,之后就是要修改这个文件。
[root@Sean ~]# cat /etc/nginx/nginx.conf

user  nginx;
worker_processes  1;

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


events {
    
    
    worker_connections  1024;
}


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

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
[root@Sean ~]#
  1. 进入对应的文件夹,先把之前备份一下
cd /etc/nginx/conf.d/
cp default.conf default.conf.bak
  1. 修改配置文件
  • 用vim打开
vi default.conf
  • i进入编辑模式,然后修改
  • 找到location部分修改成下面这样(记住这个root的地址
location / {
    
    
    root   /usr/share/nginx/html;
    index  index.html index.htm index.php;
}
  • 去掉被注释的location ~ \.php$大括号内容前的#,并修改大括号的内容,修改成下面这样
location ~ \.php$ {
    
    
    root           /usr/share/nginx/html;
    fastcgi_pass   unix:/run/php-fpm/www.sock;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}
  • esc键 输入:wq 保存文件
  1. 启动服务+设置开机自启动
systemctl start nginx
systemctl enable nginx

步骤五:配置Mysql

  1. 启动mysql,并设置为自启动
systemctl enable --now mysqld
  1. 看是否启动成功
systemctl status mysqld
  • 如果输出是Active: active (running) 就是成功了,类似于下面这样,截取了一部分
[root@Sean ~]# systemctl status mysqld
* mysqld.service - MySQL 8.0 database server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since
  1. 输入下面命令,修改密码
mysql_secure_installation
  • 一路填y就行了,中间第二个问题是问密码策略难度,这个选2,最难就行了,这样最安全,然后输入密码的时候,设置一个复杂一点的密码就好了 。其他都是推荐选y。当然你自己想要看看再选也可以。

步骤六:配置PHP

  1. 修改php配置文件
vi /etc/php-fpm.d/www.conf
  • 输入/user找一下 user = apachegroup = apache,将apache修改为nginx。大概是在下面文末的位置。
[root@Sean ~]# cat /etc/php-fpm.d/www.conf
; Start a new pool named 'www'.
; the variable $pool can be used in any directive and will be replaced by the
; pool name ('www' here)
[www]

; Per pool prefix
; It only applies on the following directives:
; - 'access.log'
; - 'slowlog'
; - 'listen' (unixsocket)
; - 'chroot'
; - 'chdir'
; - 'php_values'
; - 'php_admin_values'
; When not set, the global prefix (or @php_fpm_prefix@) applies instead.
; Note: This directive can also be relative to the global prefix.
; Default Value: none
;prefix = /path/to/pools/$pool

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache user chosen to provide access to the same directories as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
  1. 之后就是保存vim的保存方式,上面已经讲过了不赘述

  2. 创建一个网页。很自然,你需要创建一个网页,才能后续访问,所以很合理。这时候需要自己写一个网页,放在根目录,因为一开始我们设置的根目录是/usr/share/nginx/html,所以这时候,在这个地方创建一个php文件试试。例如,phpinfo.php

vim /usr/share/nginx/html/phpinfo.php
  • 网页内容就放最简单的
<?php echo phpinfo(); ?>
  • 然后保存
  1. 启动php-fpm 并且设置开机自启
systemctl start php-fpm
systemctl enable php-fpm

步骤七:测试是否成功

在本地的电脑,找个浏览器,通过http://<公网IP地址>/phpinfo.php,看看能不能有信息出来,如果能访问成功,则说明完成。

  • 最后,在服务器上可以删掉那个phpinfo.php文件,避免暴露这个部分。

猜你喜欢

转载自blog.csdn.net/a19990412/article/details/129403950