PHP 环境搭建
By skyshappiness Posted 2017-01-04 21:52:34 In


创建源码存放目录: mkdir -p /usr/local/src


查看可安装 Nginx 版本

远程下载 Nginx 安装源码 wget

解压缩 Nginx 源码 tar -zxvf nginx-1.6.2.tar.gz

进入源码文件夹 cd nginx-1.6.2/

安装 Nginx 编译依赖项 yum  -y install gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel perl-ExtUtils-Embed

配置 Nginx 参数 ./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx —conf- path=/etc/nginx/nginx.conf --error-log-path=/data/log/nginx/error.log --http-log-   path=/data/log/nginx/access.log --http-client-body-temp-   path=/var/lib/nginx/tmp/client_body --http-proxy-temp-     path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi   --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-   path=/var/lib/nginx/tmp/scgi --pid-path=/var/run/ —lock-   path=/var/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio —with-   ipv6 --with-http_ssl_module --with-http_realip_module --with-http_addition_module   --with-http_sub_module --with-http_dav_module --with-http_flv_module —with-   http_mp4_module --with-http_gzip_static_module --with-http_random_index_module —   with-http_secure_link_module --with-http_degradation_module —with-     http_stub_status_module --with-http_perl_module --with-mail —with-   mail_ssl_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-   D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64   -mtune=generic' --with-ld-opt=-Wl,-E

编译,安装编译           make;make install

创建配置项文件夹   mkdir -p /etc/nginx/conf.d

按如下内容修改主配置文件   vi /etc/nginx/nginx.conf

#user  nobody;

user nginx;

worker_processes  2;

error_log       /var/log/nginx/error.log;

#error_log      logs/error.log;

#error_log      logs/error.log  notice;

#error_log      logs/error.log  info;

#pid            logs/;

pid             /var/run/;

events {

worker_connections  1024;


http {

client_max_body_size 32m;

include       mime.types;

default_type  application/octet-stream;

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

access_log   /var/log/nginx/access.log main;

#access_log  logs/access.log  main;

sendfile        on;

#tcp_nopush     on;

#keepalive_timeout  0;

keepalive_timeout  65;

gzip  on;

gzip_http_version 1.0;

gzip_disable "MSIE[1-6].";

gzip_types text/plain application/x-javascript text/css text/javascript       image/jpeg image/gif image/png;

gzip_min_length 1024;

gzip_comp_level 3;

gzip_vary on;

# Load config files from the /etc/nginx/conf.d directory

# The default server is in conf.d/default.conf

include /etc/nginx/conf.d/*.conf;


修改多站点配置文件    vi /etc/nginx/conf.d/project_name.conf

server {

listen 80;


rewrite ^/([a-z0-9/]+)?$ /index.php?s=$1 break;

root /data/www/project/metlife;

location / {

index index.html;


location ~ .*\.(php)$ {


fastcgi_index  index.php;

fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

fastcgi_param  SCRIPT_NAME      $fastcgi_script_name;

include        /etc/nginx/fastcgi.conf;



测试配置文件    /usr/sbin/nginx -t

创建 nginx 分组    /usr/sbin/groupadd -f nginx

创建 nginx 用户并分组    /usr/sbin/useradd -g nginx nginx

启动 Nginx    /usr/sbin/nginx

重启 Nginx    /usr/sbin/nginx -s reload

三、安装 PHP

进入源码文件夹    cd /usr/local/src/

下载 EPEL 源码仓库    wget

安装 EPEL 源码    rpm -Uvh epel-release-6*.rpm

下载 IUS 源码仓库    wget    14.ius.centos6.noarch.rpm

安装 IUS 源码 rpm -Uvh ius-release*.rpm

下载 Remi 源码 wget

安装 Remi 源码    rpm -Uvh remi-release-6*.rpm

查找 PHP5.6    yum search php56u | grep x86_64

安装 PHP5.6及扩展    yum install -y php56u.x86_64 php56u-bcmath.x86_64 php56u-cli.x86_64 php56u-    common.x86_64 php56u-fpm.x86_64 php56u-gd.x86_64 php56u-mbstring.x86_64 php56u-    mcrypt.x86_64 php56u-mysqlnd.x86_64 php56u-pdo.x86_64 php56u-pecl-imagick.x86_64     php56u-pecl-jsonc.x86_64 php56u-pecl-memcache.x86_64 php56u-pecl-    memcached.x86_64 php56u-pecl-redis.x86_64 php56u-soap.x86_64 php56u-xml.x86_64     php56u-xmlrpc.x86_64(扩展可后续使用 yum 安装或者编译安装)

查看 PHP 版本    php -v

修改 PHP 如下配置项    vi /etc/php.ini

engine = On

short_open_tag = Off

asp_tags = Off

max_execution_time = 30

memory_limit = 256M # 根据服务器实际情况配置可用内存大小

display_errors = Off

post_max_size = 32M # 根据服务器实际情况配置最大上传单个文件大小

upload_max_filesize = 32M # 根据服务器实际情况配置合计最大上传文件大小

date.timezone = PRC # 设置时区为中国北京时间

session.save_handler = files # 设置会话句柄为文件

修改 PHP-FPM 如下配置项    vi /etc/php-fpm.conf

include=/etc/php-fpm.d/*.conf   # 加载外部配置文件

error_log = /var/log/php-fpm/error.log

修改默认配置文件    vi /etc/php-fpm.d/www.conf

user = nginx

group = nginx

request_terminate_timeout = 600

php_admin_value[error_log] = /var/log/php-fpm/www-error.log

php_admin_flag[log_errors] = on

php_value[session.save_handler] = files

php_value[session.save_path] = /var/lib/php/session  # 注意此文件夹的用户必须与php-    fpm的用户一致

启动 php-fpm     service php-fpm start

重启 php-fpm    service php-fpm restart

四、安装 mysql

进入源码下载文件夹    cd /usr/local/src/

卸载系统自带 Mysql    yum -y remove mysql-libs-*

安装 Mysql Server    yum install -y mysql-community-server

下载 Mysql 源码仓库    wget

安装 Mysql 源码仓库    rpm -Uvh mysql-community-release-el5-5.noarch.rpm

安装 Mysql    yum install -y mysql-community-server

启动 Mysql    service mysqld start

配置 Mysql    /usr/bin/mysql_secure_installation(按提示配置 Mysql)

目录说明    /var/log/mysqld.log #MySQL日志目录

/var/lib/mysql #MySQL数据文件目录

/usr/share/mysql/ #MySQL配置文件目录

/usr/bin/ #MySQL启动脚本目录

修改 Mysql 配置    vi /etc/my.cnf

# For advice on how to change settings please see




# Remove leading # and set to the amount of RAM for the most important data

# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.

# innodb_buffer_pool_size = 128M


# Remove leading # to turn on a very important data integrity option: logging

# changes to the binary log between backups.

# log_bin


# Remove leading # to set options mainly useful for reporting servers.

# The server defaults are faster for transactions and fast SELECTs.

# Adjust sizes as needed, experiment to find the optimal values.

# join_buffer_size = 128M

# sort_buffer_size = 2M

# read_rnd_buffer_size = 2M



# Disabling symbolic-links is recommended to prevent assorted security risks


# Recommended in standard MySQL setup


character_set_server = utf8




启动 Mysql    service mysqld start

重启 Mysql    service mysqld restart

  • 邮箱 /