LAMP环境镜像使用手册
简介
- 支持备份(包含本地、远程、OSS、COS、七牛云、upyun)等功能
- 未经容许,禁止盗用图片,文档和代码不能复制传播
- 快速部署PHP网站,参考:如何部署一个PHP网站?
- 注意:安全组端口(80, 443, 21, 20000/30000)是否打开,参考:云主机安全组端口开放教程
腾讯云Ubuntu服务器默认ssh账号 ubuntu
,必须 sudo su -
命令切换至 root
,脚本位置:/root/oneinstack
镜像说明
版本说明
软件版本详细
- Apache2.4
- PHP8.3, 8.2, 8.1, 8.0, 7.4, 7.3, 7.2, 7.1, 7.0, 5.6, 5.5, 5.4, 5.3
- MySQL5.7
- Redis7.0
- Memcached1.6
- Jemalloc5.2.1
- Pure-FTPd1.0.49
- phpMyAdmin4.4.15.10
功能亮点
- 基于源码编译安装,细节安全优化,纯命令行,占用系统资源低
- 源码编译安装,细节安全优化,纯命令行,占用系统资源低
- 支持切换至8.1、8.0、7.4、7.3、7.2、7.1、7.0、5.6、5.5、5.4、5.3任意版本
- jemalloc优化MySQL内存管理
- 交互添加Nginx虚拟主机,方便快捷, 内置Let's Encrypt
- 菜单式FTP账号管理脚本,轻松建立ftp虚拟用户
- 提供在线Apache、MySQL、PHP、Redis、Memcached、phpMyAdmin升级脚本
- 提供本地备份和远程备份(服务器之间rsync)、OSS、COS、UPYUN备份功能
目录说明
源码安装目录
- Apache:
/usr/local/apache
- PHP:
/usr/local/php
- MySQL:
/usr/local/mysql
- Pureftpd:
/usr/local/pureftpd
- Redis:
/usr/local/redis
- Memcached:
/usr/local/memcached
- phpMyAdmin:
/data/wwwroot/default/phpMyAdmin
数据存储目录
- 数据库(MySQL):
/data/mysql
- 网站根目录:
/data/wwwroot/www.example.com
#添加虚拟主机会自动创建这个目录 - Web访问日志:
/data/wwwlogs
- 默认ip直接访问内容对应根目录:
/data/wwwroot/default
,该目录包含首页demo、Opcache缓存管理、phpinfo、phpmyadmin、探针等文件,目录里面文件可删除,目录不能删除。 - 首页demo:
http://公网IP
- PHPINFO地址:
http://公网IP/phpinfo.php
- Opcache地址:
http://公网IP/ocp.php
- phpmyadmin管理地址:
http://公网IP/phpMyAdmin
#建议用chrome浏览器,访问注意大小写,为了安全建议重命名或者删除 - PHP运行环境探针地址:
http://公网IP/xprober.php
参数优化
由于镜像制作是下1核1G内存配置完成,MySQL、PHP参数还是该配置的参数,强烈建议如果不是这个配置,请执行下面优化脚本:
sudo /root/oneinstack/optimize.sh
- 文档命令请勿bash optimize.sh(或者sh)执行
- 可能需要等待1分钟,请勿中断
- 此脚本会根据系统配置,自动优化Nginx、MySQL、PHP、JVM等参数
[root@WeiLiu ~]# sudo /root/oneinstack/optimize.sh
Shutting down MySQL.. [ OK ]
Starting MySQL. [ OK ]
php版本切换
sudo /root/oneinstack/change_php_version.sh
[root@WeiLiu ~]# sudo /root/oneinstack/change_php_version.sh
#######################################################################
# OneinStack for CentOS/RedHat 6+ Debian 8+ and Ubuntu 16+ #
# Change your PHP version #
#######################################################################
Current PHP Version: 7.4 #注解: 当前PHP版本
Please select a version of the PHP:
1. php-5.3
2. php-5.4
3. php-5.5
4. php-5.6
5. php-7.0
6. php-7.1
7. php-7.2
8. php-7.3
9. php-7.4
10. php-8.0
11. php-8.1
12. php-8.2
13. php-8.3
Please input a number:(Default 9 press Enter) 10 #注解: 切换至序号10, 即php-8.0
You have successfully changed to 8.0 #注解: 切换成功标示
[root@WeiLiu ~]# php -v #注解: 验证切换后当前PHP版本
PHP 8.0.24 (cli) (built: Oct 23 2022 15:32:28) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.24, Copyright (c) Zend Technologies
with Zend OPcache v8.0.24, Copyright (c), by Zend Technologies
将网站数据迁移至数据云盘
如果您购买了数据云盘(脚本仅支持未本地挂载磁盘),建议先挂载数据云盘,将网站内容、数据库放在数据云盘中,未购买数据云盘请忽略该教程!
sudo /root/oneinstack/move_auto_fdisk.sh
[root@WeiLiu ~]# sudo /root/oneinstack/move_auto_fdisk.sh
#######################################################################
# OneinStack for CentOS/RedHat 6+ Debian 8+ and Ubuntu 16+ #
# Auto fdisk #
#######################################################################
Step 1.No lock file, begin to create lock file and continue
Step 2.Begin to check free disk
You have a free disk, Now will fdisk it and mount it
This system have free disk :
/dev/vdb
Step 3.Begin to fdisk free disk
Step 4.Begin to make directory
Step 5.Begin to write configuration to /etc/fstab and mount device
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.8G 0 1.8G 0% /dev
tmpfs 1.8G 24K 1.8G 1% /dev/shm
tmpfs 1.8G 432K 1.8G 1% /run
tmpfs 1.8G 0 1.8G 0% /sys/fs/cgroup
/dev/vda1 50G 8.8G 39G 19% /
tmpfs 361M 0 361M 0% /run/user/0
/dev/vdb1 9.8G 37M 9.3G 1% /data
数据库管理
数据库默认root密码
sudo grep dbrootpwd /root/oneinstack/options.conf
[root@WeiLiu ~]# sudo grep dbrootpwd /root/oneinstack/options.conf
dbrootpwd='A0R3Ru5Z' #注解: 数据库root密码为 A0R3Ru5Z
重置数据库root密码
sudo /root/oneinstack/reset_db_root_password.sh
[root@WeiLiu ~]# sudo grep dbrootpwd /root/oneinstack/options.conf
dbrootpwd='A0R3Ru5Z'
[root@WeiLiu ~]# sudo /root/oneinstack/reset_db_root_password.sh
#######################################################################
# OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
# Reset Database root password for OneinStack #
#######################################################################
Please input the root password of database: VrTN4k5PYHsw #注解: 输入数据库新的root密码,不少于5个字符
Password reset succesfully! #注解: 成功标识
The new password: VrTN4k5PYHsw
[root@WeiLiu ~]# mysql -uroot -p #注解: 命令行进入MySQL控制台
Enter password: #注解: 输入数据库root密码,输入时不在屏幕显示
MySQL [(none)]> #注解: 成功进入MySQL命令行控制台
创建一个数据库
方法一:命令行创建
例:创建 oneinstack
的数据库
mysql -uroot –p #进入数据库控制台
Enter password: #数据库root密码,⚠️输入密码不显示在屏幕上
MySQL [(none)]> create database oneinstack; #特别注意有分号
MySQL [(none)]> show databases; #查看数据库,除oneinstack数据库,其它3个为系统默认库,不能删除
MySQL [(none)]> exit; #退出数据库控制台,特别注意有分号
[root@WeiLiu ~]# mysql -uroot -p
Enter password:
MySQL [(none)]> create database oneinstack; #注解: 创建库名为 oneinstack 的数据库,特别注意有分号
Query OK, 1 row affected (0.01 sec)
MySQL [(none)]> show databases; #注解: 显示所有数据库,分号结束符
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| oneinstack |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
MySQL [(none)]> exit; #注解: 退出数据库命令行控制台,分号结束符
Bye
方法二:phpMyAdmin创建
1). 浏览器输入 http://公网IP/phpMyAdmin
浏览器建议用谷歌或火狐,否则可能会报错,注意phpMyAdmin大小写
2). 按照下图创建数据库
如下图:
删除一个数据库
方法一:命令行删除
例:删除 oneinstack
的数据库
mysql -uroot –p #进入数据库控制台
Enter password: #数据库root密码,输入密码不显示在屏幕上
MySQL [(none)]> drop database oneinstack; #删除库名为 oneinstack 的数据库
MySQL [(none)]> show databases; #查看数据库
MySQL [(none)]> exit; #退出数 据库控制台,特别注意有分号
[root@WeiLiu ~]# mysql -uroot -p
Enter password:
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| oneinstack |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
MySQL [(none)]> drop database oneinstack; #注解: 删除库名为oneinstack的数据库,其余3个库为MySQL系统库请勿删除,特别注意有分号
Query OK, 0 rows affected (0.00 sec)
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
MySQL [(none)]> exit;
Bye
方法二:phpMyAdmin删除
1). 浏览器输入 http://公网IP/phpMyAdmin
浏览器建议用谷歌或火狐,否则可能会报错,注意phpMyAdmin大小写
2). 按照下图创建数据库
如下图:
创建数据库用户
为了安全我们通常给每一个网站程序建立唯一的数据库连接帐号(建议帐号名不要为 root
)
例:添加一个用户名为 db_user
,密码为 db_pass
,授权为本机 localhost
对 db_name
数据库所有权限,命令如下:
mysql -uroot -p
Enter password: #输入数据库的root密码,默认不显示密码
MySQL [(none)]> grant all privileges on db_name.* to db_user@'localhost' identified by 'db_pass'; #授权语句,特别注意有分号
MySQL [(none)]> flush privileges; #权限立即生效
MySQL [(none)]> exit; #退出数据库控制台,特别注意有分号
[root@WeiLiu ~]# mysql -uroot -p
Enter password:
MySQL [(none)]> grant all privileges on db_name.* to db_user@'localhost' identified by 'db_pass';
Query OK, 0 rows affected, 2 warnings (0.01 sec)
MySQL [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MySQL [(none)]> exit;
Bye
如何配置数据库远程连接
为了安全考虑,OneinStack仅允许云主机本机(localhost
)连接数据库,如果需要远程连接数据库,需要如下操作:
云主机安全组端口(如云厂商控制台)、iptables(部分云主机没有启用iptables,可跳过)、grant授权用户,缺一不可。
1. 云主机安全组端口开放3306端口
参考文档:《云主机安全组端口开放教程》
2. 打开iptables 3306端口
镜像V2.0以上版本,未开启iptables,请忽略此步骤2
参考: 《关于iptables防火墙》,注意端口号。
3. 数据库授权
远程连接新建一个帐号(帐号名不能为 root
, 远程数据库账号不建议 root
, 如需root远程连接,请更新 mysql.user
表对应行)。
例:添加一个用户名为db_user
,密码为db_pass
,授权为%
(%
表示所有外网IP能连接)对db_name
数据库所有权限,命令如下:
mysql -uroot -p
MySQL [(none)]> grant all privileges on db_name.* to db_user@'%' identified by 'db_pass'; #授权语句,特别注意有分号
MySQL [(none)]> flush privileges;
MySQL [(none)]> exit; #退出数据库控制台,特别注意有分号
[root@WeiLiu ~]# mysql -uroot -p
Enter password:
MySQL [(none)]> grant all privileges on db_name.* to db_user@'%' identified by 'db_pass';
Query OK, 0 rows affected, 2 warnings (0.01 sec)
MySQL [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MySQL [(none)]> exit;
Bye
如何管理虚拟主机
添加虚拟主机
sudo /root/oneinstack/vhost.sh
[root@WeiLiu ~]# sudo /root/oneinstack/vhost.sh
#######################################################################
# OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
#######################################################################
What Are You Doing?
1. Use HTTP Only #注解: 1. 仅配置http
2. Use your own SSL Certificate and Key #注解: 2. 自己购买ssl替换,自动配置http、https
3. Use Let's Encrypt to Create SSL Certificate and Key #注解: 3. Let's 免费证书,自动配置http、https
q. Exit #注解: 4. 退出
Please input the correct option: 3
Please input domain(example: www.example.com): demo.linuxeye.com #注解: 添加的虚拟主机域名
domain=demo.linuxeye.com
Please input the directory for the domain:demo.linuxeye.com :
(Default directory: /data/wwwroot/demo.linuxeye.com): #注解: 网站根目录(绝对路径)
Virtual Host Directory=/data/wwwroot/demo.linuxeye.com
Create Virtul Host directory......
set permissions of Virtual Host directory......
Do you want to add more domain name? [y/n]: y #注解: 是否添加更多域名或IP访问同一个网站根目录
Type domainname or IP(example: example.com other.example.com): demo2.linuxeye.com #注解: 输入域名或IP(添加多个以空格隔开)
domain list=demo2.linuxeye.com
Please select domain cert key length.
Enter one of 2048, 3072, 4096, 8192 will issue a RSA cert.
Enter one of ec-256, ec-384, ec-521 will issue a ECC cert.
Please enter your cert key length (default 2048): 2048 #注解: 申请免费证书加密长度
Please enter your email: lj2007331@mail.com #注解: 输入邮箱
[Sun Oct 30 04:27:06 PM CST 2022] Create account key ok.
[Sun Oct 30 04:27:06 PM CST 2022] No EAB credentials found for ZeroSSL, let's get one
[Sun Oct 30 04:27:13 PM CST 2022] Registering account: https://acme.zerossl.com/v2/DV90
[Sun Oct 30 04:27:15 PM CST 2022] Could not get nonce, let's try again.
[Sun Oct 30 04:27:39 PM CST 2022] Registered
[Sun Oct 30 04:27:39 PM CST 2022] ACCOUNT_THUMBPRINT='LMG4Elq-AFm-IH17QAk3yjmp-8TNoxi__gWTIIT1tzY'
....
[Sun Oct 30 04:28:20 PM CST 2022] Cert success.
Syntax OK
Restart Apache......
Allow Apache access_log? [y/n]: y #注解: 是否记录访问日志
You access log file=/data/wwwlogs/demo.linuxeye.com_apache.log
#######################################################################
# OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
#######################################################################
Your domain: demo.linuxeye.com
Virtualhost conf: /usr/local/apache/conf/vhost/demo.linuxeye.com.conf
Directory of: /data/wwwroot/demo.linuxeye.com
Let's Encrypt SSL Certificate:/usr/local/apache/conf/ssl/demo.linuxeye.com.crt
SSL Private Key: /usr/local/apache/conf/ssl/demo.linuxeye.com.key
上述Directory of为网站根目录(/data/wwwroot/demo.linuxeye.com
),脚本执行后会自动创建,部署网站时请将代码上传到该目录(目录、子目录和文件权限都为:www
)
- 对应Apache配置文件路径:
/usr/local/apache/conf/vhost/demo.linuxeye.com.conf
- 伪静态请将
.htaccess
放到网站根目录/data/wwwroot/demo.linuxeye.com
删除虚拟主机
sudo /root/oneinstack/vhost.sh --del
[root@WeiLiu ~]# sudo /root/oneinstack/vhost.sh --del
#######################################################################
# OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
#######################################################################
Virtualhost list: #注解: 已经绑定域名
demo1.linuxeye.com demo.linuxeye.com www.linuxeye.com
Please input a domain you want to delete: demo.linuxeye.com #注解: 删除demo.linuxeye.com域名虚拟主机
Do you want to delete Virtul Host directory? [y/n]: y #注解: 是否删除网站根目录,注意备份
Press Ctrl+c to cancel or Press any key to continue... #注解: 按Ctrl+c取消,人已经继续执行
Domain: demo.linuxeye.com has been deleted.
如何管理FTP账号
sudo /root/oneinstack/pureftpd_vhost.sh
[root@WeiLiu ~]# sudo /root/oneinstack/pureftpd_vhost.sh
#######################################################################
# OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
# FTP virtual user account management #
#######################################################################
What Are You Doing?
1. UserAdd #注解: 添加Ftp账号
2. UserMod #注解: 更改某个Ftp账号根目录权限
3. UserPasswd #注解: 更改某个Ftp密码
4. UserDel #注解: 删除某个Ftp账号
5. ListAllUser #注解: 列出所有Ftp账号
6. ShowUser #注解: 显示某个Ftp账号详情
q. Exit #注解: 退出(字母q)
Please input the correct option: 1
Please input a username: ftpuser
Please input the password: UArUDjLZ12HD
Please input the directory(Default directory: /data/wwwroot): #注解: ftpuser用户根目录,建议默认,可直接回车
Password:
Enter it again:
#####################################
[ftpuser] create successful!
You user name is : ftpuser
You Password is : UArUDjLZ12HD
You directory is : /data/wwwroot
建议本地PC使用《Filezilla》客户端登录Ftp服务器。
如何备份
设置备份参数
sudo /root/oneinstack/backup_setup.sh
[root@WeiLiu ~]# sudo /root/oneinstack/backup_setup.sh
#######################################################################
# OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
# Setup the backup parameters #
#######################################################################
Please select your backup destination:
1. Localhost #解析: 1. 本地备份
2. Remote host #解析: 2. 远程备份(ssh)
3. Aliyun OSS #解析: 3. 阿里云OSS备份
4. Qcloud COS #解析: 4. 腾讯云COS备份
5. UPYUN #解析: 5. 又拍云备份
6. QINIU #解析: 6. 七牛云备份
7. Amazon S3 #解析: 7. 亚马孙S3备份
8. Dropbox #解析: 8. Dropbox备份
Please input numbers:(Default 1 press Enter) 1
Please select your backup content:
1. Only Database
2. Only Website
3. Database and Website
Please input a number:(Default 1 press Enter) 1 #解析: 选择备份数据库
Please enter the directory for save the backup file:
(Default directory: /data/backup): #解析: 本地或远程备份目录
Please enter a valid backup number of days:
(Default days: 5): 10 #解析: 备份保留天数
Please enter one or more name for database, separate multiple database names with commas:
(Default database: linuxeye,oneinstack) oneinstack,linuxeye #解析: 备份数据库,以逗号隔开
You have to backup the content:
Database: oneinstack,linuxeye
立即备份
sudo /root/oneinstack/backup.sh
** 设置计划任务自动定时备份 **
sudo echo '0 1 * * * sudo /root/oneinstack/backup.sh > /dev/null 2>&1' >> /var/spool/cron/root
上面命令请勿重复执行
查看本地备份
ls -l /data/backup
[root@WeiLiu ~]# ls -l /data/backup/
total 12
-rw-r--r-- 1 root root 637 Oct 30 19:15 DB_linuxeye_20221030_191506.tgz
-rw-r--r-- 1 root root 258 Oct 30 19:15 db.log
-rw-r--r-- 1 root root 641 Oct 30 19:15 DB_oneinstack_20221030_191506.tgz
如果设置远程云存储备份,查看某云存储
** 登陆某云管理控制台 **
如下图:
如何管理服务
- Systemd
- Service
systemctl {start|stop|status|restart|reload} httpd
systemctl {start|stop|restart|reload|status} php-fpm
systemctl {start|stop|restart|status} mysqld
systemctl {start|stop|restart|status} pureftpd
systemctl {start|stop|status|restart|reload} redis-server
systemctl {start|stop|status|restart|reload} memcached
service httpd {start|stop|status|restart|reload}
service php-fpm {start|stop|restart|reload|status}
service mysqld {start|stop|restart|status}
service pureftpd {start|stop|restart|status}
service redis-server {start|stop|status|restart|reload}
service memcached {start|stop|status|restart|reload}
如何升级版本
sudo /root/oneinstack/upgrade.sh
[root@WeiLiu ~]# sudo /root/oneinstack/upgrade.sh -h #注解: 查看升级命令帮助信息
#######################################################################
# OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
# Upgrade Software versions for OneinStack #
#######################################################################
Usage: /root/oneinstack/upgrade.sh command ...[version]....
--help, -h Show this help message
--nginx [version] Upgrade Nginx
--tengine [version] Upgrade Tengine
--openresty [version] Upgrade OpenResty
--apache [version] Upgrade Apache
--tomcat [version] Upgrade Tomcat
--db [version] Upgrade MySQL/MariaDB/Percona
--php [version] Upgrade PHP
--redis [version] Upgrade Redis
--memcached [version] Upgrade Memcached
--phpmyadmin [version] Upgrade phpMyAdmin
--oneinstack Upgrade OneinStack latest
--acme.sh Upgrade acme.sh latest
[root@WeiLiu ~]# sudo /root/oneinstack/upgrade.sh
#######################################################################
# OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
# Upgrade Software versions for OneinStack #
#######################################################################
What Are You Doing?
1. Upgrade Nginx/Tengine/OpenResty
2. Upgrade Apache
3. Upgrade Tomcat
4. Upgrade MySQL/MariaDB/Percona
5. Upgrade PHP
6. Upgrade Redis
7. Upgrade Memcached
8. Upgrade phpMyAdmin
9. Upgrade OneinStack latest
10. Upgrade acme.sh latest
q. Exit
Please input the correct option: q
如何添加扩展组件
添加php扩展
如新增swoole扩展,如下命令:
sudo /root/oneinstack/install.sh --php_extensions swoole
可执行 sudo /root/oneinstack/install.sh --help
查看支持哪些扩展
添加附件组件
sudo /root/oneinstack/addons.sh
可执行 sudo /root/oneinstack/addons.sh --help
查看支持参数
[root@WeiLiu ~]# sudo /root/oneinstack/addons.sh -h #注解: 添加附件组件命令帮助
#######################################################################
# OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
# Install/Uninstall Extensions #
#######################################################################
Usage: /root/oneinstack/addons.sh command ...
--help, -h Show this help message
--install, -i Install
--uninstall, -u Uninstall
--composer Composer
--fail2ban Fail2ban
--ngx_lua_waf Ngx_lua_waf
--python Python (PATH: /usr/local/python)
[root@WeiLiu ~]# sudo /root/oneinstack/addons.sh
#######################################################################
# OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+ #
# Install/Uninstall Extensions #
#######################################################################
What Are You Doing?
1. Install/Uninstall PHP Composer
2. Install/Uninstall fail2ban
3. Install/Uninstall ngx_lua_waf
q. Exit
Please input the correct option: 3
Please select an action:
1. install
2. uninstall
Please input a number:(Default 1 press Enter) 1