Você está na página 1de 13

How To Install Apache, PostrgeSQL, PHP, PhpPgAdmin on Centos 6.

xx
Update centos
yum y update

Disable SELINUX
sed s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/config

Reboot centos
Reboot

Remove epel-repo file and reinstall the lastest epel-repo file


rm -rf /var/cache/yum/x86_64/6/epel
yum remove epel-release
yum -y install epel-release

Install the Python library:


yum install pyliblzma

Apache + PHP-FPM on CentOS 6


Installation of additional repositories
We need to import the Dag GPG key:
rpm --import http://dag.wieers.com/rpm/packages/RPM-GPG-KEY.dag.txt

Now well install the rpmforge repo:


for 32bit:
wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.31.el6.rf.i686.rpm
rpm -ivh rpmforge-release-0.5.3-1.el6.rf.i686.rpm

for 64bit:
wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.31.el6.rf.x86_64.rpm

rpm -ivh rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm

As PHP-FPM cant be downloaded with the default CentOS repo we need


the EPEL and the REMI repo:
rpm --import https://fedoraproject.org/static/0608B895.txt

for 32bit:
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-68.noarch.rpm

for 64bit:
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-68.noarch.rpm

REMI:
rpm --import http://rpms.famillecollet.com/RPM-GPG-KEY-remi
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

After that we need to install a plugin for yum that manages repo priorities:
yum install yum-priorities

Then
nano /etc/yum.repos.d/epel.repo

Add priority=10 to the [epel] section and save the file


Do the same for REMI repo:
nano /etc/yum.repos.d/remi.repo

Install PostgreSQL9.4.4 Repo


rpm -Uvh http://yum.postgresql.org/9.4/redhat/rhel-7-x86_64/pgdg-centos94-9.41.noarch.rpm

Install PostgreSQL9.4.4 server , library ,php56w-pgsql, and postgresql9.4.4-devel

yum -y install postgresql94-server postgresql94-contrib postgresql94-libs


postgresql94-devel

Initialize postgresql database using following command:


su postgres
-bash-4.1$
-bash-4.1$ /usr/pgsql-9.4/bin/initdb -D /var/lib/pgsql/9.4/data
-bash-4.1$ /usr/pgsql-9.4/bin/postmaster &
-bash-4.1$ /usr/pgsql-9.4/bin/createdb test
-bash-4.1$
/usr/pgsql-9.4/bin/psql test

Restart PostgreSQL service and make it to start automatically on every reboot.


service postgresql-9.4 restart
chkconfig --levels 235

postgresql-9.4 on

Set postgres user password


Login to postgresql prompt,
su postgres
psql

.. and set postgres password with following command:


postgres=# \password postgres
Enter new password: 123
Enter it again: 123
postgres=# \q

Adjust Iptables/Firewall
Add the following line:

iptables -F
iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP

iptables
iptables
iptables
iptables
iptables
iptables
iptables
iptables
iptables
iptables
iptables
iptables
iptables
iptables
iptables
iptables
iptables
iptables

-A
-A
-A
-A
-A
-A
-A
-A
-A
-A
-A
-A
-A
-A
-A
-A
-A
-A

INPUT
INPUT
INPUT
INPUT
INPUT
INPUT
INPUT
INPUT
INPUT
INPUT
INPUT
INPUT
INPUT
INPUT
INPUT
INPUT
INPUT
INPUT

-p
-p
-i
-p
-p
-p
-p
-p
-p
-p
-p
-p
-p
-p
-p
-p
-p
-p

tcp ! --syn -m state --state NEW -j DROP


tcp --tcp-flags ALL ALL -j DROP
lo -j ACCEPT
tcp -m tcp --dport 22
-j ACCEPT
tcp -m tcp --dport 25
-j ACCEPT
tcp -m tcp --dport 80
-j ACCEPT
tcp -m tcp --dport 81
-j ACCEPT
tcp -m tcp --dport 82
-j ACCEPT
tcp -m tcp --dport 110
-j ACCEPT
tcp -m tcp --dport 143
-j ACCEPT
tcp -m tcp --dport 465
-j ACCEPT
tcp -m tcp --dport 587
-j ACCEPT
tcp -m tcp --dport 993
-j ACCEPT
tcp -m tcp --dport 995
-j ACCEPT
tcp -m tcp --dport 2525 -j ACCEPT
tcp -m tcp --dport 5432 -j ACCEPT
tcp -m state --state NEW -m tcp --dport 5432 -j ACCEPT
tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT

Save the firewall settings


Now that we have all the configuration in, we can list the rules to see if anything is missing..
iptables -L

Then save settings


iptables-save | sudo tee /etc/sysconfig/iptables

