CentOS7上安裝並配置Nginx、PHP、MySql

來源:互聯網
上載者:User

標籤:安裝過程   www   get   body   設定   put   release   path   stat   

一、Nginx

 1、安裝nginx

yum install nginx

2、啟動nginx

systemctl start nginx

除了systemctl start nginx之外,常用的相關命令還有systemctl stop nginx、systemctl restart nginx、systemctl status nginx

3、測試nginx是否安裝成功

    瀏覽器輸入ip地址或者網域名稱(已經解析過的網域名稱),如所示,則安裝成功。

 

4,配置Nginx支援PHP解析

     編輯/etc/nginx/nginx.conf,藍色字型處為新加內容

 server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
        index index.php index.html index.htm;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
        location ~ .php$ {
        try_files $uri =404;
        root /usr/share/nginx/html;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi.conf;
}

}

二、PHP

1,安裝PHP

yum install php php-mysql php-fpm

安裝過程中經常會見到如下問題:
2:postfix-2.10.1-6.el7.x86_64 有缺少的需求 libmysqlclient.so.18()(64bit)
2:postfix-2.10.1-6.el7.x86_64 有缺少的需求 libmysqlclient.so.18(libmysqlclient_18)(64bit)
解決方案:
把php-mysql換成php-mysqlnd
即執行

yum install php php-mysqlnd php-fpm

2、編輯PHP的設定檔,/etc/php.ini,注意去掉分號注釋

vim /etc/php.ini

   將 ;cgi.fix_pathinfo=1 改為 cgi.fix_pathinfo=0

3、編輯PHP-FPM設定檔

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

 將
 user = nobody
 group = nobody   

 改為
 user = nginx
 group = nginx
 前提是已經建立了nginx使用者和nginx組。如果沒有建立方法:

1 groupadd -r nginx2 useradd -r -g nginx nginx

4、啟動PHP—FPM

systemctl start php-fpm

5、設定開機啟動

systemctl enable php-fpm

6,確保Nginx設定檔修該之後,重啟Nginx

systemctl restart nginx

7、在/usr/share/nginx/html/目錄下建立phpinfo.php

    內容如下:

   <?php phpinfo();?>

   查看php進程:ps aux | grep php  查看連接埠佔用:netstat -ano|grep 80

8、瀏覽器上輸入ip/phpinfo.php,如果出現如下介面,說明PHP和Nginx均安裝和配置成功。

     

 三、MySql

CentOS 7的yum源中貌似沒有正常安裝mysql時的mysql-sever檔案,需要去官網上下載

1、補充yum源(1)

wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

2、補充yum源(2)

rpm -ivh mysql-community-release-el7-5.noarch.rpm

3、安裝mysql

yum install mysql-community-server

4、成功安裝之後重啟mysql服務

systemctl start mysqld

初次安裝mysql是root賬戶是沒有密碼的
設定密碼的方法

1 mysql -uroot2 mysql> set password for ‘root’@‘localhost’ = password(‘mypasswd‘);3 mysql> exit

 

mysql -u root -p

輸入密碼之後,錯誤提示如下:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES),意思是root的密碼不正確。

解決辦法如下:
a.sudo mysqld_safe --user=root --skip-grant-tables --skip-networking &
輸入命令之後,如果提示資訊為:
mysqld_safe A mysqld process already exists
 
表示mysqld_safe進程存在,可以通過
ps -A|grep mysql 查看mysqld_safe進程ID
kill -9 -xxxx            終結ID為xxxx的進程

 

問題:以上安裝之後 nginx並不解析 php檔案的原因 

nginx安裝完成後,修改nginx設定檔為,nginx.conf

其中server段增加如下配置,注意標紅內容配置,否則會出現No input file specified.錯誤

# 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 $document_root$fastcgi_script_name;
include fastcgi_params;
}

當時就是預設nginx裡的配置與上面的紅色有些出入,導致不能正常解析php檔案。請注意紅色加粗的部份。

網上有很多文章都不靠譜,後來找到一個靠譜的答案,已解決:http://blog.sina.com.cn/s/blog_6fcb75890102w8xm.html  轉自:https://www.cnblogs.com/peteremperor/p/6740725.html

CentOS7上安裝並配置Nginx、PHP、MySql

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.