885 字
4 分钟
Linux 网站搭建

Linux网站搭建#

LNMP:Linux+Nginx+MySQL+PHP

LAMP:Linux+apache+MySQL+PHP

库站分离

Nginx#

nginx下载链接:https://nginx.org/en/download.html

编译环境(依赖环境)

1.安装环境
yum -y install gcc-c++ pcre-devel zlib-devel
2.下载
wget https://nginx.org/download/nginx-1.22.1.tar.gz
tar -zxvf nginx-1.22.1.tar.gz //解压安装包
3.源码安装
./configure --prefix=路径 //指定编译安装目录,默认目录位置在/usr/local
make&&make install //编译与安装
4.启动服务
cd 安装目录
./sbin/nginx //执行文件启动服务
vim /etc/profile
export NGINX_PATH=/usr/local/nginx/sbin
export PATH=$PATH:$NGINX_PATH
source /etc/profile //重新加载该配置文件
nginx -v //查看nginx版本
nginx -s stop //停止
nginx -s quit //退出
nginx -s reopen //重启
nginx -s reload //重新加载
nginx -c /usr/local/nginx/conf/nginx.conf //指定主配置文件并开启服务

nginx主配置文件

/usr/local/nginx/conf/nginx.conf
http {
include mime.types; //文件扩展名与文件类型映射表
default_type application/octet-stream; //默认文件类型
sendfile on; //支持文件发送(下载)
keepalive_timeout 65; //连接保持超时时间
server { //web服务监听配置
listen 80; //监听端口
server_name localhost; //网站名称
location / { //定义安装目录为软件根目录
root html; //网页存放目录
index index.html index.htm; //默认索引网页
}
error_page 500 502 503 504 /50x.html; //错误页面
location = /50x.html { //错误页面配置
root html;
}
}
}

PHP-fpm#

下载链接:https://www.php.net/downloads.php

更换yum源

wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install epel-release-latest-7.noarch.rpm //安装源
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

安装所有扩展

yum -y install php72w-cli php72w-common php72w-devel php-mysqli php72w-fpm php72w php72w-cli php72w-fpm php72w-curl php72w-gd php72w-mysql php72w-xml php72w-mbstring

编辑文件

vim /etc/php.ini
short_open_tag = On
whereis php.ini //查看php.ini文件相关路径
systemctl start php-fpm //启动服务
php -v //查看PHP版本

Nginx解析PHP#

编辑nginx配置文件

cd /安装路径/conf/
vim nginx.conf
location ~ \.php$ {
root html; //php网页根目录
fastcgi_pass 127.0.0.1:9000; //php-fpm的监听地址
fastcgi_index index.php; //php首页文件
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; //网站根目录
include fastcgi_params;

测试验证

在网站根目录(/安装路径/html/)创建index.php
<?
phpinfo();
?>
可以成功访问http://web服务器IP/index.php

MySQL#

下载链接: https://downloads.mysql.com/archives/community/

安装MySQL

1.下载
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.30-1.el7.x86_64.rpm-bundle.tar
scp mysql-8.0.30-1.el7.x86_64.rpm-bundle.tar zyl@192.168.137.131:/home/zyl/Desktop/
tar -xvf mysql-8.0.30-1.el7.x86_64.rpm-bundle.tar //解压包
2.安装
rpm -ivh mysql-community-server-8.0.30-1.el7.x86_64.rpm
3.安装过程报错处理
error: Failed dependencies: //报错缺少哪些依赖,就安装哪些依赖
error: Failed dependencies: mariadb-libs is obsoleted by mysql-community-libs-8.0.30-1.el7.x86_64 //在MySQL8.0中,Linux默认的mariadb-libs已废弃,需要卸载mariadb-libs
yum remove mariadb-libs.x86_64 //卸载mariadb-libs
4.systemctl start mysqld //启动服务

登录MySQL

mysql -u root -p 密码
grep "password" /var/log/mysqld.log
ALTER USER 'root'@'localhost' IDENTIFIED BY '密码'; //初次使用需要修改密码
show variables like 'validate_password%'; //查看密码复杂度
use mysql; //使用/进入数据库
select host,user from user; //查询数据库的用户信息

PHP连接MySQL#

<?
$servername = "MySQL服务器IP";
$username = "用户名";
$password = "密码";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("数据库连接失败: " . $conn->connect_error);
}
echo "数据库连接成功";
phpinfo();
?>

测试验证

报错处理:
1.数据库连接失败: Host '192.168.81.100' is not allowed to connect to this MySQL server
update user set host='%' where user='root'; //支持远程登录root
flush privileges; //刷新权限设置
2.数据库连接失败: Authentication plugin 'caching sha2 password cannot be loaded: /usr/lib64/mysql/plugin(caching sha2 password.so: cannot openshared object file: No such file or directory
alter user 'root'@'%' identified with mysql_native_password by 'password'; //修改身份验证插件
物理机访问显示“数据库连接成功”
mysqli搜索 --- Active Links 为 1 才代表成功
以上错误都解决的情况下:
开启php报错显示:
vim /etc/php.ini
display_errors = On

导入源码系统

vim includes/db.php
$db['db_host'] = "192.168.137.131";
$db['db_user'] = "root";
$db['db_pass'] = "Aa@123456";
$db['db_name'] = "cms";
数据库:
创建数据库
create database cms;
show databases;
导入数据库文件
use cms;
source cms.sql
Linux 网站搭建
https://wangyi187.top/posts/os-18-linux-website/
作者
WangYi
发布于
2024-07-27
许可协议
CC BY-NC-SA 4.0