您的位置:首页 > 运维架构 > Nginx

FreeBSD 12 通过 Ports 编译安装 PHP7 Nginx

2016-12-07 00:00 1596 查看
摘要: FreeBSD 12 通过 Ports 编译安装 PHP7 Nginx

# FreeBSD 12 通过 Ports 编译安装 PHP7 Nginx
#作者:老农民
#QQ: 46715422
#Email: flyingercn@wo.cn
#微信: ericliu_77

# 两种方法下载Ports
# 方法一
cd /usr/
portsnap fetch extract
portsnap fetch update

# 方法二
wget --no-check-certificate ftp://ftp.freebsd.org/pub/FreeBSD/development/tarballs/ports_current.tar.gz tar xf ports_current.tar.gz -C /usr/

cd /usr/ports/lang/php70/
make install clean;rehash
# 不要选debug项

cd /usr/ports/lang/php70-extensions/
make install clean;rehash
# mysql,mysqli,pdo_mysql,pgsql,pdo_pgsql,sqlite,pdo_sqlite 等都选上好了

cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini

sed -i -e 's@^;session.save_path = "/tmp"@session.save_path = "/tmp"@' /usr/local/etc/php.ini
sed -i -e "s@^memory_limit.*@memory_limit = 192M@" /usr/local/etc/php.ini
sed -i -e 's@^;cgi.fix_pathinfo.*@cgi.fix_pathinfo=0@' /usr/local/etc/php.ini
sed -i -e 's@^short_open_tag = Off@short_open_tag = On@' /usr/local/etc/php.ini
sed -i -e 's@^expose_php = On@expose_php = Off@' /usr/local/etc/php.ini
sed -i -e 's@^request_order.*@request_order = "CGP"@' /usr/local/etc/php.ini
sed -i -e 's@^;date.timezone.*@date.timezone = Asia/Shanghai@' /usr/local/etc/php.ini
sed -i -e 's@^post_max_size.*@post_max_size = 100M@' /usr/local/etc/php.ini
sed -i -e 's@^upload_max_filesize.*@upload_max_filesize = 128M@' /usr/local/etc/php.ini
sed -i -e 's@^max_execution_time.*@max_execution_time = 600@' /usr/local/etc/php.ini
sed -i -e 's@^;realpath_cache_size.*@realpath_cache_size = 2M@' /usr/local/etc/php.ini
sed -i -e 's@^disable_functions.*@disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,fsocket,popen@' /usr/local/etc/php.ini
[ -e /usr/sbin/sendmail ] && sed -i -e 's@^;sendmail_path.*@sendmail_path = /usr/sbin/sendmail -t -i@' /usr/local/etc/php.ini
sed -i -e 's@^;pid = run/php-fpm.pid@pid = run/php-fpm.pid@' /usr/local/etc/php-fpm.conf
sed -i -e 's@^listen = 127.0.0.1:9000@listen = /var/run/php-cgi.sock@' /usr/local/etc/php-fpm.d/www.conf
sed -i -e 's@^;listen.owner = www@listen.owner = www@' /usr/local/etc/php-fpm.d/www.conf
sed -i -e 's@^;listen.group = www@listen.group = www@' /usr/local/etc/php-fpm.d/www.conf
sed -i -e 's@^;listen.mode = 0660@listen.mode = 0666@' /usr/local/etc/php-fpm.d/www.conf
sed -i -e 's@^;error_log = log/php-fpm.log@error_log = log/php-fpm.log@' /usr/local/etc/php-fpm.conf
sed -i -e 's@^;log_level.*@log_level = warning@' /usr/local/etc/php-fpm.conf
sed -i -e 's@^;emergency_restart_threshold.*@emergency_restart_threshold = 30@' /usr/local/etc/php-fpm.conf
sed -i -e 's@^;emergency_restart_interval.*@emergency_restart_interval = 60s@' /usr/local/etc/php-fpm.conf
sed -i -e 's@^;process_control_timeout.*@process_control_timeout = 5s@' /usr/local/etc/php-fpm.conf
sed -i -e 's@^;daemonize = yes@daemonize = yes@' /usr/local/etc/php-fpm.conf
sed -i -e 's@^;listen.allowed_clients = 127.0.0.1@listen.allowed_clients = 127.0.0.1@' /usr/local/etc/php-fpm.conf
sed -i -e 's@^;pm.max_requests.*@pm.max_requests = 2048@' /usr/local/etc/php-fpm.conf
sed -i -e 's@^;pm.process_idle_timeout.*@pm.process_idle_timeout = 10s@' /usr/local/etc/php-fpm.conf
sed -i -e 's@^;request_terminate_timeout.*@request_terminate_timeout = 120@' /usr/local/etc/php-fpm.conf
sed -i -e 's@^;request_slowlog_timeout.*@request_slowlog_timeout = 0@' /usr/local/etc/php-fpm.conf
sed -i -e 's@^;pm.status_path.*@pm.status_path = /php-fpm_status@' /usr/local/etc/php-fpm.conf
sed -i -e 's@^;slowlog.*@slowlog = log/slow.log@' /usr/local/etc/php-fpm.conf
sed -i -e 's@^;rlimit_files.*@rlimit_files = 51200@' /usr/local/etc/php-fpm.conf
sed -i -e 's@^;rlimit_core = 0@rlimit_core = 0@' /usr/local/etc/php-fpm.conf
sed -i -e 's@^;catch_workers_output = yes@catch_workers_output = yes@' /usr/local/etc/php-fpm.conf

