CentOS 8 安裝LNMP

oakcdrom0条评论 597 次浏览

运行环境

操作系统:CentOS 8
软件版本:PHP7.3、MariaDB10.4、Nginx1.17

开始安裝

1. 安装Nginx

在CentOS 8 本身预设版本是 1.14,那Nginx 官方版本稳定版目前是 1.16,如不想使用预设版本想要使用官方采用的版请参考本次安裝流程。

到 Nginx 官方网站参考目前如何新增來源库

先安裝可以选择指定的 yum 的套件

# dnf install dnf-utils -y

新增#vim /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
module_hotfixes=true

[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
module_hotfixes=true

指定所要安裝的版本,目前官方有兩个版本可以选择,一個是(nginx-stable)稳定版另一个是(nginx-mainline)开发版,正常我都是安裝稳定版本。

# yum-config-manager --enable nginx-nginx-stable

安裝Nginx服务,如果想要安裝自行新增的来源库的话,要下這一段指令才能安裝,不然會安裝到預設的安裝檔 --disablerepo=AppStream

# dnf install nginx --disablerepo=AppStream -y

配置Nginx

 location ~ \.php$ {

        root   /usr/share/nginx/html;

        fastcgi_pass unix:/run/php-fpm/www.sock;

        fastcgi_index index.php;

        try_files $uri =404;

        fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;

        include fastcgi_params;

    }

启动服务

#systemctl start nginx ; systemctl enable nginx

2. 安装MariaDB

CentOS8现在预设是用MariaDB 10.3版本,想使用MariaDB 10.4版本可以利用新增来源库来安装。

新增加官方来源库MariaDB
vim /etc/yum.repos.d/MariaDB.repo

# MariaDB 10.4 CentOS repository list - created 2019-11-05 05:10 UTC
2
# http://downloads.mariadb.org/mariadb/repositories/
3
[mariadb]
4
name = MariaDB
5
baseurl = http://yum.mariadb.org/10.4/centos8-amd64
6
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
7
gpgcheck=1
#dnf install boost-program-options -y
#dnf install MariaDB-server MariaDB-client --disablerepo=AppStream -y

启动MariaDB服务

#systemctl enable mariadb ; systemctl start mariadb

MariaDB初始化

#mysql_secure_installation   #设定资料库的root密码

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB

SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current

password for the root user. If you've just installed MariaDB, and

haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none):

OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody

can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n]

Enabled successfully!

Reloading privilege tables..

 ... Success!

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n]

New password:                      # 
设置root的密码
28
Re-enter new password:             # 确认root的密码

Password updated successfully!

Reloading privilege tables..

 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone

to log into MariaDB without having to have a user account created for

them.  This is intended only for testing, and to make the installation

go a bit smoother.  You should remove them before moving into a

production environment.

Remove anonymous users? [Y/n]

 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This

ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]

 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can

access.  This is also intended only for testing, and should be removed

before moving into a production environment.

Remove test database and access to it? [Y/n]

 - Dropping test database...

 ... Success!

 - Removing privileges on test database...

 ... Success!

Reloading the privilege tables will ensure that all changes made so far

will take effect immediately.

Reload privilege tables now? [Y/n]

 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB

installation should now be secure.

Thanks for using MariaDB!

重启maridb服务
#systemctl restart mariadb

3.安装PHP7.3

CentOS8预设版本是PHP7.2版,那如果想要使用比较新的版本的话需要新增加来源库安装

安装 epel-release 套件服务
#dnf install -y epel-release

安裝官方所提供来源库版本
dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm -y

查询目前可安裝的版本
#dnf module list php
执行结果如下
file

使用PHP7.3版本

#dnf module reset php`
#dnf module enable php:remi-7.3

使用PHP7.4版本

#dnf module reset php
#dnf module enable php:remi-7.4

安裝PHP

#dnf install -y php php-mysqlnd php-pdo php-xml php-pear php-mbstring php-fpm php-mysql php-gd php-cli \
php-json php-opcache php-curl php-ldap php-odbc php-xmlrpc php-soap php-intl php-zip curl curl-devel \
gcc-c++ gcc php-tidy

配置php.ini
vim /etc/php.ini

[Date]
修改前
;date.timezone = 
修改后
date.timezone = Asia/Taipei
保存退出
:wq

配置php-fpm

vim /etc/php-fpm.d/www.conf

; RPM: apache user chosen to provide access to the same directories as httpd

;user = apache

user = nginx

; RPM: Keep a group allowed to write in log dir.

;group = apache

group =nginx

保存退出
:wq

启动服务
#systemctl restart php-fpm ; systemctl enable php-fpm

测试

测试 PHP 是否能够正在运行,在 web 服务目录新增 info.php 档

<?php
  phpinfo();
?>

执行结果如下
file


发表评论

? razz sad evil ! smile oops grin eek shock ??? cool lol mad twisted roll wink idea arrow neutral cry mrgreen