centos 7 配置php運行環境 (新)

Building nginx from Sources

Centos7.2經過源碼編譯安裝Nginx php

 

 

第一步:安裝編譯所需工具css

命令以下:【html

yum install gccmysql

linux

 

第二步:安裝依賴包nginx

命令以下:web

yum install zlib zlib-devel openssl openssl-devel pcre pcre-develsql

shell

  • zlib: 爲nginx提供gzip模塊,須要zlib庫支持,傳輸數據打包,省流量(但消耗資源)
  • openssl: 爲nginx提供ssl功能
  • pcre: 爲支持地址重寫rewrite功能

 

第三步:下載nginx-1.12.0.tar.gzapache

http://nginx.org/en/download.html

命令以下:[

cd /usr/local/src

wget http://nginx.org/download/nginx-1.12.0.tar.gz

tar -xvzf nginx-1.12.0.tar.gz

cd nginx-1.12.0/

]

第四步:建立用來運行nginx的用戶及組

groupadd www

 useradd -g www -M -s /bin/false www

 

-g參數爲www 用戶指定了一個組。-M參數保證其不自動生成home目錄。

-s 使用-s選項改變用戶的登錄shell。

useradd --help 查看命令幫助

【備註:userdel -r username刪除系統非root用戶, 例如: userdel -r raocui

【備註: id wwww  

//查看用戶www的信息 uid=1005(www) gid=1006(www) groups=1006(www)

【備註: 查看系統用戶組cat /etc/group | grep wwww

【備註: 查看系統用戶cat /etc/passwd| grep wwww

 

 

 

第五步:編譯源代碼:

先使用./configure –help 查看編譯幫助:

命令以下:

./configure  \

--prefix=/usr/local/programfile/nginx \

    --sbin-path=/usr/local/programfile/nginx/sbin/nginx \

    --conf-path=/usr/local/programfile/nginx/nginx.conf \

    --pid-path=/usr/local/programfile/nginx/logs/nginx.pid \

--error-log-path=/usr/local/programfile/nginx/logs/error.log \

--http-log-path=/usr/local/programfile/nginx/logs/access.log \

--build=nginxBuildName  \

--user=www \

--group=www \

    --with-http_ssl_module

 

 

第六步:執行make make install完成安裝

 

編譯好的nginx能夠經過 /usr/local/nginx/nginx -V  (具體路徑以安裝的爲主) 查看編譯時候的參數

 

 

 

 

 

Nginx 配置 域名

Configure NGINX to recognize new VirtualHosts (Server Blocks)

