php精簡完全小結(linux/laravel篇)

來源:互聯網
上載者:User

標籤:apach   clu   archive   var_dump   htm   from   global   height   /usr   

php官網:http://www.php.net
php版本:

查看:php -version
說明:None-Thread Safe就是非安全執行緒,在執行時不進行線程(thread)安全檢查;Thread Safe就是安全執行緒,執行時會進行線程(thread)安全檢查,以防止有新要求就啟動新線程的 CGI 執行方式耗盡系統資源。
再來看PHP的兩種執行方式:ISAPI和FastCGI。FastCGI執行方式是以單一線程來執行操作,所以不需要進行線程的安全檢查,除去安全執行緒檢查的防護反而可以提高執行效率,所以,如果是以 FastCGI(無論搭配 IIS 6 或 IIS 7)執行 PHP ,都建議下載、執行 non-thread safe 的 PHP (PHP 的二進位檔有兩種封裝方式:msi 、zip ,請下載 zip 套件)。而安全執行緒檢查正是為ISAPI方式的PHP準備的,因為有許多php模組都不是安全執行緒的,所以需要使用Thread Safe的PHP。

php下載:

http://www.php.net/downloads.php;http://windows.php.net/download#php-7.1

php常用的擴充:

php-redis:下載URLhttps://pecl.php.net/package/redis

1、安裝redis
下載:https://github.com/nicolasff/phpredis/archive/2.2.4.tar.gz
上傳phpredis-2.2.4.tar.gz到/usr/local/src目錄

cd /usr/local/src #進入軟體包存放目錄
tar zxvf phpredis-2.2.4.tar.gz #解壓
cd phpredis-2.2.4 #進入安裝目錄
/usr/local/php/bin/phpize #用phpize產生configure設定檔
./configure –with-php-config=/usr/local/php/bin/php-config #配置
make #編譯
make install #安裝
安裝完成之後,出現下面的安裝路徑
/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
2、配置php支援

vi /usr/local/php/etc/php.ini #編輯設定檔,在最後一行添加以下內容
添加

extension=”redis.so”

:wq! #儲存退出
3 重啟服務

sudo service nginx restart
sudo /etc/init.d/php-fpm restart

 

php配置:
php.ini,在很多的一件安裝環境裡,php還回載入php.d裡的設定檔,通常那是php的擴充檔案。

php升級的方法:

下載新的版本,備份舊的php版本的目錄,用新的版本整個目錄覆蓋舊版本目錄,同時保留舊版本目錄的php設定檔(php.cnf,php.ini),測試回合你的網站;

php變數定義:

$a;//定義變數$a

$a=”hello world!”;//定義變數並賦值

陣列變數

$arr1=[“a”,”b”,”c”,”c”];//數組定義

$arr1[0]=”s”;

$arr2=array(“a”,”b”,”c”,”c”])//數組定義

索引值對數組

$arr=[“key1″=>”value1″,”key2″=>”value2”];

數組編曆,foreach速度最快:
$urls= array(‘aaa’,’bbb’,’ccc’,’ddd’);
foreach ($urls as $url){
echo “This Site url is $url! <br />”;
}
索引值對數組遍曆:
foreach($arr as$key=> $val){

echo $key.”=”.$val;
}

變數空值檢查:

總結PHP中,”NULL” 和 “空” 是2個概念。

isset  主要用來判斷變數是否被初始化過
empty  可以將值為 “假”、”空”、”0″、”NULL”、”未初始化” 的變數都判斷為TRUE
is_null  僅把值為 “NULL” 的變數判斷為TRUE
var == null  把值為 “假”、”空”、”0″、”NULL” 的變數都判斷為TRUE
var === null  僅把值為 “NULL” 的變數判斷為TRUE

所以我們在判斷一個變數是否真正為”NULL”時,大多使用 is_null,從而避免”false”、”0″等值的幹擾

變數的查看:

var_dump(變數);print_r(變數)可以列印出複雜類型變數的值(如數組,對象)

php的運行環境:
一般有apache和nginx,建議使用nginx

php的nginx配置範例:

######################## default ############################
server {
listen 80;
server_name _;
access_log /data/wwwlogs/access_nginx.log combined;
root /data/wwwroot/default;
index index.html index.htm index.php;
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
location ~ [^/]\.php(/|$) {
#fastcgi_pass remote_php_ip:9000;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
}
location ~ /\.ht {
deny all;
}
}

#################### vhost #############################

CI架構的nginx配置範例:

listen 80;
server_name www.xxx.com;
root /disk2/home/www;
index index.html index.php;
access_log /disk2/log/www.log;

location / {
index index.html index.php;
try_files $uri $uri/ /index.php?uri&args;
}

location ~ /.well-known {
allow all;
}

location ~* \.(ini|docx|doc|pem)$ {
deny all;
}

location /phpfpm_status {
# fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
}