Restart iptables service.


service iptables restart

Note: To flush the filter use the following command


iptables -F

Adjust SELinux
Run the following command to make PostgreSQL work if SELinux enabled on your system.
setsebool -P httpd_can_network_connect_db 1

You may not login to PostegreSQL if you didnt run the above command.
Configure PostgreSQL-MD5 Authentication
nano /var/lib/pgsql/9.4/data/pg_hba.conf

Add or Modify the lines as shown below

[...]
# TYPE

DATABASE

USER

ADDRESS

# "local" is for Unix domain socket connections only


local
all
all
# IPv4 local connections:
host
all
all
127.0.0.1/32
host
all
all
192.168.1.0/24
# IPv6 local connections:
host
all
all
::1/128
[...]

METHOD
md5
md5
md5
md5

Restart postgresql service to apply the changes:


service postgresql-9.4 restart

Configure PostgreSQL-Configure TCP/IP


By default, TCP/IP connection is disabled, so that the users from another computers cant access
postgresql. To allow to connect users from another computers, Edit
file /var/lib/pgsql/9.4/data/postgresql.conf:
nano /var/lib/pgsql/9.4/data/postgresql.conf

Find the lines:


[...]
#listen_addresses = 'localhost'
[...]
#port = 5432
[...]

Uncomment both lines, and set the IP address of your postgresql server or set * to listen from
all clients as shown below:
listen_addresses = '*'
port = 5432

Restart postgresql service to save changes:


service postgresql-9.4 restart

Install PostgreSQL drivers with Yum


You should now be able to install your unixODBC rpm and PostgreSQL drivers with yum.
yum install postgresql94-odbc.x86_64 postgresql94-odbc-debuginfo.x86_64
unixODBC

If your repository is for a slightly different version of PostgreSQL, then you can use yum search
to find the exact rpm to install:
# yum search postgre | grep odbc
postgresql-odbc.x86_64 : PostgreSQL ODBC driver
postgresql93-odbc.x86_64 : PostgreSQL ODBC driver

Configure the PostgreSQL ODBC Driver in the Odbcinst.ini File


Our next task is to edit the /etc/odbcinst.ini file to configure the PostgreSQL driver. This tells
unixODBC where it can find the files specific to connecting to PostgreSQL database servers.
The following is the portion of my /etc/odbcinst.ini file. Make a similar section in your file:
[PostgreSQL]
Description = ODBC for PostgreSQL
Driver = /usr/pgsql-9.3/lib/psqlodbc.so
Setup = /usr/lib64/libodbcpsqlS.so
FileUsage = 1

Test Your ODBC Driver


Now that your driver is installed, we can test that the driver is installed using this command:
# odbcinst -q -d
[PostgreSQL]
[MySQL]

The above example shows us that we have ODBC drivers installed on this machine for
connecting to both MySQL and PostgreSQL.
Configure Our ODBC Connections in /etc/odbc.ini
Next, we configure the connection information specfic to our PostgreSQL database. If you
configure the /etc/odbc.ini file, it will work for all users on this machine. If we configure the
$HOME/.odbc.ini file, it will be configured for our specific user only. This is a security feature
that allows you to allow connections for only those users that merit it.

[sallydb]
Description
Driver
Database
Servername
UserName
Password
Port
Protocol
ReadOnly
RowVersioning
ShowSystemTables
ConnSettings

=
=
=
=
=
=
=
=
=
=
=
=

PostgreSQL connection to SallyDB


PostgreSQL
sallydb
10.2.3.4
sally
sallypassword
5432
9.3
No
No
No

Connect to PostgreSQL with ODBC isql Command


Now, that we have our configuration complete, we can check to see if we can connect.
unixODBC will have installed the isql command that allows us to login to our database using
the ODBC configuration we just completed.
# isql sallydb
+---------------------------------------+
| Connected!
|
|
|
| sql-statement
|
| help [tablename]
|
| quit
|
|
|
+---------------------------------------+
SQL>

Success! We were able to connect successfully. I can now query the database directly by typing
my SQL at the SQL> prompt.
unixODBC isql Command Usage
We can use isql with the following usage and options:
isql Usage:
isql DSN [UID [PWD]] [options]

isql Options:
-b
-dx
-x0xXX
-w
-c
-mn
-v
-lx

batch.(no prompting etc)


delimit columns with x
delimit columns with XX, where x is in hex, ie 0x09 is tab
wrap results in an HTML table
column names on first row. (only used when -d)
limit column display width to n
verbose.
set locale to x

-q
-3
-n
--version

wrap char fields in dquotes


Use ODBC 3 calls
Use new line processing
version

Other Linux ODBC Commands


