基于第一篇的服务器环境的搭建,我们这篇进行了极大的拓展,让我们踩着巨人的肩膀继续前行!(感谢海哥提供技术支持)
1.ssh免重启
systemctl list-unit-files | grep sshd
systemctl enable sshd.service
但是下次还是发现不行了(尤其是在改了ssh端口号后),那么我们可以通过下面这篇文章来解决问题
http://blog.51cto.com/lorysun/1663567
2.nginx开机启动
https://www.cnblogs.com/piscesLoveCc/p/5867900.html
内容如下替换成自己的目录
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/sbin/nginx
ExecReload=/sbin/nginx -s reload
ExecStop=/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
加入开机启动
#systemctl enable nginx.service
3.禁止防火墙启动
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl is-enabled firewalld.service
4.自动启动mysql
systemctl stop mysqld #关闭MySQL
systemctl restart mysqld #重启MySQL
systemctl status mysqld #查看MySQL运行状态
systemctl enable mysqld #设置开机启动
systemctl disable mysqld #关闭开机启动
5.php连接数据库报错
Permission denied
https://blog.csdn.net/eclothy/article/details/48265445
可能是mysql.socks路径问题,自行找下文章,很容易设定要么设定php.ini,要么设定my.ini
sestatus
setenforce 0
如果要永久关闭,可以修改配置文件/etc/selinux/config,将SELINU置为disabled。
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
6.安装memcache
https://www.cnblogs.com/ThinkVenus/p/6853827.html
ps -ef|grep memcached
其他端口开启:/
usr/bin/memcached -b -l 127.0.0.1 -p 11311 -m 150 -u root
php扩展安装
https://blog.csdn.net/baidu_30000217/article/details/51494007
最后有可能报错:
Installing shared extensions: /usr/lib64/php/modules/
ll /usr/lib64/php/modules/
可以看到:memcached.so
php -m
必须要看到memcached和memcache
否则不成功请看下面文章
https://blog.csdn.net/doubleface999/article/details/55798750
php.ini增加:
extension=/usr/lib64/php/modules/memcache.so
extension=/usr/lib64/php/modules/memcached.so
7.安装及配置php与redis
https://www.cnblogs.com/haozhen/p/9851712.html
php测试
<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->auth('密码');
echo "Connection to server sucessfully";
echo "Server is running: " . $redis->ping();
$redis->set('key1',1123);//设置key=aa value=1 [true]
echo $redis->get('key1');//获取key [value]
?>
ps -ef|grep redis
设置自动启动:需要两篇文章配合着来
https://blog.csdn.net/chwshuang/article/details/68489968
https://www.cnblogs.com/wyy123/p/6141236.html
查看自动启动项目:
systemctl list-unit-files
8.nginx负载配置及测试
安装及基础配置nginx第一篇中有推荐。
下面开始负载的配置
模拟多台服务器那么就要安装tomcat测试
先安装JAVA+tomcat
JAVA:
自行下载安装包然后上传到服务器
https://blog.csdn.net/flyhaotian/article/details/82352932
tar xvf jdk-8u201-linux-x64.tar
cp -r jdk1.8.0_201/ /opt/java/
打开/root/.bashrc
增加以下内容在文件末尾:
export JAVA_HOME=/opt/java
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
运行
source ~/.bashrc
Tomcat:
自行下载tomcat从官网:
https://tomcat.apache.org/download-90.cgi
tar xvf apache-tomcat-9.0.14.tar
两篇文章交叉看
http://blog.51cto.com/13525470/2073657
https://blog.csdn.net/u010395496/article/details/79921903
修改端口:tomcat2的server.xml进行修改:搜索并修改8005–>8006 ; 8080 -->8090 ; 8009 --> 8010
分别启动tomcat
./bin/startup.sh
浏览器访问:ip:8080 ip:8090
如果可以分别访问到页面,那么可以修改如下看看每个页面的不同情况
/usr/local/tomcat/tomcat1/webapps/ROOT/index.jsp
继续配置看
https://blog.csdn.net/u010395496/article/details/79921903
http://blog.51cto.com/gaowenlong/1887997
worker_processes 1; #设置值和CPU核心数一致
如果想单纯的使用nginx负载,并需要本机虚拟机和其他虚拟机共同完成负载代理,那么本机应该设置
upstream www.z.com {
server 本机ip:8088 weight=1;
server 从域名:80 weight=1;
}
两个server的配置,一个负责转化www.z.com域名,一个负责本机的虚拟主机访问 ,域名访问请自行设置本机hosts文件
########start nginx.conf##########
#user nobody;
worker_processes auto;
error_log /var/log/nginx/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid /usr/sbin/nginx/nginx.pid;
pid /usr/local/nginx/logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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"';
#new
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#new 本机和另一个机器作为负载的机器,max_fails失败次数,fail_timeout达到失败次数后多少秒以后才能访问
upstream www.z.com {
server 主域名:8088 weight=1;
server 从域名:80 weight=1 max_fails=2 fail_timeout=30;
}
#开启另一个端口,作为实际访问本机网站的虚拟主机
server {
listen 8088;
server_name 域名;
root /var/www/html/自己的目录/public;
index index.php index.html ;
charset utf-8;
client_max_body_size 200m;
access_log /var/log/nginx/自己的日志名字.log main;
error_log /var/log/nginx/自己的日志名字-err.log;
location /h5 {
rewrite ^/h5(.+)$ /h5/app/webroot$1 break;
try_files $uri $uri/ /h5/app/webroot/index.php?$args;
fastcgi_connect_timeout 150;
fastcgi_read_timeout 600;
fastcgi_send_timeout 600;
}
location ~ \.php$ {
fastcgi_pass unix:/dev/shm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_connect_timeout 150;
fastcgi_read_timeout 600;
fastcgi_send_timeout 600;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
#负载专用
server {
listen 80;
server_name www.z.com;
location / {
proxy_pass http://www.z.com; #\u001a\u001atomcat\u001a\u001a
proxy_redirect off;#\u001a\u001a\u001a\u001a
proxy_set_header Host $host; #\u001a\u001a\u001a\u001a\u001a\u001ahost
proxy_set_header X-Real-IP $remote_addr;#\u001a\u001a\u001a\u001a\u001a\u001a\u001a \u001a\u001a\u001a\u001a\u001a\u001a\u001aheader\u001a\u001a\u001a\u001a\u001a\u001a\u001a\u001a\u001a
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90; #\u001a\u001a\u001a\u001a\u001a\u001a\u001a\u001a\u001a\u001a\u001a\u001a
proxy_send_timeout 90;#\u001a\u001a\u001a\u001a\u001a\u001a\u001a\u001a\u001a\u001a\u001a\u001a\u001a
proxy_read_timeout 90;#\u001a\u001a\u001a\u001a\u001a\u001a
proxy_buffer_size 4k; # \u001a\u001a\u001a\u001a\u001a\u001a
proxy_buffers 4 32k; #
proxy_busy_buffers_size 64k; # #proxy_buffers\u001a\u001a\u001a\u001a\u001a\u001a\u001a\u001a\u001a32k\u001a\u001a\u001a
proxy_temp_file_write_size 64k; ##\u001a\u001a\u001a\u001a\u001a\u001a\u001a\u001a\u001aproxy_buffers*2\u001a
}
}
}
########end##########
如果浏览器每次刷新看不出来,请清理历史记录,或者新开窗口,或者通过命令行查看并访问
还得加一个能自动屏蔽坏IP的,自动发现访问不了的机器,然后不再访问
http://blog.51cto.com/gaowenlong/1887997 下方
9.数据库主从配置
理论:https://blog.csdn.net/starlh35/article/details/78735510
通过设置主从数据库实现读写分离,主数据库负责“写操作”,从数据库负责“读操作”,
根据压力情况,从数据库可以部署多个提高“读”的速度,借此来提高系统总体的性能。
systemctl stop mysqld #关闭MySQL
systemctl restart mysqld #重启MySQL
systemctl status mysqld #查看MySQL运行状态
主数据库:
CREATE USER '用户名'@'localhost' IDENTIFIED BY '密码';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'从域名' IDENTIFIED BY '密码';
flush privileges;
从数据库:
change master to master_host='主域名',master_port=3306,master_user='主用户名',master_password='密码',master_log_file='master-bin.000001',master_log_pos=0;
start slave;
其他:
stop slave;停止从
show slave status \G; 查看状态
PHP实现读写分离代码
https://www.cnblogs.com/we-jack/p/8204457.html
使用mysql进行分配ip访问权限
GRANT ALL PRIVILEGES ON *.* TO 'happy'@'自己的域名' IDENTIFIED BY '密码' WITH GRANT OPTION;
FLUSH PRIVILEGES;
重启mysql服务
/对于PhalApi接口框架主从操作/
临时解决方案:
1.创建dbss.php复制dbs.php。并填写好从数据库信息
2.di.php增加从数据库访问对象:
d
i
−
>
n
o
t
o
r
m
S
=
n
e
w
N
o
t
O
R
M
D
a
t
a
b
a
s
e
(
di->notormS = new NotORMDatabase(
di−>notormS=newNotORMDatabase(di->config->get(‘dbss’), $di->debug);//从数据库,读取使用
3.model层代码增加两个保护类,分别调用主或者从
//从数据库调用方法,zyh 这个保护类,是调用从数据库进行查询,配套的文件修改有新建从数据库配置dbss.php,增加从数据库配置di.php
protected function getORMs($id = NULL) {
$table = $this->getTableName($id);
return \PhalApi\DI()->notormS->$table;
}
//主数据库调用方法
protected function getORM($id = NULL) {
$table = $this->getTableName($id);
return \PhalApi\DI()->notorm->$table;
}
10.会话保持
原理:http://www.cnblogs.com/wajika/p/6645581.html
php redis session会话保持
http://blog.51cto.com/13447608/2295961
php打印错误信息
ini_set('display_errors',1); //错误信息
ini_set('display_startup_errors',1); //php启动错误信息
error_reporting(-1); //打印出所有的 错误信息
可以使用phpredisadmin可视化管理工具
但是需要安装predis https://blog.csdn.net/u013378306/article/details/52074304
然后放到phpredisadmin的目录下命名vendor
Warning: require(): It is not safe to rely on the system’s timezone settings
修改php.ini时区
或者php代码加入:date_default_timezone_set(“UTC”);
phpredisadmin下的配置文件config.inc.php中
'servers' => array(
array(
'name' => 'local server', // Optional name.
'host' => '127.0.0.1',
'port' => 6379,
'filter' => '*',
'scheme' => 'tcp', // Optional. Connection scheme. 'tcp' - for TCP connection, 'unix' - for connection by unix domain socket
'path' => '', // Optional. Path to unix domain socket. Uses only if 'scheme' => 'unix'. Example: '/var/run/redis/redis.sock'
// Optional Redis authentication.
'auth' => '密码' // Warning: The password is sent in plain-text to the Redis server.
),
找到redis配置文件:/usr/local/redis/redis.conf
找到bind 127.0.0.1并注释掉
修改 protected-mode 属性值为no
注:redis默认是只能本地访问,注释掉并叫保护模式禁用以后可以IP访问
systemctl start redis 重启服务
php的会话保持有多种方式:建议使用如下两种方式
http://blog.51cto.com/13447608/2295961
1.修改php.ini和www.conf
2.直接加入php代码
ini_set("session.save_handler","redis");
ini_set("session.save_path","tcp://172.16.1.51:6379");
//如果redis设置了密码需要改下面一句为
//ini_set("session.save_path","tcp://172.16.1.51:6379?auth=123123");
//如果session.auto = 1就不需要session_start();
最后我要感谢上述提及的文章中各位大牛作者的贡献,我只是一个站在居然肩膀上前进的小白,我只是把大家各个分支做了汇总,提及文章的版权均为各位作者所有!