cat >> /etc/rc.conf <<EOF
php_fpm_enable="YES"
EOF

wget --no-check-certificate http://pecl.php.net/get/imagick-3.4.3RC1.tgz tar xf imagick-3.4.3RC1.tgz
cd imagick-3.4.3RC1/
setenv PKG_CONFIG_PATH /usr/local/libdata/pkgconfig
/usr/local/bin/phpize
./configure CPPFLAGS='-I/usr/local/include' --with-php-config=/usr/local/bin/php-config --with-imagick=/usr/local
gmake -j `sysctl hw.ncpu | wc -w`
gmake install
cd ../

wget --no-check-certificate https://github.com/swoole/swoole-src/archive/1.8.12-stable.tar.gz tar xf 1.8.12-stable.tar.gz
cd swoole-src-1.8.12-stable/
/usr/local/bin/phpize
./configure CPPFLAGS='-I/usr/local/include' --with-php-config=/usr/local/bin/php-config
gmake -j `sysctl hw.ncpu | wc -w`
gmake install
cd ../

wget --no-check-certificate https://pecl.php.net/get/apcu-5.1.6.tgz tar xf apcu-5.1.6.tgz
cd apcu-5.1.6/
/usr/local/bin/phpize
./configure CPPFLAGS='-I/usr/local/include' --with-php-config=/usr/local/bin/php-config
gmake -j `sysctl hw.ncpu | wc -w`
gmake install
cd ../

wget --no-check-certificate https://github.com/Jan-E/uploadprogress/archive/master.zip unzip -q master.zip
cd uploadprogress-master/
/usr/local/bin/phpize
./configure CPPFLAGS='-I/usr/local/include' --with-php-config=/usr/local/bin/php-config
gmake -j `sysctl hw.ncpu | wc -w`
gmake install
cd ../

cat >> /usr/local/etc/php.ini <<EOF

extension_dir = "/usr/local/lib/php/20151012/"
extension = "imagick.so"
extension = "swoole.so"

opcache.enable=1
opcache.memory_consumption=192
opcache.interned_strings_buffer=8
opcache.revalidate_freq=60
opcache.save_comments=1
opcache.fast_shutdown=1
opcache.validate_timestamps=1
opcache.enable_cli=1
opcache.use_cwd=1
opcache.max_accelerated_files=100000
opcache.max_wasted_percentage=5
opcache.consistency_checks=0
opcache.optimization_level=0

[apcu]
extension = apcu.so
apc.enabled=1
apc.shm_size=32M
apc.ttl=7200
apc.enable_cli=1
apc.rfc1867 = 1

[uploadprogress]
extension = uploadprogress.so
EOF

pkg install libiconv libmcrypt mhash curl mcrypt openssl jpeg png libtool libvpx freetype bzip2 curl e2fsprogs krb5 nss_ldap tiff autoconf automake libgd pcre re2c
pkg install google-perftools
pkg install ImageMagick

ln -s /usr/local/lib/libmcrypt.a /usr/lib/libmcrypt.a
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
ln -s /usr/local/lib/libpcre.so.1 /lib/libpcre.so.1
ln -s /usr/local/lib/libpcre.so /lib/libpcre.so

ldconfig

mkdir /tmp/tcmalloc
chmod  777 /tmp/tcmalloc
mkdir -p /web/websys
chmod +w /web/websys
chown -R www:www /web/websys

wget --no-check-certificate http://ftp.exim.llorien.org/pcre/pcre-8.39.tar.bz2 wget --no-check-certificate http://nginx.org/download/nginx-1.11.6.tar.gz tar xf pcre-8.39.tar.bz2
tar xf nginx-1.11.6.tar.gz
cd nginx-1.11.6/

vi auto/lib/openssl/conf

