CentOS 7 安裝php5.6,Nginx環境及配置


CentOS 7 安裝php5.6,Nginx環境及配置

 

安裝php5.6版本以后不再需要安裝Zend Guard,而是用yum命令安裝php-opcache及php-pecl-apcu就可以有效的提高php執行速度。

1. 配置yum源

事先確認yum源的鏈接是不是有效的。

yum install epel-release

rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

3. 確認安裝的php版本

yum list --enablerepo=remi --enablerepo=remi-php56 | grep php

4. 安裝php5.6

yum install --enablerepo=remi --enablerepo=remi-php56 php php-opcache php-pecl-apcu php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof php-pdo php-pear php-fpm php-cli php-xml php-bcmath php-process php-gd php-common

php-opcache及php-pecl-apcu會有效的提高php執行速度

4. 確認php版本

# php -v
PHP 5.6.6 (cli) (built: Feb 19 2015 10:19:37)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2015, by Zend Technologies
    with Xdebug v2.3.1, Copyright (c) 2002-2015, by Derick Rethans

測試PHP-fpm服務是否正常

#systemctl start  php-fpm.service 

 使用:#netstat -an查看9000端口有沒有在監聽

 

systemctl start nginx.service              #啟動nginx服務
systemctl enable nginx.service             #設置開機自啟動
systemctl disable nginx.service            #停止開機自啟動
systemctl status nginx.service             #查看服務當前狀態
systemctl restart nginx.service           #重新啟動服務
systemctl list-units --type=service        #查看所有已啟動的服
systemctl enable php-fpm.service
 

 

Mysql配置文件my.cnf路徑:/etc/my.cnf 

Nginx配置文件nginx.conf路徑:/etc/nginx/nginx.conf 

PHP配置文件php.ini路徑: /etc/php.ini 

php-fpm配置文件php-fpm.conf路徑:/etc/php-fpm.conf 

 

安裝NGINX

1.添加資源

添加CentOS 7 Nginx yum資源庫,打開終端,使用以下命令(沒有換行):

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

 

2.安裝Nginx

在你的CentOS 7 服務器中使用yum命令從Nginx源服務器中獲取來安裝Nginx:

這里有一個需要注意的地方,盡量不要用網上的下載源碼包然后再傳到服務器上的方式進行安裝,因為nginx已經不算是簡單的Linux了,做了很多擴展,這個時候如果你用源碼包安裝會出現各種各樣的問題,盡量用已經封裝好的rpm\yum進行安裝

 yum install -y nginx

 

Nginx將完成安裝在你的CentOS 7 服務器中。

3.啟動Nginx

剛安裝的Nginx不會自行啟動。運行Nginx:

systemctl start nginx.service

 

如果一切進展順利的話,現在你可以通過你的域名或IP來訪問你的Web頁面來預覽一下Nginx的默認頁面

當然,這里一般很可能會無法訪問的。

我們先不急於解決我們的問題,先看看nginx的基本配置:

網站文件存放默認目錄

/usr/share/nginx/html
網站默認站點配置

/etc/nginx/conf.d/default.conf
自定義Nginx站點配置文件存放目錄,自己在這里也可以定義別的名字的.conf,這個的作用以后再說。

/etc/nginx/conf.d/
Nginx全局配置

/etc/nginx/nginx.conf
在這里你可以改變設置用戶運行Nginx守護程序進程一樣,和工作進程的數量得到了Nginx正在運行,等等。

好了,這個時候我們再來看看可能遇到的問題:無法在公網訪問。

這個時候首先看看配置文件default.conf對不對,一個正確的例子: 
(域名要先進行解析到響應的IP)

