Linux -- Centos 下配置LNAMP 服務器環境


1.Mysql

centos 7 下mysql被替換掉,如有需要請看另一篇:

centos 6.5下:

yum install mysql mysql-server mysql-devel

啟動mysql :

centos 6.5:

/etc/init.d/mysqld start

開機啟動:

centos 6.5:

chkconfig mysqld on

2.安裝php

yum install php php-mysql php-common php-gd php-mbstring php-mcrypt php-devel php-xml php-fpm php-cli php-ldap php-odbc php-pdo php-pecl-memcache php-pear php-mbstring php-xml php-xmlrpc php-snmp php-soap

啟動fpm:

centos 6.5:

service php-fpm start

centos 7:

systemctl enable php-fpm.service
systemctl start php-fpm.service

3.安裝apache(可選)

yum install httpd httpd-devel

啟動和開機啟動:

/etc/init.d/httpd start
chkconfig httpd on

這里需要更改apache的默認端口,使其不為80,避免與nginx沖突,如有需要參見apache配置文件修改

4.Nginx安裝

Nginx不是從官方CentOS庫安裝,我們從 nginx 項目安裝庫安裝,修改源:

vi /etc/yum.repos.d/nginx.repo
修改為:
 [nginx]
     name=nginx repo
     baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
     gpgcheck=0
     enabled=1
yum install nginx

啟動和開機啟動:

centos 7:

systemctl enable nginx.service
systemctl start nginx.service

centos 6.5:

service nginx start

5.配置nginx

現在我們打開配置文件 

/etc/nginx/nginx.conf

這里沒有需要不需更改,如有需要參見nginx詳細配置
vi /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

    #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           /usr/share/nginx/html;
        #try_files $uri =404;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$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:

systemctl restart nginx.service

ystemctl reload nginx.service

 

6.打開防火牆端口:

注意:有時安裝好可能無法訪問,很大的原因是防火牆,需要打開80端口

通過

/etc/init.d/iptables status

命令查詢是否有打開80端口,如果沒有可通過兩種方式處理:

1.修改vi /etc/sysconfig/iptables命令添加使防火牆開放80端口

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT


2.關閉防火牆
/etc/init.d/iptables stop 
#start 開啟 
#restart 重啟
永久性關閉防火牆chkconfig --level 35 iptables off

7.測試php

現在創建的文檔根目錄下的PHP探針文件 /usr/share/nginx/html

vi /usr/share/nginx/html/info.php
<?php
phpinfo();
?>

測試輸入ip,根據nginx的log來調試。

8.使用php-fpm套接字來連接nginx(可選)

默認情況下監聽端口 9000 。 另外,也可以使PHP-FPM使用Unix套接字,這避免了TCP的開銷。要做到這一點,打開 /etc/php-fpm.d/www.conf…

vi /etc/php-fpm.d/www.conf

… 修改后如下:

[...]
;listen = 127.0.0.1:9000
listen = /var/run/php-fpm/php5-fpm.sock
[...]

 

然后重新加載 PHP-FPM:

systemctl restart php-fpm.service

接下來通過你的nginx的配置和所有的虛擬主機和改線 fastcgi_pass 127.0.0.1:9000; to fastcgi_pass unix:/tmp/php5-fpm.sock;,像這樣:

vi /etc/nginx/conf.d/default.conf

[...]
    location ~ .php$ {
        root           /usr/share/nginx/html;
        try_files $uri =404;
        fastcgi_pass   unix:/var/run/php-fpm/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
[...]

 

最后重新加載 nginx:

systemctl restart nginx.service

 


免責聲明!

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



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