# 将下面的内容修改
CORE_INCS="$CORE_INCS $OPENSSL/.openssl/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/.openssl/include/openssl/ssl.h"
CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libssl.a"
CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libcrypto.a"
CORE_LIBS="$CORE_LIBS $NGX_LIBDL"
# 为
CORE_INCS="$CORE_INCS $OPENSSL/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/ssl.h"
CORE_LIBS="$CORE_LIBS /usr/local/lib/libssl.a"
CORE_LIBS="$CORE_LIBS /usr/local/lib/libcrypto.a"
CORE_LIBS="$CORE_LIBS $NGX_LIBDL"

./configure --prefix=/usr/local/nginx --user=www --group=www --with-ipv6 --with-http_gzip_static_module --with-http_v2_module --with-http_realip_module --with-google_perftools_module --with-http_stub_status_module --with-http_sub_module --with-http_flv_module --with-http_ssl_module --with-openssl=/usr/local/include/openssl --with-pcre=../pcre-8.39 --with-pcre-jit
gmake -j `sysctl hw.ncpu | wc -w`
gmake install -j `sysctl hw.ncpu | wc -w`
cd ../

mkdir -p /web/logs/nginx
chmod +w /web/logs/nginx
chown -R www:www /web/logs/nginx

ln -s /usr/local/lib/libjpeg.so.8.1.2 /usr/local/lib/libjpeg.so.8

#创建Nginx配置文件,在/usr/local/nginx/conf/目录中创建nginx.conf文件:
rm -f /usr/local/nginx/conf/nginx.conf
vi /usr/local/nginx/conf/nginx.conf

# nginx 进程数,建议按照CPU数目来指定,一般为它的倍数 (如,2个四核的CPU计为8)
# worker_processes 8;
# worker_cpu_affinity 00000001 00000010 00000100 00001000 0001000000100000 01000000 10000000;
# worker_processes 16;
# worker_cpu_affinity 0000000000000001 0000000000000010 00000000000001000000000000001000 0000000000010000 0000000000100000 00000000010000000000000010000000 0000000100000000 0000001000000000 00000100000000000000100000000000 0001000000000000 0010000000000000 01000000000000001000000000000000;

user  www www;
worker_processes auto;
error_log  /web/logs/nginx/nginx_error.log  crit;

pid        logs/nginx.pid;

google_perftools_profiles /tmp/tcmalloc;

worker_rlimit_nofile 65535;

events {
worker_connections 65535;
multi_accept on;
}

