[color=rgb(0, 0, 0)]安装 —— CentOS 7 服务器 Nginx 环境推荐教程
[color=rgb(0, 0, 0)]一、系统约定 [color=rgb(0, 0, 0)]软件源代码包存放位置 /usr/local/src [color=rgb(0, 0, 0)]源码包编译安装位置(prefix) /usr/local/software_name [color=rgb(0, 0, 0)]脚本以及维护程序存放位置 /usr/local/sbin [color=rgb(0, 0, 0)]mysql 数据库位置 /var/lib/mysql(可按情况设置) [color=rgb(0, 0, 0)]网站根目录 /home/www/wwwroot(可按情况设置) [color=rgb(0, 0, 0)]虚拟主机日志根目录 /home/www/logs(可按情况设置) [color=rgb(0, 0, 0)]运行账户 www:www [color=rgb(0, 0, 0)]二、系统环境部署及调整 [color=rgb(0, 0, 0)]1、检查系统是否正常 [color=rgb(0, 0, 0)]# more /var/log/messages (检查有无系统级错误信息) [color=rgb(0, 0, 0)]# dmesg (检查硬件设备是否有错误信息) [color=rgb(0, 0, 0)]# ifconfig(检查网卡设置是否正确) [color=rgb(0, 0, 0)]# ping www.163.com (检查网络是否正常) [color=rgb(0, 0, 0)]# cat /proc/cpuinfo (检查CPU频率是否正常) [color=rgb(0, 0, 0)]# top (按1检测CPU核数是否正常,内存大小是否正常) [color=rgb(0, 0, 0)]2、关闭不需要的服务 [color=rgb(0, 0, 0)]# ntsysv [color=rgb(0, 0, 0)]以下仅列出需要启动的服务,未列出的服务一律推荐关闭: [color=rgb(0, 0, 0)]atd [color=rgb(0, 0, 0)]crond [color=rgb(0, 0, 0)]irqbalance [color=rgb(0, 0, 0)]microcode_ctl [color=rgb(0, 0, 0)]network [color=rgb(0, 0, 0)]sendmail [color=rgb(0, 0, 0)]sshd [color=rgb(0, 0, 0)]syslog [color=rgb(0, 0, 0)]关闭SElinux:修改/etc/selinux/config文件中的SELINUX= 为 disabled [color=rgb(0, 0, 0)]3、更换yum国内源 [color=rgb(0, 0, 0)]# cd /etc/yum.repos.d [color=rgb(0, 0, 0)]# mv CentOS-Base.repo CentOS-Base.repo.save [color=rgb(0, 0, 0)]# wget http://centos.ustc.edu.cn/CentOS-Base.repo.5 [color=rgb(0, 0, 0)]# mv CentOS-Base.repo.5 CentOS-Base.repo [color=rgb(0, 0, 0)]# yum clean all [color=rgb(0, 0, 0)]4、服务器时间检查和设置 [color=rgb(0, 0, 0)]#data (检查时间是否正确,是否是中国时间CST) [color=rgb(0, 0, 0)]#cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime (如果时区不对,则执行,时间正常的跳过) [color=rgb(0, 0, 0)]#yum -y install ntp (安装ntp对时工具) [color=rgb(0, 0, 0)]#chkconfig ntpd on (让对时服务开机启动) [color=rgb(0, 0, 0)]5、使用 yum 对系统进行更新并且安装必要软件包 [color=rgb(0, 0, 0)]#yum update –y [color=rgb(0, 0, 0)]#yum -y install make openssl openssl-devel pcre pcre-devel libpng libpng-devel libjpeg-6b libjpeg-devel-6b freetype freetype-devel gd gd-devel zlib zlib-devel gcc gcc-c++ libXpm libXpm-devel ncurses ncurses-devel libmcrypt libmcrypt-devel libxml2 libxml2-devel imake autoconf automake screen sysstat compat-libstdc++-33 curl curl-devel [color=rgb(0, 0, 0)]6. 重新启动系统 [color=rgb(0, 0, 0)]# init 6 [color=rgb(0, 0, 0)]三、编译安装L.A.M.P环境 [color=rgb(0, 0, 0)]1、下载软件(截止到09年10月的最新版本) [color=rgb(0, 0, 0)]# cd /usr/local/src [color=rgb(0, 0, 0)]#wget http://sysoev.ru/nginx/nginx-0.7.63.tar.gz [color=rgb(0, 0, 0)]#wget http://download.scientificlinux.net/nginx [color=rgb(0, 0, 0)]#wget http://download.scientificlinux.net/php-fpm.conf [color=rgb(0, 0, 0)]#wget http://download.scientificlinux.net/nginx.conf [color=rgb(0, 0, 0)]#wget http://download.scientificlinux.net/fcgi.conf [color=rgb(0, 0, 0)]#wget http://download.scientificlinux.net/php-5.2.10.tar.gz [color=rgb(0, 0, 0)]#wget http://download.scientificlinux.net/php-5.2.10-fpm-0.5.13.diff.gz [color=rgb(0, 0, 0)]#wget http://download.scientificlinux. ... glibc23-i386.tar.gz (32位系统) [color=rgb(0, 0, 0)]#wget http://download.scientificlinux. ... ibc23-x86_64.tar.gz (64位系统) [color=rgb(0, 0, 0)]#wget http://download.scientificlinux. ... i686-glibc23.tar.gz (32位系统) [color=rgb(0, 0, 0)]#wget http://download.scientificlinux. ... 6_64-glibc23.tar.gz (64位系统) [color=rgb(0, 0, 0)]2、安装MySQL [color=rgb(0, 0, 0)]cd /usr/local/src [color=rgb(0, 0, 0)]tar zxvf mysql-5.1.39-linux-i686-glibc23.tar.gz [color=rgb(0, 0, 0)]mv mysql-5.1.39-linux-i686-glibc23 /usr/local/ [color=rgb(0, 0, 0)]ln -s /usr/local/mysql-5.1.39-linux-i686-glibc23/ /usr/local/mysql [color=rgb(0, 0, 0)]groupadd mysql [color=rgb(0, 0, 0)]useradd -g mysql mysql [color=rgb(0, 0, 0)]chown -R mysql:mysql /usr/local/mysql [color=rgb(0, 0, 0)]chown -R mysql:mysql /usr/local/mysql-5.1.39-linux-i686-glibc23/ [color=rgb(0, 0, 0)]cd /usr/local/mysql [color=rgb(0, 0, 0)]./scripts/mysql_install_db --user=mysql [color=rgb(0, 0, 0)]cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld [color=rgb(0, 0, 0)]chmod 755 /etc/rc.d/init.d/mysqld [color=rgb(0, 0, 0)]chkconfig --add mysqld [color=rgb(0, 0, 0)]chkconfig --level 3 mysqld on [color=rgb(0, 0, 0)]cp ./support-files/my-huge.cnf /etc/my.cnf [color=rgb(0, 0, 0)]mv /usr/local/mysql/data /var/lib/mysql [color=rgb(0, 0, 0)]chown -R mysql:mysql /var/lib/mysql [color=rgb(0, 0, 0)]编辑/etc/my.cnf [color=rgb(0, 0, 0)]在 [mysqld] 段增加 [color=rgb(0, 0, 0)]datadir = /var/lib/mysql [color=rgb(0, 0, 0)]skip-innodb [color=rgb(0, 0, 0)]wait-timeout = 10 [color=rgb(0, 0, 0)]max_connections = 512 [color=rgb(0, 0, 0)]max_connect_errors = 10000000 [color=rgb(0, 0, 0)]在 [mysqld] 段修改 [color=rgb(0, 0, 0)]max_allowed_packet = 16M [color=rgb(0, 0, 0)]thread_cache_size = CPU个数*2 [color=rgb(0, 0, 0)]将 log-bin 注释 [color=rgb(0, 0, 0)]service mysqld start [color=rgb(0, 0, 0)]bin/mysqladmin -u root password 'password_for_root' [color=rgb(0, 0, 0)]其中引号内的password_for_root是要设置的root密码 [color=rgb(0, 0, 0)]3、安装Nginx [color=rgb(0, 0, 0)]cd /usr/local/src/ [color=rgb(0, 0, 0)]tar zxvf nginx-0.7.63.tar.gz [color=rgb(0, 0, 0)]cd nginx-0.7.63 [color=rgb(0, 0, 0)]./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --with-http_realip_module --with-http_addition_module --with-http_gzip_static_module --with-http_random_index_module --with-http_stub_status_module --with-http_sub_module --with-http_dav_module [color=rgb(0, 0, 0)]make [color=rgb(0, 0, 0)]make install [color=rgb(0, 0, 0)]cp /usr/local/src/nginx /etc/init.d/nginx [color=rgb(0, 0, 0)]chmod 755 /etc/init.d/nginx [color=rgb(0, 0, 0)]chkconfig --add nginx [color=rgb(0, 0, 0)]chkconfig nginx on [color=rgb(0, 0, 0)]4、安装PHP和Zend [color=rgb(0, 0, 0)]cd /usr/local/src [color=rgb(0, 0, 0)]tar zxvf php-5.2.10.tar.gz [color=rgb(0, 0, 0)]gzip -cd php-5.2.10-fpm-0.5.13.diff.gz | patch -d php-5.2.10 -p1 [color=rgb(0, 0, 0)]cd php-5.2.10 [color=rgb(0, 0, 0)]./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/etc/cgi --enable-mbstring --enable-ftp --with-gd --with-jpeg-dir=/usr --with-png-dir=/usr --enable-magic-quotes --with-mysql=/usr/local/mysql --with-pear --enable-sockets --with-ttf --with-freetype-dir=/usr --enable-gd-native-ttf --with-zlib --enable-sysvsem --enable-sysvshm --with-libxml-dir=/usr --enable-force-cgi-redirect --enable-fastcgi --with-xmlrpc --enable-zip --enable-fpm [color=rgb(0, 0, 0)]make [color=rgb(0, 0, 0)]make install [color=rgb(0, 0, 0)]mkdir -p /usr/local/etc/cgi/ [color=rgb(0, 0, 0)]cp php.ini-dist /usr/local/etc/cgi/php.ini [color=rgb(0, 0, 0)]编辑/usr/local/etc/cgi/php.ini [color=rgb(0, 0, 0)]找到disable_functions =这行,在=号后面加上exec,system,passthru,shell_exec,escapeshellarg,escapeshellcmd,proc_close,proc_open,dl,popen,show_source [color=rgb(0, 0, 0)]找到; cgi.fix_pathinfo=0这行去掉前面;号,如果这行=号后面不是0,则修改成0 [color=rgb(0, 0, 0)]mv -f /usr/local/src/php-fpm.conf /usr/local/php5/etc/php-fpm.conf [color=rgb(0, 0, 0)]groupadd www [color=rgb(0, 0, 0)]useradd -g www www [color=rgb(0, 0, 0)]echo 'ulimit -SHn 65535' >> /etc/rc.local [color=rgb(0, 0, 0)]echo '/usr/local/php5/sbin/php-fpm start' >> /etc/rc.local [color=rgb(0, 0, 0)]cd /usr/local/src [color=rgb(0, 0, 0)]tar zxvf ZendOptimizer-3.3.3-linux-glibc23-i386.tar.gz [color=rgb(0, 0, 0)]cd ZendOptimizer-3.3.3-linux-glibc23-i386 [color=rgb(0, 0, 0)]./install [color=rgb(0, 0, 0)](注意第一个要填的路径是Zend安装路径,第二个是php.ini所在的路径,即/usr/local/etc/cgi) [color=rgb(0, 0, 0)](不要选重启apache) [color=rgb(0, 0, 0)]5、启动Nginx和php [color=rgb(0, 0, 0)]mv -f /usr/local/src/fcgi.conf /usr/local/nginx/conf/ [color=rgb(0, 0, 0)]cp -f /usr/local/src/nginx.conf /usr/local/nginx/conf/nginx.conf [color=rgb(0, 0, 0)]mkdir -p /home/www/wwwroot [color=rgb(0, 0, 0)]ulimit -SHn 65535 [color=rgb(0, 0, 0)]/usr/local/php5/sbin/php-fpm start [color=rgb(0, 0, 0)]service nginx start [color=rgb(0, 0, 0)]在/home/www/wwwroot放入一个index.php,内容为 [color=rgb(0, 0, 0)]打开浏览器访问,即可看到phpinfo页面 [color=rgb(0, 0, 0)]6、设置系统防火墙 [color=rgb(0, 0, 0)]编辑/usr/local/sbin/fw.sh [color=rgb(0, 0, 0)]复制以下内容进去 [color=rgb(0, 0, 0)]#!/bin/bash [color=rgb(0, 0, 0)]# Stop iptables service first [color=rgb(0, 0, 0)]service iptables stop [color=rgb(0, 0, 0)]# Load FTP Kernel modules [color=rgb(0, 0, 0)]/sbin/modprobe ip_conntrack_ftp [color=rgb(0, 0, 0)]/sbin/modprobe ip_nat_ftp [color=rgb(0, 0, 0)]# Inital chains default policy [color=rgb(0, 0, 0)]/sbin/iptables -F -t filter [color=rgb(0, 0, 0)]/sbin/iptables -P INPUT DROP [color=rgb(0, 0, 0)]/sbin/iptables -P OUTPUT ACCEPT [color=rgb(0, 0, 0)]# Enable Native Network Transfer [color=rgb(0, 0, 0)]/sbin/iptables -A INPUT -i lo -j ACCEPT [color=rgb(0, 0, 0)]# Accept Established Connections [color=rgb(0, 0, 0)]/sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT [color=rgb(0, 0, 0)]# ICMP Control [color=rgb(0, 0, 0)]/sbin/iptables -A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT [color=rgb(0, 0, 0)]# WWW Service [color=rgb(0, 0, 0)]/sbin/iptables -A INPUT -p tcp --dport 80 -j ACCEPT [color=rgb(0, 0, 0)]# FTP Service [color=rgb(0, 0, 0)]/sbin/iptables -A INPUT -p tcp --dport 21 -j ACCEPT [color=rgb(0, 0, 0)]# SSH Service [color=rgb(0, 0, 0)]/sbin/iptables -A INPUT -p tcp --dport 22 -j ACCEPT [color=rgb(0, 0, 0)]退出编辑,执行以下命令 [color=rgb(0, 0, 0)]# chmod 755 /usr/local/sbin/fw.sh [color=rgb(0, 0, 0)]# echo '/usr/local/sbin/fw.sh' >> /etc/rc.local [color=rgb(0, 0, 0)]# /usr/local/sbin/fw.sh |