本文目的
教你通过写批量命令实现.sh在Ubuntu上实现自动化搭建php环境,并安装配置网站
真正傻瓜式的安装部署php网站
文章目录
前言
如果你也是开发php的程序员,下面这篇硬核的文章对你有莫大的帮助:
你一定很感兴趣!。
提示:大家有问题请在评论区留言,谢谢。
一、准备工作
开始前我们要先准备下:
- 全新安装的Ubuntu Server系统(例如:系统版本:[18.04 | 20.04]LTS 或更高的长期支持版)
- 拥有root的权限
- 服务器内存至少1G内存
- 网站系统zip远程资源(例如:https://www.xxx.com/php_cms.zip)
- HTTP服务器:Nginx 最新稳定版
- PHP:8.0+
- MySQL:8.0+
二、编写命令
1.新建.sh文件
例如:新建文件 init.sh
2.update和upgrade
update 是更新 /etc/apt/sources.list 和 /etc/apt/sources.list.d 中列出的源的地址,这样才能获取到最新的软件包
upgrade 是升级已安装的所有软件包,升级之后的版本就是本地地址里的,因此,在执行 upgrade 之前一定要执行 update, 这样才能更新到最新的。
代码如下:
apt-get update -y
apt-get upgrade -y
3.software-properties-common
用于添加ppa源的小工具,ubuntu server默认没装,后面可以使用 add-apt-repository 添加具体的源
代码如下:
apt-get install -y software-properties-common
4.add-apt-repository添加nginx和php具体的源
add-apt-repository 命令(实质是个python脚本)在source.list里添加 ppa 源了(同时完成导入key)
代码如下:
add-apt-repository -y ppa:ondrej/nginx
add-apt-repository -y ppa:ondrej/php
5.安装Nginx和PHP8
是更新 /etc/apt/sources.list 和 /etc/apt/sources.list.d 中列出的源的地址,这样才能获取到最新的软件包
代码如下:
apt-get update -y
apt-get upgrade -y
apt-get install nginx -y
apt-get install php8.0 php8.0-fpm -y
apt-get install php8.0-curl php8.0-gd php8.0-xml php8.0-mbstring php8.0-mysql -y
6.新建网站目录
使用makedir在home下新建site目录
代码如下:
mkdir /home/site/
7.配置Nginx并重载生效
通过cat命令修改Nginx默认网站的配置信息
下面是/etc/nginx下 配置信息的关系图
代码如下:
cat>/etc/nginx/sites-available/default<<EOF
server {
listen 80;
access_log off;
root /home/site/Public/;
index index.htm index.html index.php;
if (!-f \$request_filename) {
rewrite ^(.*)\$ /index.php\$1 last;
break;
}
location ~* \.(ini|site|pem|cert)\$ {
deny all;
}
location ~ \.php {
fastcgi_pass unix:/run/php/php8.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
set \$real_script_name \$fastcgi_script_name;
if (\$fastcgi_script_name ~ "^(.+?\.php)(/.+)\$") {
set \$real_script_name \$1;
set \$path_info \$2;
}
fastcgi_param SCRIPT_FILENAME \$document_root\$real_script_name;
fastcgi_param SCRIPT_NAME \$real_script_name;
fastcgi_param PATH_INFO \$path_info;
}
}
EOF
service nginx restart
配置完记得用restart让修改生效
8.安装unzip并下载安装包进行解压
默认新系统没有解压工具,需要安装,同时下载网站源码包进行解压
代码如下:
apt-get install unzip -y
wget -O site.zip https://www.xxx.com/php_cms.zip.zip
unzip site.zip -d /home/site/
9.安装mysql-server
安装mysql-server,配置数据库密码并打印
代码如下:
mysql_password=`cat /dev/urandom | head -n 16 | md5sum | head -c 8`
echo "mysql-server mysql-server/root_password password $mysql_password" | debconf-set-selections
echo "mysql-server mysql-server/root_password_again password $mysql_password" | debconf-set-selections
apt-get install mysql-server -y
10.修改权限
通过chown修改相关目录的权限,因为有的网站需要写数据库相关配置到文件
代码如下:
chown -R www-data:www-data /home/site/
chmod -R 755 /home/site/
echo -e "\033[37;41m网站环境已经安装完毕,您的MySQL数据库初始root密码为:${mysql_password},请通过浏览器打开http://你的IP地址或域名/即可打开网站。\033[0m"
11.删除安装包
操作完我们把源码包清理下
代码如下:
rm -rf site.zip
12.整体代码
为了方便大家使用,下面全部的代码贴下
代码如下:
apt-get update -y
apt-get upgrade -y
apt-get install -y software-properties-common
add-apt-repository -y ppa:ondrej/nginx
add-apt-repository -y ppa:ondrej/php
apt-get update -y
apt-get upgrade -y
apt-get install nginx -y
apt-get install php8.0 php8.0-fpm -y
apt-get install php8.0-curl php8.0-gd php8.0-xml php8.0-mbstring php8.0-mysql -y
mkdir /home/site/
cat>/etc/nginx/sites-available/default<<EOF
server {
listen 80;
access_log off;
root /home/site/Public/;
index index.htm index.html index.php;
if (!-f \$request_filename) {
rewrite ^(.*)\$ /index.php\$1 last;
break;
}
location ~* \.(ini|site|pem|cert)\$ {
deny all;
}
location ~ \.php {
fastcgi_pass unix:/run/php/php8.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
set \$real_script_name \$fastcgi_script_name;
if (\$fastcgi_script_name ~ "^(.+?\.php)(/.+)\$") {
set \$real_script_name \$1;
set \$path_info \$2;
}
fastcgi_param SCRIPT_FILENAME \$document_root\$real_script_name;
fastcgi_param SCRIPT_NAME \$real_script_name;
fastcgi_param PATH_INFO \$path_info;
}
}
EOF
service nginx restart
apt-get install unzip -y
wget -O site.zip https://www.xxx.com/php_cms.zip.zip
unzip site.zip -d /home/site/
mysql_password=`cat /dev/urandom | head -n 16 | md5sum | head -c 8`
echo "mysql-server mysql-server/root_password password $mysql_password" | debconf-set-selections
echo "mysql-server mysql-server/root_password_again password $mysql_password" | debconf-set-selections
apt-get install mysql-server -y
chown -R www-data:www-data /home/site/
chmod -R 755 /home/site/
echo -e "\033[37;41m网站基础环境已经安装完毕,您的MySQL数据库初始root密码为:${mysql_password},请通过浏览器打开http://你的IP地址或域名/即可打开网站。\033[0m"
rm -rf site.zip
请大家把里面修改的东西修改成自己的。
总结
.sh文件如何用,如果有客户要用这个.sh去安装我们的网站怎么用
我们只要把这个.sh文件放在服务器上,人家可以下载就可以用了。或直接运行下面这条命令也可以完成自动化部署安装网站了。
curl https://www.xxx.com/init.sh | sh