LNMP

LNMP


20231122版本|

centos:latest镜像yum出错:

nginx安装成功后的默认网页就可以通过地址:http://localhost:1060 访问:




安装过程的操作:20230612_DOcker |DOcker2 |DOcker3 |DOcker4 |DOcker5


lnmp_byp容器说明:

lnmp_byp_20230613.tar,用Ubuntu为基础系统,然后直接使用LNMP自动安装包安装的容器,2.78GB。使用DOcker4的语句来部署,包含了2023年6月12日及以前的数据库,进行过测试(见:DOcker5);

lnmp_byp_20230612.tar用Ubuntu为基础系统,然后分别安装Nginx、PHP-FPM、MarinaDB,并未成功,646MB。使用DOcker3的语句来部署,数据库有密码,但是为空,进行过测试(见:DOcker5);



基于CentOS的nginx的安装操作

注意:

(1)系统的差异:不同的Linux系统的命令还是区别蛮大的,例如Ubuntu用apt-get来安装软件,而CentOS用的是yum;服务管理的命令Ubuntu用service,CentOS用systemctl;

(2)EOL问题:也就是CentOS停服问题。例如CentOs Linux 8 从 2021.10.31 号后已经停止维护。

##报错
yum list nginx --showduplicates
Failed to set locale, defaulting to C.UTF-8
CentOS Linux 8 - AppStream                                                               84  B/s |  38  B     00:00
Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist
所以之后更新镜像需要通过 vault.centos.org来获取更新。所以需要修改软件源mirrors 地址,具体操作如下(操作):

(1)进入yum.repos.d 目录下
cd /etc/yum.repos.d/
(2)修改源链接
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
(3)要将之前的mirror.centos.org 改成 vault.centos.org
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*

(3)update问题:一般容器用的基础系统都需要对软件安装源更新,例如yum update,apt-get update。甚至很多基本的软件都没有安装;

(4)容器的一个bug?如果用一般的docker run命令建立的容器,其实是无法使用systemctl命令的。据说是容器的一个BUG?

需要注意:加了/usr/sbin/init、--privileged这两个参数。例如:

docker run -itd --privileged -p 1060:1060 -v D:BYPProjectNSsystemskepu:/var/www/html --name kepu01 centos:lnmp /usr/sbin/init

(5)软件自启动设置为开机自启动服务(php无需设置自启动):

systemctl enable mariadb

systemctl enable nginx

无需修改.bashrc

(6)php模块安装:可以直接yum -y install php安装php,需要的模块可以以后安装(参考)。例如:php-mysql。不过如果直接:

yum install php-mysql

会报错,可以用yum search php-mysql查询,发现其实名称不对:

php-mysqlnd.x86_64 : A module for PHP applications that use MySQL databases

因此,正确的安装语句为:yum install php-mysqlnd

(7)数据库安全设置:数据库安装简单:

yum install -y mariadb-server
systemctl start mariadb # 开启服务
systemctl enable mariadb # 设置为开机自启动服务
但是需要设置密码:
mysql_secure_installation #数据库安全设置
一般的选择如下:
Set root password? [Y/n] y
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] n
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y
(8)ngnix的操作

编辑配置文件
vi /etc/nginx/nginx.conf
重新启动ngnix
systemctl restart nginx
查看nginx状态(安装后不会自动启动)
systemctl status nginx

(9)默认主页位置

/var/www/html  #php默认主页位置

/usr/share/nginx/html #nginx默认主页位置


#######################################

基于CentOS 8的LNMP的容器的安装及测试
20230702
#######################################
1、拉一个基础系统
docker pull centos
Docker Desktop显示:
CREATED:2 years ago
SIZE:231.26 MB

2、建立容器
考虑systemctl问题,采用以下参数组合:
docker run -itd --privileged -p 1060:1060 -v D:BYPProjectNSsystemskepu:/var/www/html --name kepu02 centos:latest /usr/sbin/init

3、进入容器
docker exec -it kepu02 bash
##查看系统版本:
cat /etc/redhat-release
CentOS Linux release 8.4.2105

4、更新镜像(不更新,yum会报错)
cd /etc/yum.repos.d/
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*

5、安装nginx
##查看nginx的源是否存在,然后安装
yum list nginx --showduplicates
yum install -y nginx
##启动ngnix
systemctl start nginx
##设置为开机自启动服务
systemctl enable nginx
##接着编辑该配置文件
vi /etc/nginx/nginx.conf
找到服务器监听端口,将原来的80,更改为1060
##再次启动ngnix
systemctl restart nginx
nginx的默认网页就可以通过地址:http://localhost:1060 访问

6、安装php
yum -y install php
##安装php-mysql(注意名称不一样)
yum -y install php-mysqlnd
##查看php是否安装成功
编辑该配置文件
vi /etc/nginx/nginx.conf
##将web主目录root(/usr/share/nginx/html)更改为:
/var/www/html
##再次启动ngnix
systemctl restart nginx
##进入主目录,建立phpinfo页面查看能否显示
cd /var/www/html/

7、安装mariadb
yum install -y mariadb-server
systemctl start mariadb # 开启服务
systemctl enable mariadb # 设置为开机自启动服务
mysql_secure_installation #数据库安全设置
具体选项如下:
Set root password? [Y/n] y
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] n
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y

mysql -uroot -p*** # 登录数据库

8、测试kepu网站
create database bypDB;
use bypDB;
source /var/www/html/bypDB_kepu_2023052401S.sql
浏览器访问地址:http://localhost:1060/

9、导出安装最新安装的LNMP容器
docker export -o CentOS_lnmp_20230702.tar kepu02
tar大小为:641 MB

10、测试新容器
##删除kepu02容器
##导入CentOS_lnmp镜像:
docker import CentOS_lnmp_20230702.tar centos:lnmp02
导入后Desktop里面显示大小为:661.49 MB
##建立容器
考虑systemctl问题,采用以下参数组合:
docker run -itd --privileged -p 1060:1060 -v D:BYPProjectNSsystemskepu:/var/www/html --name kepu02 centos:lnmp02 /usr/sbin/init
浏览器访问地址:http://localhost:1060/
成功!



####################################################
CentOS 7 安装nginx操作
20230702
####################################################
默认情况Centos7中无Nginx的源!
docker run -itd --privileged -p 1061:1061 -v D:/BYP/Project/NS/systems/kepu:/var/www/html --name kepu01 centos:7 /usr/sbin/init
yum install -y nginx
##报错,找不到nginx

###############################
方法1:通过阿里云资源
###############################
1、增加阿里云资源:
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repon.com/repo/Centos-7.repoentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

3、运行yum makecache
4、运行yum install epel-release
5、运行yum install nginx
参考:https://www.cnblogs.com/maxwell-xu/p/8000458.html

systemctl start nginx
vi /etc/nginx/nginx.conf

修改为:
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;

server {
listen       1061 default_server;
listen       [::]:1061 default_server;
server_name  _;
root         /usr/share/nginx/html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location / {
}

error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}

##即可

###############################
方法2:通过CentOS资源
###############################
yum install -y sudo
##Nginx官网提供了Centos的源地址。因此可以如下执行命令添加源:
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
##通过yum search nginx看看是否已经添加源成功
yum list nginx --showduplicates
yum install -y nginx
systemctl start nginx
vi /etc/nginx/nginx.conf






BypInformation