Find out which files are used by unixODBC:
# odbcinst -j
unixODBC 2.2.14
DRIVERS............:
SYSTEM DATA SOURCES:
FILE DATA SOURCES..:
USER DATA SOURCES..:

/etc/odbcinst.ini
/etc/odbc.ini
/etc/ODBCDataSources
/root/.odbc.

Other Resources on unixODBC or PostgreSQL ODBC


There are many resources on the internet that can give you further information on PostgreSQL
ODBC or unixODBC. Below are several links:
http://www.unixodbc.org/odbcinst.html
http://asteriskdocs.org/en/3rd_Edition/asterisk-book-htmlchunk/installing_configuring_odbc.html
http://www.postgresql.org/message-id/3C68E705.5060806@itek.norut.no
http://www.flatmtn.com/article/setting-postgresql-odbc

Install Apache with:


yum install httpd mod_ssl httpd-tools openssl pcre-devel

Add it to services

chkconfig --levels 235 httpd on

Start the service:


service httpd start
Starting httpd: httpd: apr_sockaddr_info_get() failed for dc6.goil.lanhttpd:
Could not reliably determine the server's fully qualified domain name, using
127.0.0.1 for ServerName
echo 192.168.1.4 dc6.goil.lan >> /etc/hosts
service httpd restart

Apaches default document root is /var/www/html for CentOS. The


configuration file is /etc/httpd/conf/httpd.conf and additional
configurations are saved in the /etc/httpd/conf.d/ directory.
Add the latest PHP5.6 repo
rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm

Installation of PHP-FPM
Install PHP-FPM and mod_fastcgi:
yum -y install php-fpm mod_fastcgi php56w

Add PHP-FPM to services:


chkconfig --levels 235 php-fpm on

Start the service:


service php-fpm start

Restart Apache:
service httpd restart

Configuration of Apache with PHP-FPM


Open the fastcgi.conf file:
nano /etc/httpd/conf.d/fastcgi.conf

Add this to the end of the file:


DirectoryIndex index.html index.shtml index.cgi index.php
AddHandler php5-fcgi .php
Action php5-fcgi /php5-fcgi
Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -host 127.0.0.1:9000 -passheader Authorization

After that search after FastCgiWrapper and make sure its set to off then save the file.
The /usr/lib/cgi-bin/ directory must exist, so we create it:
mkdir /usr/lib/cgi-bin/

If mod_php is installed and enabled, we need to disable it so open the configuration at


/etc/httpd/conf.d/php.conf:
nano /etc/httpd/conf.d/php.conf

Comment out the AddHandler and AddType lines so it looks like here:
#
# PHP is an HTML-embedded scripting language which attempts to make it
# easy for developers to write dynamically generated webpages.
#
LoadModule php5_module modules/libphp5.so

LoadModule php5_module modules/libphp5-zts.so

#
# Cause the PHP interpreter to handle files with a .php extension.
#

#AddHandler php5-script .php


#AddType text/html .php
#
# Add index.php to the list of files that will be served as directory
# indexes.
#
DirectoryIndex index.php
#
# Uncomment the following line to allow PHP to pretty-print .phps
# files as PHP source code:
#
#AddType application/x-httpd-php-source .phps

Save the file and restart Apache:

service httpd restart

Install Postgres support for PHP


yum -y install php56w-opcache php56w-gd php56w-mcrypt php56w-pdo php56w-peclmemcache php56w-mbstring php56w-devel php56w-ldap phpdbg php56w-soap php56wpgsql php56w-xml php56w-xmlrpc

Restart PHP-FPM to pickup the new components:


service php-fpm restart

Now create the following PHP file in the document root /var/www/html of the default Apache
vhost:
nano /var/www/html/info.php

<?php

phpinfo();
?>

Save and exit.


Now we call that file in a browser (e.g. http://server ip/info.php):
optional
Open postgresql.conf
Change to postgres eqivalent
default-storage-engine = myisam
key_buffer = 1M
query_cache_size = 1M
query_cache_limit = 128k
max_connections=25
thread_cache=1
skip-innodb
query_cache_min_res_unit=0
tmp_table_size = 1M
max_heap_table_size = 1M
table_cache=256
concurrent_insert=2
max_allowed_packet = 1M
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 64K

Tunning FPM:
Open /etc/php-fpm.d/www.conf
Change
listen = 127.0.0.1:9000

to
listen = /tmp/php5-fpm.sock
listen.owner = apache
listen.group = apache

Open /etc/httpd/conf.d/fastcgi.conf
Change

FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -host 127.0.0.1:9000 -passheader Authorization

to
FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /tmp/php5-fpm.sock
-pass-header Authorization

Você também pode gostar