location ~ .*\.(php|php5)?$ {
#fastcgi_pass 127.0.0.1:9002;#windows

# fastcgi_pass unix:/dev/shm/php-fpm-index.sock;
# fastcgi_index index.php;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
# fastcgi_param DOCUMENT_ROOT /home/wwwroot/index$subdomain;
# fastcgi_param SCRIPT_FILENAME /home/wwwroot/index$subdomain$fastcgi_script_name;
# include fastcgi_params
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH-TARNSLATED /disk2/home/www$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME /disk2/home/www$fastcgi_script_name;
include fastcgi_params;
}

php的web開發架構:
yii,CI(codeigniter),laravel,symfony,thinkphp
建議選laravel。

php的資訊查看:
在你的網站的根目錄建立phpinfo.php:
<?php
echo phpinfo();//輸出php
?>

php的$_GET,$_POST,$_SESSION,$_COOKIE

 

 

 

composer包管理器:
linux安裝Composer
1、將composer.phar下載到項目中
使用curl -sS https://getcomposer.org/installer | php下載Composer 的二進位檔案,是一個 PHAR 包(PHP 的歸檔)
2、可以通過 –install-dir 選項指定 Composer 的安裝目錄(它可以是一個絕對或相對路徑):curl -sS https://getcomposer.org/installer | php — –install-dir=lumen
3、如果把composer.phar放在系統的 PATH 目錄中,就能在全域訪問composer.phar。 在類Unix系統中,你甚至可以在使用時不加 php 首碼。可以執行這些命令讓 composer 在你的系統中進行全域調用:
#mv composer.phar /usr/local/bin/composer
現在只需要運行 composer 命令就可以使用 Composer 而不需要輸入 php composer.phar。
4、檢查 Composer 是否正常工作,只需要通過 php 來執行 PHAR:php composer.phar這將返回給你一個可執行檔命令列表

linux 下的laravel安裝:
composer global require “laravel/installer”
或者composer create-project laravel/laravel –prefer-dist yourprojectname
對了,你不能用root使用者執行這個命令,你要useradd新建立一個使用者,然後用這個使用者登入,在執行這個命令!
提示,這個安裝一開始沒有反應,你好耐性等待。。。保持安裝電腦的網路暢通,知道安裝完成;
安裝完成後,用ls -a ~/,也就是說,實際上產生了使用者根目錄下的隱藏檔案夾:如home/webuser/.config/composer/vendor/bin(webuser為目前使用者);
添加環境變數
vim /etc/profile

添加如下的內容
PATH=$PATH:~/.config/composer/vendor/bin
PATH=$PATH:~/.composer/vendor/bin
儲存esc鍵,輸入wq!
使檔案生效:source /etc/profile
用laravel建立項目之前,修改php.ini配置:
在php.ini裡尋找“disable_functions”,把 proc_get_status,proc_open刪除

用laravel建立項目:
命令:
laravel new web3
等待下載安裝完成即可!
產生的web3專案檔在路徑~/.config/composer/vendor/laravel/下,注意”~/”是使用者的根目錄,你如果不是用安裝laravel的使用者帳號登入,那就是”/home/[laravel安裝使用者名稱]/”,
把它拷貝到/home/web/web3
然後配置nginx.conf範例:

server {
listen 8011;
server_name 192.168.0.10;
set $root_path ‘/home/web/web3/public’;
root $root_path;
index index.php index.html index.htm;

access_log logs/web3-access.log main;
error_log logs/web3-error.log;

try_files $uri $uri/ @rewrite;

# location @rewrite {
# rewrite ^/(.*)$ /index.php?_url=/$1;
# }

location ~ \.php {

# fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/dev/shm/php-fpm-index.sock;
fastcgi_index /index.php;

fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH-TARNSLATED $root_path$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $root_path$fastcgi_script_name;
#配置下面資訊,使網站能夠訪問響應目錄,防止出現“open_basedir”的相關錯誤
fastcgi_param PHP_ADMIN_VALUE open_basedir=$document_root/:$root_path:/home/web/web3/;
include fastcgi_params;

}

location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
root $root_path;
location ~ /\.ht {
deny all;
}
}
}

用laravel5.4寫個welcome頁面:
首先建立路由:/routes/web.php添加如下代碼:
Route::get(‘/welcome’,’[email protected]’);
建立路由器:php artisan make:controller WelcomeController
然後修改:/app/Http/Controllers/WelcomeController.php

public function index()
{

$str = “Hello , this is luxing first laravel page! Welcome!” ;//最上層顯示
# echo $str;
return view(‘welcome.index’,compact(‘str’));

}
然後建立view,/resources/views/welcome/index.blade.php,內容:

<!doctype html>
<html>
<head>
<title>welcome</title>
<body>

<p style=”width:100%;height:100%; text-align:center; padding-top:150px;”>
this is str from controller:
<br/>
{{$str}}
<p>
</body>
</html>

 

php精簡完全小結(linux/laravel篇)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.