server {
    listen       80;
    server_name  nginx.310058.cn;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

我的配置文件為(可正常運行):

# nano /etc/nginx/conf.d/default.conf                                                             

server {
    listen       80;
    server_name dev1.mydomain.com.cn;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

 

 

確定文件沒問題了,看看這個時候是不是開啟了nginx進程:

ps -ef | grep nginx

應該會輸出一個或者多個進程,如果沒有的話就開啟或者重啟試試看。

這個時候接下來再試試在服務器上:

ping  115.29.102.81
telnet 115.29.102.81 80
wget nginx.310058.cn

如果有的命令沒有就直接yum安裝下:

yum -y install telnet

如果都可以的話,之后在本機嘗試以上三行。如果沒有命令也要安裝下:

brew install wget

發現很可能本機telnet不通,而服務器telnet通。 
這個時候就是防火牆的問題。

centos7.2防火牆

由於centos 7版本以后默認使用firewalld后,網上關於iptables的設置方法已經不管用了,所以根本就別想用配置iptables做啥,根本沒用。

查看下防火牆狀態:

[root@iZ28dcsp7egZ conf.d]# systemctl status firewalld  
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2016-08-03 12:06:44 CST; 2h 49min ago
 Main PID: 424 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─424 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

Aug 03 12:06:41 iZ28dcsp7egZ systemd[1]: Starting firewalld - dynamic firewall daemon...
Aug 03 12:06:44 iZ28dcsp7egZ systemd[1]: Started firewalld - dynamic firewall daemon.

增加80端口的權限:

firewall-cmd --zone=public --add-port=80/tcp --permanent  

別忘了更新防火牆的配置:

firewall-cmd --reload

這個時候再restart nginx.service 一下就會發現應該好了。

nginx 停止:

service nginx restart
也可以重啟nginx

kill -QUIT 進程號  
#從容停止

kill -TERM 進程號
#或者
kill -INT 進程號
#快速停止

p-kill -9 nginx
強制停止

nginx -t 
#驗證配置文件 前提是進入相應的配置的目錄(自己實際測試的時候發現沒有進入相應的配置目錄也是可以的)

nginx -s reload
#重啟

kill -HUP 進程號
#重啟的另外一種方式

官方文檔地址: 
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Security_Guide/sec-Using_Firewalls.html#sec-Introduction_to_firewalld

附1:一個簡單的負載均衡的實現: 
weight默認是1,自己也可以更改。

附2:防火牆基本學習:

1、firewalld簡介
firewalld是centos7的一大特性,最大的好處有兩個:支持動態更新,不用重啟服務;第二個就是加入了防火牆的“zone”概念

firewalld有圖形界面和工具界面,由於我在服務器上使用,圖形界面請參照官方文檔,本文以字符界面做介紹

firewalld的字符界面管理工具是 firewall-cmd 

firewalld默認配置文件有兩個:/usr/lib/firewalld/ (系統配置,盡量不要修改)和 /etc/firewalld/ (用戶配置地址)

zone概念:
硬件防火牆默認一般有三個區,firewalld引入這一概念系統默認存在以下區域(根據文檔自己理解,如果有誤請指正):
drop:默認丟棄所有包
block:拒絕所有外部連接,允許內部發起的連接
public:指定外部連接可以進入
external:這個不太明白,功能上和上面相同,允許指定的外部連接
dmz:和硬件防火牆一樣,受限制的公共連接可以進入
work:工作區,概念和workgoup一樣,也是指定的外部連接允許
home:類似家庭組
internal:信任所有連接
對防火牆不算太熟悉,還沒想明白public、external、dmz、work、home從功能上都需要自定義允許連接,具體使用上的區別還需高人指點

2、安裝firewalld
root執行 # yum install firewalld firewall-config

3、運行、停止、禁用firewalld
啟動:# systemctl start  firewalld
查看狀態:# systemctl status firewalld 或者 firewall-cmd --state
停止:# systemctl disable firewalld
禁用:# systemctl stop firewalld

4、配置firewalld
查看版本:$ firewall-cmd --version
查看幫助:$ firewall-cmd --help
查看設置:
                顯示狀態:$ firewall-cmd --state
                查看區域信息: $ firewall-cmd --get-active-zones
                查看指定接口所屬區域:$ firewall-cmd --get-zone-of-interface=eth0
拒絕所有包:# firewall-cmd --panic-on
取消拒絕狀態:# firewall-cmd --panic-off
查看是否拒絕:$ firewall-cmd --query-panic

更新防火牆規則:# firewall-cmd --reload
                            # firewall-cmd --complete-reload
    兩者的區別就是第一個無需斷開連接,就是firewalld特性之一動態添加規則,第二個需要斷開連接,類似重啟服務

將接口添加到區域,默認接口都在public
# firewall-cmd --zone=public --add-interface=eth0
永久生效再加上 --permanent 然后reload防火牆

設置默認接口區域
# firewall-cmd --set-default-zone=public
立即生效無需重啟

打開端口(貌似這個才最常用)
查看所有打開的端口:
# firewall-cmd --zone=dmz --list-ports
加入一個端口到區域:
# firewall-cmd --zone=dmz --add-port=8080/tcp
若要永久生效方法同上

打開一個服務,類似於將端口可視化,服務需要在配置文件中添加,/etc/firewalld 目錄下有services文件夾,這個不詳細說了,詳情參考文檔
# firewall-cmd --zone=work --add-service=smtp

移除服務
# firewall-cmd --zone=work --remove-service=smtp

還有端口轉發功能、自定義復雜規則功能、lockdown,由於還沒用到,以后再學習

 安裝vsftpd

首先用命令檢查VSFTP是否已經安裝

chkconfig --list | grep vsftpd

接着使用yum命令直接安裝

yum  install –y vsftpd

 

 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM