1. 程式人生 > 其它 >Linux(centos) 搭建PHP伺服器

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擴充套件庫
  1. yum install libevent-devel -y
  2. pecl install event
  3. echo extension=event.so > /etc/php.d/30-event.ini
  4. systemctl enable php-fpm.service
  5. systemctl start php-fpm.service

4. 安裝nginx

  1. wget http://tengine.taobao.org/download/tengine-2.2.1.tar.gz
  2. tar -zxf tengine-2.2.1.tar.gz
  3. cd tengine-2.2.1
檢查nginx編繹檔案
  1. ./configure
  2. make
  3. make install
啟動nginx
  1. /usr/local/nginx/sbin/nginx
  2. /usr/local/nginx/sbin/nginx -s reload
  3. //啟動php服務 查詢php安裝目錄
  4. whereis php
  5. service php-fpm restart

5. 安裝mysql

方法一

下載mysql
  1. wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
安裝mysql rpm(安裝,更新)
  1. rpm -ivh mysql-community-release-el7-5.noarch.rpm
  2. yum install mysql-community-server
  3. systemctl start mysqld.service
  4. //設定mysql服務開機自啟動
  5. systemctl enable mysqld.service
新增帳號及密碼
  1. mysqladmin -u root password '123456'
通過登入mysql系統
  1. mysql -uroot -p
  2. Enter password: 【輸入原來的密碼】
  3. mysql>use mysql;
  4. mysql> update user set password=passworD("新密碼") where user='root';
  5. mysql> flush privileges;
  6. mysql> exit;
授權法(遠端登入)
  1. mysql -uroot -p
  2. Enter password: 【輸入原來的密碼】
  3. GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '[你的密碼]' WITH GRANT OPTION;
  4. FLUSH PRIVILEGES;

方法二

安裝mariadb,代替mysql
  1. yum install mariadb-server.x86_64
  2. systemctl enable mariadb.service
  3. systemctl start mariadb.service
  4. mysql -uroot -p
  5. #首次密碼空
  6. grant all privileges on *.* to 'root'@'%' identified by 'password';
  7. flush privileges;

6.允許ssh密碼登入

  1. # vi /etc/ssh/sshd_config
  2. 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
新增埠
  1. firewall-cmd --zone=public --add-port=10000/tcp --permanent
  2. firewall-cmd --zone=public --add-port=8282/tcp --permanent
  3. firewall-cmd --zone=public --add-port=3306/tcp --permanent
  4. 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,如下圖所示。
  1. centos出現“FirewallD is not running”怎麼辦
  2. 通過systemctl status firewalld檢視firewalld狀態,發現當前是dead狀態,即防火牆未開啟。
  3. centos出現“FirewallD is not running”怎麼辦
  4. 通過systemctl start firewalld開啟防火牆,沒有任何提示即開啟成功。
  5. centos出現“FirewallD is not running”怎麼辦
  6. 再次通過systemctl status firewalld檢視firewalld狀態,顯示running即已開啟了。
  7. 再執行新增埠命令
  8. centos出現“FirewallD is not running”怎麼辦
  9. 5
  10. 如果要關閉防火牆設定,可能通過systemctl stop firewalld這條指令來關閉該功能。
  11. centos出現“FirewallD is not running”怎麼辦
  12. 6
  13. 再次執行執行firewall-cmd --permanent --zone=public --add-port=3306/tcp,提示success,表示設定成功,這樣就可以繼續後面的設定了。
  14. centos出現“FirewallD is not running”怎麼辦
  15. END
全部執行完畢後

關閉防火牆設定

  1. systemctl stop firewalld
檢視
  1. firewall-cmd --list-ports
Ubuntu 16.04 apach2 php7 mysql
  1. 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
  2. sudo apt install apache2
  1. cd /etc/apache2/mods-enabled
  2. ln -sf ../mods-available/rewrite.load rewrite.load
  3. vi /etc/apche2/apache2.conf
修改為以下部分為:
  1. <Directory />
  2. Options FollowSymLinks
  3. AllowOverride All
  4. Require all granted
  5. </Directory>
  6. <Directory /usr/share>
  7. AllowOverride None
  8. Require all granted
  9. </Directory>
  10. <Directory /var/www/>
  11. Options Indexes FollowSymLinks
  12. AllowOverride All
  13. Require all granted
  14. </Directory>