(https://www.godaddy.com/garage/how-to-install-and-configure-nginx-on-centos-7/)

Configure NGINX to serve for your domain

第一步:Create a new user for the webspace

$ groupadd www

  $ useradd -g www -M -s /bin/false www

第二步:Create a new directory for the site DocumentRoot

$ sudo mkdir -p /usr/local/sites/evamtime.com/lumen_html

第三步:Let’s create a test index.html in this directory so that we have something to look at when we test the configuration later:

$ sudo vi /usr/local/sites/evamtime.com/lumen_html/index.html

第四步:Now that our directory and test index is created, we must give ownership of that directory over to the user in question. So following our previous example:

$ sudo chown -R www:www /usr/local/sites/evamtime.com/lumen_html

第五步:We need to also set permissions for this folder so that it can be viewed by the outside world:

$ sudo chmod 755 /usr/local/sites/evamtime.com/lumen_html

Configure NGINX to recognize new VirtualHosts (Server Blocks)

第一步:First, we need to set up our directories where the server blocks will live:

   $ sudo mkdir /usr/local/programfile/nginx/vhost

第二步:Now we need to tell NGINX to use look at those directories for the server blocks. Open the global NGINX configuration file in the text editor of your choice. We will use vim: 

$ sudo vi /usr/local/programfile/nginx/nginx.conf

第三步:Add these lines to the end of the http {} block, then save the file:

  include /usr/local/programfile/nginx/vhost/*.conf;

Server_names_hash_bucket_size 64;

第四步:將全部的server{}註釋掉

Configure the actual NGINX server blocks

第一步:Create a new file specifically for the server block for your site. The line below will do this and open it in vim.

sudo vi  /usr/local/programfile/nginx/vhost/lumen.evamtime.com.conf

 

第二步:We are going to paste a new NGINX server block in here, which should look like this:(備註:以下server{}只是demo, 並且僅用於html文件)

server {

  listen       80;

  server_name  nginxsite.com www.nginxsite.com;

  location / {

    root   /var/www/nginxsite.com/public_html;

    index  index.html index.htm;

    try_files $uri $uri/ =404;

  }    

  error_page   500 502 503 504  /50x.html;

  location = /50x.html {

    root   html;

  }

}

 

Nginx php 配置域名訪問:

 

vi   /usr/local/programfile/nginx/vhost/lume.evamtime.com.conf

server {
listen 80;
server_name lumen.evamtime.com;
index index.html index.htm index.php;
root /usr/local/sites/evamtime.com/lumen_html/public;
#charset koi8-r;

#access_log logs/host.access.log main;


#error_page 404 /404.html;

# redirect server error pages to the static page /50x.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/local/sites/evamtime.com/lumen_html/public;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

location / {
try_files $uri $uri/ /index.php?$query_string;
}

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

#location /nginx_status
#{
#stub_status on;
#access_log off;
#}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*\.(js|css)?$
{
#expires 12h;
}

location ~ /\.
{
deny all;

 

}

 

access_log /usr/local/programfile/nginx/logs/lumen_access.log;
}

$ sudo vi /usr/local/programfile/nginx/nginx.conf

http{}的大括號裏新增:

include vhost/*.conf;

server_names_hash_bucket_size 64;

 

 

ln -s /usr/local/programfile/nginx/sites-available/lumen.evamtime.conf /usr/local/programfile/nginx/sites-enabled/lumen.evamtime.conf

 

 

 

【備註1使用 sudo /usr/local/programfile/nginx/sbin/nginx -t

nginx配置文件進行檢查

 

【備註2保存服務器名字的hash表是由指令 server_names_hash_max_size server_names_hash_bucket_size所控制的

【備註3sudo /usr/local/programfile/nginx/sbin/nginx -s reload

 

Nginx+Center OS 7.2 開機啓動設置

centos 7以上是用Systemd進行系統初始化的,Systemd Linux 系統中最新的初始化系統(init),它主要的設計目標是克服 sysvinit 固有的缺點,提升系統的啓動速度。

 

Systemd服務文件以.service結尾,好比如今要創建nginx爲開機啓動,若是用yum install命令安裝的,yum命令會自動建立nginx.service文件,直接用命令systemcel enable nginx.service設置開機啓動便可。

在這裏我是用源碼編譯安裝的,因此要手動建立nginx.service服務文件。
開機沒有登錄狀況下就能運行的程序,存在系統服務(system)裏,即:

/lib/systemd/system/

 

步驟以下:

第一步:在系統服務目錄裏建立nginx.service文件

vi /lib/systemd/system/nginx.service

內容以下:

[Unit]

Description=nginx

After=network.target

 

[Service]

Type=forking

ExecStart=/usr/local/programfile/nginx/sbin/nginx

ExecReload=//usr/local/programfile/nginx/sbin/nginx -s reload

ExecStop=/usr/local/programfile/nginx/sbin/nginx -s quit

PrivateTmp=true

[Install]

WantedBy=multi-user.target

 

[Unit]:服務的說明
Description:描述服務
After:描述服務類別
[Service]服務運行參數的設置
Type=forking是後臺運行的形式
ExecStart爲服務的具體運行命令
ExecReload爲重啓命令
ExecStop爲中止命令
PrivateTmp=True表示給服務分配獨立的臨時空間
注意:[Service]的啓動、重啓、中止命令所有要求使用絕對路徑
[Install]運行級別下服務安裝的相關設置,可設置爲多用戶,即系統運行級別爲3

保存退出。

 

第二步:設置開機啓動

systemctl enable nginx.service

 

第三步:其餘命令:

啓動nginx服務

Systemctl start nginx.service

設置開機啓動

Systemctl enable nginx.service

中止開機自啓動

Systemctl disable nginx.service

查看服務當前狀態:

Systemctl status nginx.service

從新啓動服務:

Systemctl restart nginx.service

查看全部已啓動的服務:

Systemctl list-units --type=service

 

騰訊雲主機須要開放安全組裏的80端口

 

Centos 7防火牆firewalld開放80端口

命令以下:【

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

 

 

centos7 安裝 mariadb 的正確命令

(https://www.liquidweb.com/kb/how-to-install-mysql-mariadb-on-centos-7/)

命令: yum -y install mariadb*  

yum -y install mariadb-server mariadb

systemctl start/restart/stop/status  mariadb

systemctl enable mariadb

ps -ef | grep msyqld

[root@localhost ~]# mysql 

[root@localhost ~]# mysql_secure_installation  

 

 

Centos7.2 編譯安裝PHP7

http://cn2.php.net/manual/zh/install.unix.nginx.php --官網文檔,源碼安裝PHP

安裝前 檢查PHP是否已經安裝:

$  php -v

$ find / -name 「*php*」

$ rpm -q -a | grep php

【針對rpm查出來的包, 用命令 sudo rpm -e php71w-common刪除】

 

直接下載PHP7.0.2的安裝包解壓,編譯,安裝:

下載php7,並解壓

cd /usr/src/

wget http://cn2.php.net/distributions/php-7.0.25.tar.gz

#解壓

tar -xzxvf php-7.0.2.tar.gz

cd php-7.0.2

解壓完後先不要編譯,請檢查是否安裝了gcc ,沒有的話執行yum install gcc

檢查是否安裝了libxml2 ,沒有的話執行yum install libxml2

檢查是否安裝了libxml2-devel,沒有的話執行yum install libxml2-devel

注:由於改成用nginx了,因此編譯參數中的--with-apxs2=/usr/bin/apxs去掉了,若是要配置apache用,安裝PHP前,請先安裝apache。

 

編譯參數配置

 

'./configure' '--prefix=/usr/local/programfile/php' '--with-pdo-pgsql' '--with-zlib-dir' '--with-freetype-dir' '--enable-mbstring' '--with-libxml-dir=/usr' '--enable-soap' '--enable-calendar' '--with-curl' '--with-mcrypt' '--with-gd' '--with-pgsql' '--disable-rpath' '--enable-inline-optimization' '--with-bz2' '--with-zlib' '--enable-sockets' '--enable-sysvsem' '--enable-sysvshm' '--enable-pcntl' '--enable-mbregex' '--enable-exif' '--enable-bcmath' '--with-mhash' '--enable-zip' '--with-pcre-regex' '--with-pdo-mysql' '--with-mysqli' '--with-jpeg-dir=/usr' '--with-png-dir=/usr' '--enable-gd-native-ttf' '--with-openssl' '--with-fpm-user=www-data' '--with-fpm-group=www-data' '--with-libdir=/lib/x86_64-linux-gnu/' '--enable-ftp' '--with-gettext' '--with-xmlrpc' '--with-xsl' '--enable-opcache' '--enable-fpm' '--with-iconv' '--with-xpm-dir=/usr'

 

出現報錯 Cannot find OpenSSL's <evp.h>

則執行 yum install openssl openssl-devel

 

出現報錯 Please reinstall the libcurl distribution

則執行 yum -y install curl-devel

 

出現報錯 jpeglib.h not found

則執行 yum install libjpeg.x86_64 libpng.x86_64 freetype.x86_64 libjpeg-devel.x86_64 libpng-devel.x86_64 freetype-devel.x86_64 -y

和執行yum install libjpeg-devel

 

 

checking for BZip2 in default path... not found

configure: error: Please reinstall the BZip2 distribution

這是bzip2軟件包沒有安裝

解決辦法

yum install bzip2-devel.x86_64 -y

 

 

configure: error: xpm.h not found.

yum install libXpm-devel

 

error: Unable to locate gmp.h

Fix: yum install gmp-devel

 

現象:Unable to detect ICU prefix or /usr//bin/icu-config failed. Please verify ICU install

 prefix and make sure icu-config works

解決辦法:sudo yum install -y icu.x86_64  libicu libicu_devel

 

錯誤:mcrypt.h not found. Please reinstall libmcrypt.

解決辦法:sudo yum install php-mcrypt.x86_64  libmcrypt libmcrypt-devel.x86_64

 

錯誤: configure: error: Cannot find libpq-fe.h. Please specify correct PostgreSQL installation path

解決辦法:sudo yum install postgresql-devel.x86_64 

錯誤  configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution

解決: yum install libxslt-devel

 

配置的checking 結束後,執行:

 

make clean && make && make install

 

安裝完成後,咱們要把源碼包中的配置文件複製到PHP安裝目錄下,源碼包中有兩個配置  php.ini-development  php.ini-production  ,看名字就知道,一個是開發環境,一個是生產環境,咱們這裏就複製開發環境的

 sudo cp /usr/local/src/php-7.0.25/php.ini-development /usr/local/programfile/php/php.ini

 

sudo cp /usr/local/programfile/php/etc/php-fpm.conf.default  /usr/local/programfile/php/etc/php-fpm.conf

 

sudo vi /usr/local/programfile/php/php.ini

修改:cgi.fix_pathinfo=0

 

另外還須要設置環境變量

修改/etc/profile文件使其永久性生效,並對全部系統用戶生效,在文件末尾加上以下兩行代碼

PATH=$PATH:/usr/local/php/bin

export PATH

而後執行 命令 source /etc/profile

 

php -v 就能夠看到PHP版本信息了。

 

 

此時還須要配置PHP-fpm:

cp /usr/local/programfile/php/etc/php-fpm.conf.default /usr/local/programfile/php/etc/php-fpm.conf

 

cp /usr/local/programfile/php/etc/php-fpm.d/www.conf.default /usr/local/programfile/php/etc/php-fpm.d/www.conf

啓動php-fpm:

 

cp /usr/local/src/php-7.0.25/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

chmod +x /etc/init.d/php-fpm

/etc/init.d/php-fpm start

重啓php-fpm:

$ /etc/init.d/php-fpm restart

sudo /etc/init.d/php-fpm status

 

報錯: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (98)

解決方案:

$ netstat -ntlp | grep 9000

$ killall php-fpm

$ /etc/init.d/php-fpm start

 

 

 備註:sudo cp /usr/local/src/php-7.0.25/sapi/fpm/php-fpm /usr/local/bin

啓動php-fpm:

$ /usr/local/bin/php-fpm

修改 PHP-FPM listen 的方式 

nginx與php-fpm通訊的兩種方式


若想將 PHP-FPM listen 的方式,改爲 unix socket,能夠編輯

sudo /usr/local/programfile/php/etc/php-fpm.d/www.conf

listen = 127.0.0.1:9000

改爲

listen = /var/run/php-fpm.sock

註:不要改爲 listen = /tmp/php-fcgi.sock (將 php-fcgi.sock 設定在 /tmp 底下), 因為系統產生 php-fcgi.sock 時,會放在 /tmp/systemd-private-*/tmp/php-fpm.sock 隨機私有目錄下, 除非把 /usr/lib/systemd/system/ 裡面的 PrivateTmp=true 設定改爲 PrivateTmp=false, 但還是會產生其餘問題,因此還是換個位置最方便 

sudo  vi /usr/local/programfile/nginx/enable-php-lumen.conf

fastcgi_pass unix:/var/run/php-fpm.sock;

重啓nginx php-fpm後,

這樣配置好後,就會在/var/run/php-fpm/目錄下自動生成一個php5-fpm.sock文件,而後一切OK

【備註:php-fpm.sock不存在, 怎麼生成?見本身博客

配置Nginxphp-fpmSock套接字鏈接時,找不到php-fpm.sock的緣由

然後從新啟動 php-fpm

systemctl restart php-fpm

 

 

 

Nginx訪問PHP文件的File not found錯誤處理,

相關文章
相關標籤/搜索