http
{
include       mime.types;
default_type  application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 1024m;
sendfile on;
tcp_nopush on;
keepalive_timeout 120;
server_tokens off;
tcp_nodelay on;

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

gzip on;
gzip_buffers 16 8k;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types
text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
text/javascript application/javascript application/x-javascript
text/x-json application/json application/x-web-app-manifest+json
text/css text/plain text/x-component
font/opentype application/x-font-ttf application/vnd.ms-fontobject
image/x-icon;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";

open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for'
'$http_host $upstream_status $upstream_addr $request_time $upstream_response_time';

server {
listen 80;
return 444;
}
include vhost/*.conf;
include blocksip.conf;
}

#建立屏蔽ip的文件
vi /usr/local/nginx/conf/blocksip.conf
#deny 153.221.164.120;
#deny 153.32.164.121;

mkdir /usr/local/nginx/conf/vhost
vi /usr/local/nginx/conf/vhost/pma.conf

server {
listen 80;
server_name pma.test.net;
root  /web/websys/pma;
index index.html index.htm index.php;
location /nginx_status {
stub_status on;
allow 127.0.0.1;
deny all;
}
location ~ [^/]\.php(/|$) {
fastcgi_pass unix:/var/run/php-cgi.sock;
fastcgi_index index.php;
include ./fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 7d;
}
}
access_log  /web/logs/nginx/pma.log main;

vi /usr/local/nginx/conf/vhost/drupal.conf
server {
listen       80;
server_name  drupal.test.net;

root   /web/websys/drupal;
index index.php index.htm index.html index.shtml;

error_page  404               /page_not_found;
error_page   500 502 503 504  /page_not_found;

location ~ /\.ht {
deny  all;
}

if ($fastcgi_script_name ~ \..*\/.*php) {
return 403;
}

location / {
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?q=$1 last;
}
}

location ~ \.php$ {
fastcgi_pass unix:/var/run/php-cgi.sock;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
include        ./fastcgi_params;
}

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

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

location ^~ /sites/default/files/imagecache/ {
index index.php index.html;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?q=$1 last; break;
}
}
}
access_log  /web/logs/nginx/drupal.log  main;

vi /usr/local/etc/rc.d/nginx

#!/bin/sh
# $FreeBSD: head/www/nginx/files/nginx.in 340872 2014-01-24 00:14:07Z mat $

# PROVIDE: nginx
# REQUIRE: LOGIN cleanvar
# KEYWORD: shutdown

#
# Add the following lines to /etc/rc.conf to enable nginx:
# nginx_enable (bool):		Set to "NO" by default.
#				Set it to "YES" to enable nginx
# nginx_profiles (str):		Set to "" by default.
#				Define your profiles here.
# nginx_pid_prefix (str):	Set to "" by default.
#				When using profiles manually assign value to "nginx_"
#				for prevent collision with other PIDs names.
# nginxlimits_enable (bool):	Set to "NO" by default.
#				Set it to yes to run `limits $limits_args`
#				just before nginx starts.
# nginx_flags (str):		Set to "" by default.
#				Extra flags passed to start command.
# nginxlimits_args (str):	Default to "-e -U www"
#				Arguments of pre-start limits run.

. /etc/rc.subr

name="nginx"
rcvar=nginx_enable

start_precmd="nginx_precmd"
restart_precmd="nginx_checkconfig"
reload_precmd="nginx_checkconfig"
configtest_cmd="nginx_checkconfig"
gracefulstop_cmd="nginx_gracefulstop"
upgrade_precmd="nginx_checkconfig"
upgrade_cmd="nginx_upgrade"
command="/usr/local/nginx/sbin/nginx"
_pidprefix="/usr/local/nginx/logs"
pidfile="${_pidprefix}/${name}.pid"
_tmpprefix="/var/tmp/nginx"
required_files=/usr/local/nginx/conf/nginx.conf
extra_commands="reload configtest upgrade gracefulstop"

[ -z "$nginx_enable" ]		&& nginx_enable="NO"
[ -z "$nginxlimits_enable" ]	&& nginxlimits_enable="NO"
[ -z "$nginxlimits_args" ]	&& nginxlimits_args="-e -U www"

load_rc_config $name

if [ -n "$2" ]; then
profile="$2"
if [ "x${nginx_profiles}" != "x" ]; then
pidfile="${_pidprefix}/${nginx_pid_prefix}${profile}.pid"
eval nginx_configfile="\${nginx_${profile}_configfile:-}"
if [ "x${nginx_configfile}" = "x" ]; then
echo "You must define a configuration file (nginx_${profile}_configfile)"
exit 1
fi
required_files="${nginx_configfile}"
eval nginx_enable="\${nginx_${profile}_enable:-${nginx_enable}}"
eval nginx_flags="\${nginx_${profile}_flags:-${nginx_flags}}"
eval nginxlimits_enable="\${nginxlimits_${profile}_enable:-${nginxlimits_enable}}"
eval nginxlimits_args="\${nginxlimits_${profile}_args:-${nginxlimits_args}}"
nginx_flags="-c ${nginx_configfile} -g \"pid ${pidfile};\" ${nginx_flags}"
else
echo "$0: extra argument ignored"
fi
else
if [ "x${nginx_profiles}" != "x" -a "x$1" != "x" ]; then
for profile in ${nginx_profiles}; do
echo "===> nginx profile: ${profile}"
/usr/local/etc/rc.d/nginx $1 ${profile}
retcode="$?"
if [ "0${retcode}" -ne 0 ]; then
failed="${profile} (${retcode}) ${failed:-}"
else
success="${profile} ${success:-}"
fi
done
exit 0
fi
fi

# tmpfs(5)
nginx_checktmpdir()
{
if [ ! -d ${_tmpprefix} ] ; then
install -d -o www -g www -m 755 ${_tmpprefix}
fi
}

nginx_checkconfig()
{
nginx_checktmpdir

echo "Performing sanity check on nginx configuration:"
eval ${command} ${nginx_flags} -t
}

nginx_gracefulstop()
{
echo "Performing a graceful stop:"
sig_stop="QUIT"
run_rc_command ${rc_prefix}stop $rc_extra_args || return 1
}

nginx_upgrade()
{
echo "Upgrading nginx binary:"

reload_precmd=""
sig_reload="USR2"
run_rc_command ${rc_prefix}reload $rc_extra_args || return 1

sleep 1

echo "Stopping old binary:"

sig_reload="QUIT"
pidfile="$pidfile.oldbin"
run_rc_command ${rc_prefix}reload $rc_extra_args || return 1
}

nginx_precmd()
{
nginx_checkconfig

if checkyesno nginxlimits_enable
then
eval `/usr/bin/limits ${nginxlimits_args}` 2>/dev/null
else
return 0
fi
}

run_rc_command "$1"

chmod +x /usr/local/etc/rc.d/nginx

cat >> /etc/rc.conf <<EOF
nginx_enable="YES"
EOF

service php-fpm start
service nginx start

要买正宗新疆特产,微信扫下面二维码,加老农民咨询购买:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  FreeBSD PHP Nginx fnmp