Apache+PHP+SQL Server under Linux

Keywords: PHP Apache Attribute libiconv

This document applies to CentOS 6.7 deployment Apache+PHP connection SQL Server 2008

Software version:
Apache: httpd-2.4.23 http://httpd.apache.org/download.cgi#apache24
PHP: php-5.5.12
Apr: apr apr-1.5.2
Apr-util: apr-util-1.5.4
libiconv: libiconv-1.14
FreeTDS: freetds-1.00.15

First install the development environment and dependency packages

yum install gcc openssl openssl-devel pcre pcre-devel libxml2 libxml2-devel libcurl libcurl-devel libpng libpng-devel freetype-devel libxslt-devel libxslt

Apache

Since Apache 2.4.x relies on Apr 1.4 + and apr-util 1.4+, the two packages need to be compiled and installed before compiling and installing Apache 2.4.

#Install apr
~]# ./configure  --prefix=/usr/local/apr
~]# make && make install


#Install apr-util
~]# ./configure  --prefix=/usr/local/apr-util  --with-apr=/usr/local/apr
~]# make && make install

#Install Apache (add running users and groups)
~]#  ./configure --prefix=/usr/local/apache24 --sysconfdir=/etc/httpd24  --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
~]# make && make install

Add Apache service management scripts

vim /etc/init.d/httpd

#!/bin/bash
#
# httpd        Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server.  It is used to serve \
#        HTML files and CGI.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache24/bin/apachectl #Install the path according to yourself
httpd=${HTTPD-/usr/local/apache24/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/usr/local/apache24/logs/httpd.pid} #Find your own installation path
lockfile=${LOCKFILE-/var/lock/subsys/httpd24}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}

start() {
        echo -n $"Starting $prog: "
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}

stop() {
  echo -n $"Stopping $prog: "
  killproc -p ${pidfile} -d 10 $httpd
  RETVAL=$?
  echo
  [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
        RETVAL=$?
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        killproc -p ${pidfile} $httpd -HUP
        RETVAL=$?
    fi
    echo
}

# See how we were called.
case "$1" in
  start)
  start
  ;;
  stop)
  stop
  ;;
  status)
        status -p ${pidfile} $httpd
  RETVAL=$?
  ;;
  restart)
  stop
  start
  ;;
  condrestart)
  if [ -f ${pidfile} ] ; then
    stop
    start
  fi
  ;;
  reload)
        reload
  ;;
  graceful|help|configtest|fullstatus)
  $apachectl $@
  RETVAL=$?
  ;;
  *)
  echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
  exit 1
esac

exit $RETVAL

Add execution permissions to / etc/init.d/httpd and add httpd to boot-up autostart

~]# chmod +x /etc/init.d/httpd
~]# chkconfig httpd --add
~]# chkconfig httpd on

Compile and install PHP

When backend PHP connects to SQL Server, there will be chaos. Solution: Install libiconv

Compile and install libiconv

~]# ./configure --prefix=/usr/local/libiconv 
~]# make -j 4 && make install

Compile and install PHP

~]# ./configure --prefix=/usr/local/php --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir=/usr/local/libiconv --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-openssl --with-pcre-regex --with-pdo-sqlite --with-pear --with-png-dir --with-xmlrpc --with-xsl --with-zlib --with-apxs2=/usr/local/apache24/bin/apxs --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-zip

~]# make -j 4 && make install -j 4

Configure apache to support php

Modify apache configuration file httpd.conf

~]# vim /usr/local/apache2.4/conf/httpd.conf

Then look up the text and uncomment it

LoadModule php5_module modules/libphp5.so #If cancelled, proceed to the next step

#Add to
AddType application/x-httpd-php .php

* Note that access to http:localhost/.php will be downloaded directly, not opened, if the above one is not configured properly.

Copy the php configuration file

~]# cp php-5.6.3/php.ini-production /usr/local/php/lib/php.ini 

Restart the httpd service test phpinfo page

Freetds compilation and installation

Freetds functions as PHP support to connect to SQL Server under Linux

Download
~]# wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gz
~]# tar -zxvf freetds-patched.tar.gz
~]# cd freetds-1.00.15

# Compile and Install
~]# ./configure --prefix=/usr/local/freetds --with-tdsver=7.3 --enable-msdblib
~]# make && make install

To configure
~]# cd ../
~]# echo "/usr/local/freetds/lib/" > /etc/ld.so.conf.d/freetds.conf
~]# ldconfig

Enter the following command to see if TDS Version is consistent with your version of SQL Server
FreeTDS Official Version Support Information
http://www.freetds.org/userguide/choosingtdsprotocol.htm

~]# /usr/local/freetds/bin/tsql -C

Modify the time format of FreeTDS

If the date conversion function is not used in php, the date will b e the default time format, such as:% b% e% Y% I:% M:% S:% z% P. To solve this problem, you can configure the locales.conf file under the / usr/local/freetds/etc path, and modify it as follows:

[default]
    date format = %Y-%m-%d %H:%M:%S
[en_US]
    date format = %Y-%m-%d %H:%M:%S

Testing database connectivity

65] / usr/local/freetds/bin/tsql-H database server IP-p port number-U username-P password

Extended mssql to add PHP

~]# cd /opt/software/php-5.5.12/ext/mssql/

#Using phpize to dynamically add extensions to PHP under linux
~]# /usr/local/php/bin/phpize
~]# ./configure --with-php-config=/usr/local/php/bin/php-config --with-mssql=/usr/local/freetds/
~]# make && make install

Modify the PHP configuration file

Add.so in the php.ini configuration file.
php.ini under # cd/usr/local/php/lib
 Increase:
extension = "mssql.so"  

Restart the httpd service

Setting up the self-startup of Apache service

~]# chkconfig httpd on

error

Some pages open normally, others open and display source code to report errors.

At first, I thought it was a configuration environment problem. In fact, it was because I started with <?> instead of <? php?> under Linux when I wrote php web pages in windows environment.

Solution:

The first is: (compatible with php abbreviations, but <? XML?> may also be identified, and it is best to specify in advance in development)
In php.ini
short_open_tag = Off
 Modified to on

Second species:
Let the developer change <?> to <? Php?> recognizable under Linux.

Apache File Size Upload Limitation

~]# vim /etc/httpd24/httpd.conf
#Add the following at the end
LimitRequestBody 102400000

#Modify php.ini
~]# vim /usr/local/php/lib/php.ini
#Find: upload_max_filesize = 2M, the maximum permissible size of the upload file, changed to the required size
upload_max_filesize = 50M

Posted by mlnsharma on Tue, 01 Jan 2019 20:42:08 -0800