Linux(centos) 搭建PHP服务器

Centos7 nginx mysql php

切换root帐户

  1. su

1、查看版本号

  1. cat /etc/redhat-release

2、安装工具

  1. yum install -y make apr* autoconf cmake automake curl-devel gcc gcc-c++ openssl openssl-devel pcre-devel gd kernel keyutils perl kernel-headers mpfr cpp glibc libgomp libstdc++-devel ppl cloog-ppl keyutils-libs-devel libcom_err-devel libsepol-devel libselinux-devel krb5-devel zlib-devel libXpm* freetype libjpeg* libpng* ncurses* libtool* libxml2 libxml2-devel patch bzip2-devel telnet jasper-devel libtiff-devel gtk+-devel wget

3. 安装php

  1. yum install -y php php-common php-devel php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash php-fpm php-json* php-soap php-process
安装zip解压软件
  1. yum install unzip
查询php 版本号
  1. php -v
查询--ini安装目录
  1. php --ini
查询workman支持的php版本
  1. curl -Ss http://www.workerman.net/check.php | php
安装php扩展库
yum install libevent-devel -y
pecl install event
echo extension=event.so > /etc/php.d/30-event.ini
systemctl enable php-fpm.service
systemctl start php-fpm.service

4. 安装nginx

wget http://tengine.taobao.org/download/tengine-2.2.1.tar.gz
tar -zxf tengine-2.2.1.tar.gz 
cd tengine-2.2.1
检查nginx编绎文件
./configure 
make
make install
启动nginx
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -s reload
//启动php服务 查询php安装目录
whereis php 
service php-fpm restart    

5. 安装mysql

方法一

下载mysql
  1. wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
安装mysql rpm(安装,更新)
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-community-server
systemctl start mysqld.service
//设置mysql服务开机自启动
systemctl enable mysqld.service
添加帐号及密码
  1. mysqladmin -u root password '123456'
通过登录mysql系统
mysql -uroot -p
Enter password: 【输入原来的密码】
mysql>use mysql;
mysql> update user set password=passworD("新密码") where user='root';
mysql> flush privileges;
mysql> exit;
授权法(远程登录)
mysql -uroot -p
Enter password: 【输入原来的密码】

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '[你的密码]' WITH GRANT OPTION;

FLUSH   PRIVILEGES;

方法二

安装mariadb,代替mysql
yum install mariadb-server.x86_64
systemctl enable mariadb.service 
systemctl start mariadb.service


mysql -uroot -p
#首次密码空
grant all privileges on *.* to 'root'@'%' identified by 'password';
flush privileges;

6.允许ssh密码登录

# vi /etc/ssh/sshd_config
systemctl restart sshd.service
7. 关闭selinux
  1. vim /etc/selinux/config
修改为:
  1. SELINUX=disabled

8. 防火墙修改

开机启用
  1. systemctl enable firewalld.service
重启
  1. systemctl restart firewalld.service
开启防火墙
  1. systemctl start firewalld
添加端口
firewall-cmd --zone=public --add-port=10000/tcp --permanent
firewall-cmd --zone=public --add-port=8282/tcp --permanent
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --zone=public --add-port=9990/tcp --permanent

centos出现“FirewallD is not running”怎么办

执行firewall-cmd --permanent --zone=public --add-port=3306/tcp,提示FirewallD is not running,如下图所示。
centos出现“FirewallD is not running”怎么办

通过systemctl status firewalld查看firewalld状态,发现当前是dead状态,即防火墙未开启。

centos出现“FirewallD is not running”怎么办

通过systemctl start firewalld开启防火墙,没有任何提示即开启成功。

centos出现“FirewallD is not running”怎么办

再次通过systemctl status firewalld查看firewalld状态,显示running即已开启了。


再执行添加端口命令


centos出现“FirewallD is not running”怎么办
5
如果要关闭防火墙设置,可能通过systemctl stop firewalld这条指令来关闭该功能。

centos出现“FirewallD is not running”怎么办
6
再次执行执行firewall-cmd --permanent --zone=public --add-port=3306/tcp,提示success,表示设置成功,这样就可以继续后面的设置了。

centos出现“FirewallD is not running”怎么办
END
全部执行完毕后

关闭防火墙设置

  1. systemctl stop firewalld
查看
  1. firewall-cmd --list-ports
Ubuntu 16.04 apach2 php7 mysql
sudo apt install php php-dev php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mcrypt libmcrypt4 libmcrypt-dev php-fpm php-json php-json-patch php-json-schema php-soap php-curl
sudo apt install apache2
cd /etc/apache2/mods-enabled
ln -sf ../mods-available/rewrite.load rewrite.load
vi /etc/apche2/apache2.conf
修改为以下部分为:
<Directory />
        Options FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

9.连接MySql,报host is not allowed to connect mysql

1、在装有MySQL的机器上登录 mysql -u root -p   不输入密码直接回车
2、执行use mysql;
3、执行update user set host = '%' where user = 'root';这一句执行完可能会报错,不用        管它。
4、执行FLUSH PRIVILEGES;
提示:ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'
1.关闭mysql
service mysqld stop

2.屏蔽权限

  1. mysqld_safe --skip-grant-table

屏幕出现: Starting demo from .....

3.新开起一个终端输入

mysql -u root mysql
mysql> delete from user where USER='';
mysql> FLUSH PRIVILEGES;//记得要这句话,否则如果关闭先前的终端,又会出现原来的错误
mysql> exit;

