文章目录
一、PHP
- 源码编译php:下载地址https://www.php.net/distributions/php-7.4.12.tar.gz
- 依赖:systemd-devel、libxml2-devel、sqlite-devel、libcurl-devel、libpng-devel、
yum install -y systemd-devel libxml2-devel sqlite-devel libcurl-devel libpng-devel
- 依赖oniguruma、oniguruma-devel
# 下载地址
https://vault.centos.org/centos/8/AppStream/x86_64/os/Packages/oniguruma-6.8.2-2.el8.x86_64.rpm
https://vault.centos.org/centos/8/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
- 配置文件位置:/usr/local/php/etc/
[root@server php-7.4.12]# cp php.ini-development /usr/local/php/etc/php.ini
[root@server etc]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@server etc]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
- 修改时区
vim php.ini
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = Asia/Shanghai
- 修改php-fpm.conf
[global]
; Pid file
; Note: the default prefix is /usr/local/php/var
; Default Value: none
pid = run/php-fpm.pid # 取消注释
- 添加php到环境变量
# 添加路径/usr/local/php/bin
[root@server ~]# vim ~/.bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
PATH=$PATH:$HOME/bin:/usr/local/php/bin
export PATH
- 添加服务到systemd
[root@server ~]# cp /root/php-7.4.12/sapi/fpm/php-fpm.service /lib/systemd/system
[root@server php-7.4.12]# systemctl daemon-reload
[root@server php-7.4.12]# systemctl start php-fpm.service
[root@server php-7.4.12]# netstat -antlp|grep :9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 250318/php-fpm: mas
因为php安装在/usr/local,默认设置为只读,需要修改配置
# 修改
[root@server php-7.4.12]# vim /lib/systemd/system/php-fpm.service
# Mounts the /usr, /boot, and /etc directories read-only for processes invoked by this unit.
#ProtectSystem=full # 注释
- 使用nginx测试
vim /usr/local/nginx/conf/nginx.conf
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
# 添加php测试页
[root@server php-7.4.12]# vim /usr/local/nginx/html/index.php
- php添加新模块
# 下载地址:http://pecl.php.net/get/memcache-4.0.5.2.tgz
# 源码编译
## 使用phpize给源码做配置
## 安装依赖autoconf
[root@server memcache-4.0.5.2]# yum install -y autoconf
[root@server memcache-4.0.5.2]# phpize
Configuring for:
PHP Api Version: 20190902
Zend Module Api No: 20190902
Zend Extension Api No: 320190902
[root@server memcache-4.0.5.2]# ll
total 660
drwxr-xr-x 2 root root 54 Jan 12 00:50 autom4te.cache
drwxr-xr-x 2 root root 241 Jan 12 00:49 build
-rw-r--r-- 1 501 games 1591 Dec 20 2019 cloudbuild.yaml
-rw-r--r-- 1 501 games 4547 Dec 20 2019 config9.m4
-rw-r--r-- 1 root root 1649 Jan 12 00:50 config.h.in
-rw-r--r-- 1 501 games 67 Dec 20 2019 config.m4
-rwxr-xr-x 1 root root 454509 Jan 12 00:50 configure # phpise执行后,出现configure可执行程序
-rw-r--r-- 1 root root 5082 Jan 12 00:50 configure.ac
-rw-r--r-- 1 501 games 1079 Dec 20 2019 config.w32
-rw-r--r-- 1 501 games 32 Dec 20 2019 CREDITS
drwxr-xr-x 2 root root 39 Jan 12 00:49 docker
-rw-r--r-- 1 501 games 206 Dec 20 2019 Dockerfile
-rw-r--r-- 1 501 games 509 Dec 20 2019 example.php
-rw-r--r-- 1 501 games 3208 Dec 20 2019 LICENSE
-rw-r--r-- 1 501 games 29064 Dec 20 2019 memcache.php
drwxr-xr-x 2 root root 299 Jan 12 00:49 php7
-rw-r--r-- 1 501 games 5888 Dec 20 2019 README
-rw-r--r-- 1 root root 126747 Jan 12 00:50 run-tests.php
drwxr-xr-x 2 root root 4096 Jan 12 00:49 tests
[root@server memcache-4.0.5.2]# ./configure --enable-memcache
[root@server memcache-4.0.5.2]# make
[root@server memcache-4.0.5.2]# make install
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/
[root@server memcache-4.0.5.2]# ll /usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/
total 11336
-rwxr-xr-x 1 root root 730768 Jan 12 00:52 memcache.so # 生成动态链接库,然后就可以用php加载这个模块了
-rwxr-xr-x 1 root root 7490462 Jan 11 04:45 opcache.a
-rwxr-xr-x 1 root root 3381832 Jan 11 04:45 opcache.so
# 修改php.ini,加载新模块
[root@server memcache-4.0.5.2]# vim /usr/local/php/etc/php.ini
## 定位到位置
;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
; If you wish to have an extension loaded automatically, use the following
; syntax:
;
; extension=modulename
;
; For example:
;
; extension=mysqli
;
; When the extension library to load is not located in the default extension
; directory, You may specify an absolute path to the library file:
;
; extension=/path/to/extension/mysqli.so
;
; Note : The syntax used in previous PHP versions ('extension=<ext>.so' and
; 'extension='php_<ext>.dll') is supported for legacy reasons and may be
; deprecated in a future PHP major version. So, when it is possible, please
; move to the new ('extension=<ext>) syntax.
;
; Notes for Windows environments :
;
; - Many DLL files are located in the extensions/ (PHP 4) or ext/ (PHP 5+)
; extension folders as well as the separate PECL DLL download (PHP 5+).
; Be sure to appropriately set the extension_dir directive.
;
extension=memcache # 上面查到so文件的名字
;extension=bz2
;extension=curl
;extension=ffi
;extension=ftp
;extension=fileinfo
;extension=gd2
;extension=gettext
;extension=gmp
;extension=intl
;extension=imap
;extension=ldap
;extension=mbstring
# 重启php-fpm服务
[root@server memcache-4.0.5.2]# systemctl reload php-fpm.service
# 查看已加载模块
[root@server memcache-4.0.5.2]# php -m | grep memcache
memcache
# 安装memcached服务并且测试memcache
[root@rs1 ~]# yum install -y memcached
[root@rs1 ~]# systemctl enable --now memcached
Created symlink /etc/systemd/system/multi-user.target.wants/memcached.service → /usr/lib/systemd/system/memcached.service.
memcache优点:请求直接在内存中命中,减少了php处理的负担,因此响应速度更快,并且能接受更多的请求
# 请求不使用memcache的php页面
[root@rs1 ~]# ab -n 500 -c 10 192.168.147.134/index.php
Time taken for tests: 0.829 seconds
Complete requests: 500
Failed requests: 55
# 请求用memcache的php页面
[root@rs1 ~]# ab -n 500 -c 10 192.168.147.134/example.php
Time taken for tests: 0.750 seconds
Complete requests: 500
Failed requests: 0
二、高速缓存系统
openresty
- 停止现有nginx
[root@server openresty-1.21.4.1]# systemctl stop nginx
- 可以使用官方软件源
https://openresty.org/package/centos/openresty.repo配置到yum
- 编译源码:下载地址 https://openresty.org/download/openresty-1.21.4.1.tar.gz
[root@server ~]# cd openresty-1.21.4.1/
[root@server openresty-1.21.4.1]# ll
total 104
drwxrwxr-x 46 alexw alexw 4096 May 15 2022 bundle
-rwxrwxr-x 1 alexw alexw 51290 May 15 2022 configure
-rw-rw-r-- 1 alexw alexw 22924 May 15 2022 COPYRIGHT
drwxrwxr-x 2 alexw alexw 4096 May 15 2022 patches
-rw-rw-r-- 1 alexw alexw 4689 May 15 2022 README.markdown
-rw-rw-r-- 1 alexw alexw 8974 May 15 2022 README-windows.txt
drwxrwxr-x 2 alexw alexw 52 May 15 2022 util
# 和nginx的配置一样
./configure --prefix=/usr/local/openresty --with-http_ssl_module --with-http_stub_status_module --with-threads
配置高速缓存
# 修改openresty的nginx配置
# 加入到http块
upstream memcache {
server 127.0.0.1:11211;
keepalive 512;
}
# 定义一个location
location /memc {
internal; # 表示只接受内部访问
memc_connect_timeout 100ms;
memc_send_timeout 100ms;
memc_read_timeout 100ms;
set $memc_key $query_string;
set $memc_exptime 300; # 缓存失效时间
memc_pass memcache;
}
# 修改原php location
location ~ \.php$ {
set $key $uri$args;
srcache_fetch GET /memc $key; # nginx查询是否有缓存,有的话直接返回缓存
srcache_store PUT /memc $key; # 没有缓存的话,去php取到结果后,加入到缓存
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
- 重启nginx,测试
[root@server nginx]# sbin/nginx -s reload
[root@rs1 ~]# ab -n 500 -c 10 192.168.147.134/example.php
Time taken for tests: 0.176 seconds
Complete requests: 500
Failed requests: 0
三、tomcat
- 针对jsp页面
安装open-jdk
[root@rs1 local]# yum install -y java-1.8.0-openjdk.x86_64
配置tomcat
- 创建软连接
[root@rs1 local]# ln -s apache-tomcat-7.0.37/ tomcat
- 测试
[root@rs1 bin]# ./startup.sh
Using CATALINA_BASE: /usr/local/apache-tomcat-7.0.37
Using CATALINA_HOME: /usr/local/apache-tomcat-7.0.37
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-7.0.37/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/apache-tomcat-7.0.37/bin/bootstrap.jar:/usr/local/apache-tomcat-7.0.37/bin/tomcat-juli.jar
[root@rs1 bin]# netstat -ano | grep :8080
tcp6 0 0 :::8080 :::* LISTEN off (0.00/0/0)
[root@rs1 bin]# curl -I localhost:8080
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=ISO-8859-1
Transfer-Encoding: chunked
Date: Thu, 12 Jan 2023 10:25:57 GMT
tomcat默认发布页面:/usr/local/tomcat/webapps/ROOT
整合nginx和tomcat
1台nginx+2台tomcat
- 修改nginx配置
# 创建location
# if page .jsp
location ~ \.jsp$ {
proxy_pass http://tomcat;
}
# 配置负载均衡
upstream tomcat {
server rs1:8080;
server rs2:8080;
}
四、mysql
- 安装依赖
cmake、gcc-c++、openssl-devel、ncurses-devel、libtirpc-devel - 源码安装依赖rpcsvc
# 下载地址:https://github.com/thkukuk/rpcsvc-proto/releases/download/v1.4.1/rpcsvc-proto-1.4.1.tar.xz
- 源码编译
[root@rs1 mysql-5.7.36]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DSYSCONFDIR=/etc -DENABLED_LOCAL_INFILE=1 -DWITH_EXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_unicode_ci -DWITH_BOOST=/root/mysql-5.7.36/boost/boost_1_59_0
[root@rs1 mysql-5.7.36]# make -j
[root@rs1 mysql-5.7.36]# make install
内存和硬盘大小要给多一些
- 拷贝启动脚本
[root@localhost mysql-5.7.36]# cd support-files/
[root@localhost support-files]# ls
build-tags CMakeLists.txt dtrace Makefile mysql-log-rotate mysql.server plugins.files
CMakeFiles compiler_warnings.supp MacOSX mysqld_multi.server mysql-log-rotate.sh mysql.server.sh
cmake_install.cmake CTestTestfile.cmake magic mysqld_multi.server.sh mysql.m4 mysql.server-sys5.sh
[root@localhost support-files]# cp mysql.server /etc/init.d/mysqld
[root@localhost support-files]# ll /etc/init.d/
total 56
-rw-r--r--. 1 root root 18281 Aug 24 2018 functions
-rw-r--r--. 1 root root 10566 Jan 13 04:46 mysqld
-rwxr-xr-x. 1 root root 4569 Aug 24 2018 netconsole
-rwxr-xr-x. 1 root root 7923 Aug 24 2018 network
-rw-r--r--. 1 root root 1160 Sep 7 2018 README
-rwxr-xr-x. 1 root root 2437 Oct 19 2017 rhnsd
- 创建mysql用户
[root@localhost support-files]# useradd -M -d /usr/local/mysql/ -s /sbin/nologin mysql
[root@localhost support-files]# mkdir -p /data/mysql
[root@localhost support-files]# chown mysql.mysql /data/mysql
[root@localhost support-files]# vim ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin
[root@localhost support-files]# source ~/.bash_profile
- 修改mysql配置
[root@localhost support-files]# cp /etc/my.cnf /etc/my.cnf.bak
[root@localhost support-files]# vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/data/mysql/mysql.sock
- 初始化数据库
[root@localhost mysql]# mysqld --initialize --user=mysql # 正常初始化
2023-01-13T12:58:25.992600Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2023-01-13T12:58:26.222387Z 0 [Warning] InnoDB: New log files created, LSN=45790
2023-01-13T12:58:26.251060Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2023-01-13T12:58:26.313516Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: f81c9a24-9341-11ed-b0de-000c299c98e4.
2023-01-13T12:58:26.314378Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2023-01-13T12:58:26.612690Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2023-01-13T12:58:26.612706Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2023-01-13T12:58:26.613182Z 0 [Warning] CA certificate ca.pem is self signed.
2023-01-13T12:58:26.625707Z 1 [Note] A temporary password is generated for root@localhost: fHjtc8hR< ## 会给一个随机初始化密码
### 启动MySQL服务
[root@localhost data]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/localhost.localdomain.err'.
SUCCESS!
[root@localhost data]# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
Error: Access denied for user 'root'@'localhost' (using password: YES)
[root@localhost data]# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
The existing password for the user account root has expired. Please set a new password.
New password:
Re-enter new password:
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: n
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n
... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : y
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? (Press y|Y for Yes, any other key for No) : y
Success.
By default, MySQL 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? (Press y|Y for Yes, any other key for No) : y
- 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? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
- 登录数据库
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.36 Source distribution
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
五、phpmyadmin
# 下载地址https://files.phpmyadmin.net/phpMyAdmin/5.0.2/phpMyAdmin-5.0.2-all-languages.tar.gz
[root@server nginx]# mv /root/phpMyAdmin-5.0.2-all-languages/ /usr/local/nginx/html/phpadmin
# 修改nginx.conf添加index.php
[root@server nginx]# vim conf/nginx.conf
location / {
root html;
index index.php index.html index.htm;
}
# 确保php-fpm开启
[root@server nginx]# netstat -antlp | grep :9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 250318/php-fpm: mas
tcp 0 0 127.0.0.1:9000 127.0.0.1:57568 TIME_WAIT -
tcp 0 0 127.0.0.1:9000 127.0.0.1:57566 TIME_WAIT -
tcp 0 0 127.0.0.1:9000 127.0.0.1:57564 TIME_WAIT -
六、数据库主从复制
- 拷贝编译完成的mysql目录到虚拟机2
[root@localhost ~]# scp -r /usr/local/mysql 192.168.147.138:/usr/local/
# 启动脚本也要拷
[root@localhost ~]# scp /etc/init.d/mysqld 192.168.147.138:/etc/init.d
-
切换到虚拟机2,执行上面的操作
-
虚拟机1上随意创建一些数据用于测试
mysql> select * from testT;
+----+-------+
| id | name |
+----+-------+
| 1 | alex |
| 2 | bob |
| 3 | candy |
| 4 | dan |
+----+-------+
- 使用mysqldump
# 虚拟机1上
[root@localhost ~]# mysqldump testDB > dump.sql -p
# 虚拟机2上,需要先创建testDB数据库
[root@localhost ~]# mysql testDB < dump.sql -p
- 修改虚拟机1上的mysql配置
[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-bin=mysql-bin # 启用二进制日志
server-id=1
# 重启mysqld
[root@localhost ~]# /etc/init.d/mysqld restart
# 创建用户专用于主从复制
mysql> create user 'repl'@'%' identified by 'passwd';
Query OK, 0 rows affected (0.00 sec)
# 授权
mysql> grant replication slave on *.* to 'repl'@'%';
Query OK, 0 rows affected (0.01 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 595 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
- 虚拟机2上
mysql> CHANGE MASTER TO MASTER_HOST='192.168.147.139', MASTER_USER='repl',MASTER_PASSWORD='passwd',MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=595;
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.147.139
Master_User: repli
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 1066
Relay_Log_File: lamp2-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1066
Relay_Log_Space: 527
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 02d9bff7-9345-11ed-a419-000c299c98e4
Master_Info_File: /data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
- 出现报错
Slave_IO_Running: No
Slave_SQL_Running: No
# 可以尝试重启slave
stop slave;
start slave;