centos 7 配置php运行环境 (新)

Building nginx from Sources

Centos7.2通过源码编译安装Nginx

 

 

第一步:安装编译所需工具

命令如下:【

yum install gcc

 

第二步:安装依赖包

命令如下:

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

  • zlib: 为nginx提供gzip模块,需要zlib库支持,传输数据打包,省流量(但消耗资源)
  • openssl: 为nginx提供ssl功能
  • pcre: 为支持地址重写rewrite功能

 

第三步:下载nginx-1.12.0.tar.gz

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错误处理,

 

posted @ 2017-11-15 13:07  饶翠  阅读(813)  评论(0编辑  收藏  举报