执行sql文件

1、连接数据库
  1. mysql -uroot -p123456 -h127.0.0.1

-h,指定ip地址,默认为localhost

-u,指定用户名

-p,指定密码

2、新建数据库

create database 数据库名;
show databases; --显示所有数据库列表

3、选择数据库
  1. use 数据库名;
4、执行数据库文件
source /usr/local/crazy/file/test.sql
show tables;        --显示该数据库中的所有表

注:需先把sql文件放到服务器的一个文件夹中,不能有中文目录,否则执行不了

drop database 数据库名;     --删除数据库mydatabase

exit; 退出

10.替换usr/local/nginx/conf 文件夹,并且替换相应文件

如果部署多个项目,把下列文件复制,修改服务器端口,项目路径

添加端口
firewall-cmd --zone=public --add-port=9990/tcp --permanent
systemctl restart firewalld.service
server {
        #替换服务器端口
        listen 10000;
        #include /usr/local/nginx/conf/port.conf;
        server_name  127.0.0.1;
        charset utf8;
        location / {
                    #替换项目路径
            root    /html/bbx;
            index  index.htm index.html index.php;
            #如果文件不存在则尝试TP解析
            try_files  $uri  /index.php$uri;

            if (!-e $request_filename) {
                    rewrite ^/index.php(.*)$ /index.php?s=$1 last;
                    rewrite ^(.*)$ /index.php?s=$1 last;
                    break;
            }
        }
        location ~ .+\.php($|/) {
                    #替换项目路径
            root    /html/bbx;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^(.+\.php)(/.*)$;
            fastcgi_param  PATH_INFO $fastcgi_path_info;
            include        fastcgi.conf;
        }
    }

访问项目如果报:Think/Storage/Driver/File.class.php

进入项目目录执行命令

  1. chmod -R 777 runtime
虚拟机IP地址与本机IP地址不一致,右键—— 虚拟机名称 ——网络适配器——桥接模式

11.部署Workerman长连接

把workerman文件夹放到opt文件夹中

(创建applog/socket 日志文件夹)

修改bbx\Applications\YourApp\Event.php文件,数据库参数
public static function onWorkerStart($worker)
    {
        self::$db = new Workerman\MySQL\Connection('127.0.0.1', '3306', 'root', 'BbX#_e82lc', 'bbx');
    }
}


修改端口(start_gateway.php文件)

  1. $gateway = new Gateway("tcp://0.0.0.0:8282");

启动
以debug(调试)方式启动

  1. php start.php start

以daemon(守护进程)方式启动

  1. php start.php start -d

停止

  1. php start.php stop
停止失败,杀进程

查询所有占用端口

  1. netstat -tnlp

杀掉进程

  1. kill -9 进程ID

重启

  1. php start.php restart

平滑重启

  1. php start.php reload

查看状态

  1. php start.php status

查看连接状态(需要Workerman版本>=3.5.0)

  1. php start.php connections
解压文件:
unzip  文件名
tar zxvf FileName.tar.gz
压缩文件:
  1. tar zcvf 压缩后文件名.tar.gz 源文件
导入sql数
1、SSH登录LINUX系统,登录mysql数据库
  1. #mysql -u root -p #-p为密码,输入登录mysql的账户的密码
2、选择数据库
  1. #mysql>use database #database为你的的数据库
3、选择你已经备份好的sql文件,开始导入
  1. #mysql>source /backup.sql #选择对应路径即可恢复
4、备份数据库
mkdir home/mysql_backup  
touch mysql_backup.sh    #创建sh脚本文件
chmod 770 mysql_backup.sh   #授权
使用VI工具编写test_database_backup.sh备份数据库脚本内容
mysqldump -uroot(用户名) -p(数据库密码) --default-character-set=utf8  --opt -R --triggers  --hex-blob newgz(数据库名称) |gzip -9 > /home/mysql_backup/DatabaseName_$(date +%Y%m%d_%H%M%S).sql.gz

find /home/mysql_backup/ -ctime +1 -name "*.gz" -exec rm -rf {} \;   #删除7天前数据文件


/sbin/service crond stop   #停止服务

/sbin/service crond restart #重启服务  
/sbin/service crond reload #重新载入配置


crontab -e    #添加定时器

0  0 * * * /bin/bash /home/mysql_backup/mysql_backup.sh     #0小时执行数据库备份
0  8 * * * /bin/bash /root/mysql_backup/mysql_backup.sh     #8小时执行数据库备份
0 16 * * * /bin/bash /root/mysql_backup/mysql_backup.sh     #16小时执行数据库备份

# 每分钟释放一次暂存的柜子
* * * * * /usr/bin/curl "http://box.bbxtek.com/api/order/device_init" >> /root/log/device_init.txt

* * * * * curl http://box.bbxtek.com/api/order/automation
* * * * * sleep 10; curl http://xxxxxxx/api/order/automation
* * * * * sleep 20; curl http://xxxxxxx/api/order/automation
* * * * * sleep 30; curl http://xxxxxxx/api/order/automation
* * * * * sleep 40; curl http://xxxxxxx/api/order/automation
* * * * * sleep 50; curl http://xxxxxxx/api/order/automation


/sbin/service crond start    #启动服务 




posted @   挽风说笑  阅读(566)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示