Linux(centos) 搭建PHP伺服器
阿新 • • 發佈:2022-05-13
Centos7 nginx mysql php
切換root帳戶
su
1、檢視版本號
cat /etc/redhat-release
2、安裝工具
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
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解壓軟體
yum install unzip
查詢php 版本號
php -v
查詢--ini安裝目錄
php --ini
查詢workman支援的php版本
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
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
新增帳號及密碼
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
vim /etc/selinux/config
修改為:
SELINUX=disabled
8. 防火牆修改
開機啟用
systemctl enable firewalld.service
重啟
systemctl restart firewalld.service
開啟防火牆
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
全部執行完畢後
關閉防火牆設定
systemctl stop firewalld
檢視
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.遮蔽許可權
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、連線資料庫
mysql -uroot -p123456 -h127.0.0.1
-h,指定ip地址,預設為localhost
-u,指定使用者名稱
-p,指定密碼
2、新建資料庫
create database 資料庫名;
show databases; --顯示所有資料庫列表
3、選擇資料庫
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
進入專案目錄執行命令
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檔案)
$gateway = new Gateway("tcp://0.0.0.0:8282");
啟動
以debug(除錯)方式啟動
php start.php start
以daemon(守護程序)方式啟動
php start.php start -d
停止
php start.php stop
停止失敗,殺程序
查詢所有佔用埠
netstat -tnlp
殺掉程序
kill -9 程序ID
重啟
php start.php restart
平滑重啟
php start.php reload
檢視狀態
php start.php status
檢視連線狀態(需要Workerman版本>=3.5.0)
php start.php connections
解壓檔案:
unzip 檔名
tar zxvf FileName.tar.gz
壓縮檔案:
tar zcvf 壓縮後文件名.tar.gz 原始檔
匯入sql數
1、SSH登入LINUX系統,登入mysql資料庫
#mysql -u root -p #-p為密碼,輸入登入mysql的賬戶的密碼
2、選擇資料庫
#mysql>use database #database為你的的資料庫
3、選擇你已經備份好的sql檔案,開始匯入
#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 #啟動服務
來自為知筆記(Wiz)