9.連線MySql,報host is not allowed to connect mysql

  1. 1、在裝有MySQL的機器上登入 mysql -u root -p 不輸入密碼直接回車
  2. 2、執行use mysql;
  3. 3、執行update user set host = '%' where user = 'root';這一句執行完可能會報錯,不用 管它。
  4. 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.新開起一個終端輸入

  1. mysql -u root mysql
  2. mysql> delete from user where USER='';
  3. mysql> FLUSH PRIVILEGES;//記得要這句話,否則如果關閉先前的終端,又會出現原來的錯誤
  4. 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、執行資料庫檔案
  1. source /usr/local/crazy/file/test.sql
  2. show tables; --顯示該資料庫中的所有表

注:需先把sql檔案放到伺服器的一個資料夾中,不能有中文目錄,否則執行不了

  1. drop database 資料庫名; --刪除資料庫mydatabase
  2. exit; 退出

10.替換usr/local/nginx/conf 資料夾,並且替換相應檔案

如果部署多個專案,把下列檔案複製,修改伺服器埠,專案路徑

新增埠
  1. firewall-cmd --zone=public --add-port=9990/tcp --permanent
  2. systemctl restart firewalld.service
  1. server {
  2. #替換伺服器埠
  3. listen 10000;
  4. #include /usr/local/nginx/conf/port.conf;
  5. server_name 127.0.0.1;
  6. charset utf8;
  7. location / {
  8. #替換專案路徑
  9. root /html/bbx;
  10. index index.htm index.html index.php;
  11. #如果檔案不存在則嘗試TP解析
  12. try_files $uri /index.php$uri;
  13. if (!-e $request_filename) {
  14. rewrite ^/index.php(.*)$ /index.php?s=$1 last;
  15. rewrite ^(.*)$ /index.php?s=$1 last;
  16. break;
  17. }
  18. }
  19. location ~ .+\.php($|/) {
  20. #替換專案路徑
  21. root /html/bbx;
  22. fastcgi_pass 127.0.0.1:9000;
  23. fastcgi_index index.php;
  24. fastcgi_split_path_info ^(.+\.php)(/.*)$;
  25. fastcgi_param PATH_INFO $fastcgi_path_info;
  26. include fastcgi.conf;
  27. }
  28. }

訪問專案如果報:Think/Storage/Driver/File.class.php

進入專案目錄執行命令

  1. chmod -R 777 runtime
虛擬機器IP地址與本機IP地址不一致,右鍵—— 虛擬機器名稱 ——網路介面卡——橋接模式

11.部署Workerman長連線

把workerman資料夾放到opt資料夾中

(建立applog/socket 日誌資料夾)

修改bbx\Applications\YourApp\Event.php檔案,資料庫引數
  1. public static function onWorkerStart($worker)
  2. {
  3. self::$db = new Workerman\MySQL\Connection('127.0.0.1', '3306', 'root', 'BbX#_e82lc', 'bbx');
  4. }
  5. }

修改埠(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
解壓檔案:
  1. unzip 檔名
  2. 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、備份資料庫
  1. mkdir home/mysql_backup
  2. touch mysql_backup.sh #建立sh指令碼檔案
  3. chmod 770 mysql_backup.sh #授權
使用VI工具編寫test_database_backup.sh備份資料庫指令碼內容
  1. 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
  2. find /home/mysql_backup/ -ctime +1 -name "*.gz" -exec rm -rf {} \; #刪除7天前資料檔案
  3. /sbin/service crond stop #停止服務
  4. /sbin/service crond restart #重啟服務
  5. /sbin/service crond reload #重新載入配置
  6. crontab -e #新增定時器
  7. 0 0 * * * /bin/bash /home/mysql_backup/mysql_backup.sh #0小時執行資料庫備份
  8. 0 8 * * * /bin/bash /root/mysql_backup/mysql_backup.sh #8小時執行資料庫備份
  9. 0 16 * * * /bin/bash /root/mysql_backup/mysql_backup.sh #16小時執行資料庫備份
  10. # 每分鐘釋放一次暫存的櫃子
  11. * * * * * /usr/bin/curl "http://box.bbxtek.com/api/order/device_init" >> /root/log/device_init.txt
  12. * * * * * curl http://box.bbxtek.com/api/order/automation
  13. * * * * * sleep 10; curl http://xxxxxxx/api/order/automation
  14. * * * * * sleep 20; curl http://xxxxxxx/api/order/automation
  15. * * * * * sleep 30; curl http://xxxxxxx/api/order/automation
  16. * * * * * sleep 40; curl http://xxxxxxx/api/order/automation
  17. * * * * * sleep 50; curl http://xxxxxxx/api/order/automation
  18. /sbin/service crond start #啟動服務


來自為知筆記(Wiz)