linux 下搭建php服务器

来源:互联网 发布:淘宝上男装剪标折扣店 编辑:程序博客网 时间:2024/06/06 18:58
防火墙设置


!!!!注意:防火墙删除部分在使用远程连接的时候或无法亲自操作服务器本机时请勿使用该命令


#查看防火墙规则
 iptables -L -n 


#清除预设表filter中的所有规则链的规则
 iptables -F


#清除预设表filter中使用者自定链中的规则
 iptables -X


#保存数据(如果不保存数据所有操作在重启后失效)
 /etc/rc.d/init.d/iptables save


#防火墙重启
service iptables restart


#当超出了IPTABLES里filter表里的两个链规则(INPUT,FORWARD)时,不在这两个规则里的数据包DROP(放弃).应该说这样配置是很安全的.控制流入数据包
#而对于OUTPUT链,也就是流出的包我们不用做太多限制,而是采取ACCEPT,也就是说,不在着个规则里的包怎么办呢,那就是通过.
 iptables -P INPUT DROP
 iptables -P OUTPUT ACCEPT
 iptables -P FORWARD DROP


#开启SSH的22号端口
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT


#开启WEB服务器80号端口
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 80 -j ACCEPT


#有需要可以开启MYSQL的3306端口
iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 3306 -j ACCEPT


#允许icmp包通过,也就是允许ping,
iptables -A OUTPUT -p icmp -j ACCEPT
iptables -A INPUT -p icmp -j ACCEPT
#域名无法解析时
iptables -A INPUT -p udp --sport 53 -j ACCEPT  
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT  
iptables -A INPUT -p udp --dport 53 -j ACCEPT  
iptables -A OUTPUT -p udp --sport 53 -j ACCEPT




#允许loopback!(不然会导致DNS无法正常关闭等问题)
iptables -A INPUT -i lo -p all -j ACCEPT
iptables -A OUTPUT -o lo -p all -j ACCEPT


#如:我们只允许192.168.0.3的机器进行SSH连接
iptables -A INPUT -s 192.168.0.3 -p tcp --dport 22 -j ACCEPT


/etc/rc.d/init.d/iptables save




#添加必要程序库
sudo -s
LANG=C
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers


###############   nginx1.5.2 安装 #########


wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.32.tar.gz
tar zxvf pcre-8.35.tar.gz
cd pcre-8.32
./configure --prefix=/usr/local/pcre-8.32
make && make install
cd ../


wget http://zlib.net/zlib-1.2.8.tar.gz


tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure --prefix=/usr/local/zlib-1.2.8
make && make install 


#建立用户及用户组


/usr/sbin/groupadd www
/usr/sbin/useradd -g www www
ulimit -SHn 65535




wget http://nginx.org/download/nginx-1.5.2.tar.gz
tar zxvf nginx-1.5.2.tar.gz
cd nginx-1.5.2
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/pcre-8.35 --with-zlib=/usr/local/zlib-1.2.8 --with-http_realip_module --with-http_image_filter_module
#其中--with-pcre=/usr/local/pcre-8.35,--with-zlib=/usr/local/zlib-1.2.8 为解压目录




##############  mysql5.6 安装#######




wget http://www.cmake.org/files/v3.1/cmake-3.1.3-Linux-i386.tar.gz


wget ftp://mirror.switch.ch/mirror/mysql/Downloads/MySQL-5.6/mysql-5.6.24.tar.gz




wget http://ftp.gnu.org/gnu/bison/bison-2.5.tar.gz
groupadd mysql
useradd mysql -g mysql -M -s /sbin/nologin


cmake ./ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/usr/local/conf -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DENABLE_DOWNLOADS=1
# Can't find messagefile '/usr/local/mysql/bin/mysql/share/errmsg.sys
#配置 my.cnf  [mysqld]language        = /usr/local/mysql/share/mysql/english


make&&make install


#初始化mysql表选项
scripts/mysql_install_db --user=mysql --language=/usr/local/mysql/share/english --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data  --user=mysql --force option


#将mysql的启动服务添加到系统服务中 
cp support-files/mysql.server  /etc/init.d/mysql 


#启动mysql
service mysql start 


#停止mysql服务
service mysql stop


#重启mysql服务
service mysql restart 




#修改默认root账户密码,默认密码为空
#修改密码 cd 切换到mysql所在目录
chkconfig --add mysql
./bin/mysqladmin -u root password






##############  php5.5安装#######


#安装PHP依赖库


mkdir -p /usr/local/plib/
wget http://www.ijg.org/files/jpegsrc.v9.tar.gz     
tar zxvf jpegsrc.v9.tar.gz
cd jpeg-9/
./configure --prefix=/usr/local/plib --enable-shared --enable-static --prefix=/usr/local/plib
make
make install
cd ../






wget http://prdownloads.sourceforge.net/libpng/libpng-1.6.2.tar.gz
tar zxvf libpng-1.6.2.tar.gz
cd libpng-1.6.2/
./configure --prefix=/usr/local/plib
make
make install
cd ../


wget http://download.savannah.gnu.org/releases/freetype/freetype-2.4.12.tar.gz
tar zxvf freetype-2.4.12.tar.gz
cd freetype-2.4.12/
./configure --prefix=/usr/local/plib
make
make install
cd ../


wget "http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz"
wget "http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz"
wget "http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz"


tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure --prefix=/usr/local/plib
make
make install
cd libltdl/
./configure --prefix=/usr/local/plib --enable-ltdl-install
make
make install
cd ../../


tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure --prefix=/usr/local/plib
make
make install
cd ../


vi /etc/ld.so.conf


/usr/local/plib/lib


ldconfig


tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
export LDFLAGS="-L/usr/local/pliblib -L/usr/lib"
export CFLAGS="-I/usr/local/plib/include -I/usr/include"
touch malloc.h
./configure --prefix=/usr/local/plib --with-libmcrypt-prefix=/usr/local/plib
make
make install
cd ../


#开始编译PHP


tar zxvf php-5.5.23.tar.gz
cd php-5.5.23/
export LIBS="-lm -ltermcap -lresolv"
export DYLD_LIBRARY_PATH="/usr/local/mysql/lib/:/lib/:/usr/lib/:/usr/local/lib:/lib64/:/usr/lib64/:/usr/local/lib64"
export LD_LIBRARY_PATH="/usr/local/mysql/lib/:/lib/:/usr/lib/:/usr/local/lib:/lib64/:/usr/lib64/:/usr/local/lib64"
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir --with-freetype-dir=/usr/local/plib --with-jpeg-dir=/usr/local/plib --with-png-dir=/usr/local/plib --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt=/usr/local/plib --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-opcache --with-pdo-mysql --enable-maintainer-zts


make
make install
cp php.ini-development /usr/local/php/etc/php.ini
cd ../


ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18
mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf


#测试启动php-cgi 
/usr/local/php/sbin/php-fpm




#添加开机启动
vi /etc/rc.d/rc.local
#添加
/usr/local/nginx/sbin/nginx
/usr/local/php/sbin/php-fpm


#参考 http://php.net/manual/zh/install.php
https://dev.mysql.com/doc/refman/5.6/en/source-installation.html