Linux Web服务器完整配置步骤(ubuntu 12.04+php+mysql+nginx+apache+mongodb+PureFTPd+jdk)

今天准备重新装一下服务器,so,完整步骤记录一下。 环境: ubuntu 12.04.4 Desktop 中文版 nginx 1.4.5 mysql 5.6.16 apache 2.4.7 jdk-7u51 mongodb 2.4.9 pure-ftpd 1.0.36 User manager for PureFTPd 2.1 都是64bit的,因为这个服务器是装在虚拟机上,主要是为了开发以及备份,so,选择了桌面版的ubuntu,但跟服务器版本没什么区别。 装好系统后 1,设置root密码,desktop默认是没有root密码的 sudo passwd root 2,更换软件源,我用了阿里的,怎么改参考: http://mirrors.aliyun.com/help/ubuntu 各位自己看着办,哪个快用哪个,搜狐,163都有。 改完更新源: sudo apt-get update 升级软件包,要不要升还是问一下服务器提供商吧(万一VPS升了启动不了,悲剧了) sudo apt-get upgrade 3,安装ssh服务(为了模拟真实的服务器,后面操作全用ssh) sudo apt-get install openssh-server 正常情况下,会自动启动ssh服务,输入 ps -e |grep ssh 会看到ssh-agent和sshd两个进程,不正常自己找原因。手工操作: sudo /etc/init.d/ssh {stop|start|restart} 下面的操作都用ssh进行。 ssh root@ip 4,安装screen screen的功能在于可以像用桌面一样,开启多个终端,同时执行多条命令,而且当因网络原因导致ssh断线后,正在执行的任务也不会终止。如果没有screen,编译源码之类的耗时操作也得开着ssh客户端,而且什么都干不了,干等。 apt-get install screen 通常用法,在执行其他操作前,先进screen: screen -S xxx xxx是一个会话的名字,screen支持建立多个会话。 此时自动进入xxx的会话,你可以执行任意命令,比如下载wget,可能需要时间很长,想回去先干点别的事,这时,可以: ctrl+a,d 先按ctrl+a,松开a,保持ctrl键按住,再按d键,此时,xxx会话就进入后台执行,你可以继续screen -S yyy创建其他会话。 或者,可以直接在xxx会话中screen -S yyy也行。 想要切换会话,可以先看会话列表: screen -ls 会显示所有会话(会话id.会话名) 再进入指定会话 screen -r 会话id或会话名 退出会话: exit 基本上记住以上几条命令就够用了。 5、往server传安装文件(直接在server上下载也行) scp -r /Users/xxx/Downloads/linux_server root@10.68.184.17:/root/ 这里传的是整个目录。 6,开始安装前,先确定我们的安装目录。 我的系统分区是/一个10G分区,/home一个50G分区,计划把服务器相关的软件以及数据都装在/home里。 软件装在/home/server/app/下,数据放/home/server/data/,日志放/home/server/data/,配置文件放/home/server/config/ 之所以不用各个软件默认的配置,是为了以后管理方便,以后备份服务器,只需要tar整个/home/server/目录就可以了。 创建目录: mkdir /home/server/ mkdir /home/server/app mkdir /home/server/data mkdir /home/server/config mkdir /home/server/config/httpd mkdir /home/server/config/mysql mkdir /home/server/config/mongodb mkdir /home/server/data/wwwroot mkdir /home/server/data/mysqldata mkdir /home/server/data/mongodbdata 下面如果有设置目录,但上面没创建的,请先创建,否则可能会失败。 后面三个分别是web目录mysql数据保存目录,mongodb数据保存目录。 7,安装jdk tar zxvf jdk-7u51-linux-x64.gz -C /home/server/app/ 解压出来后,路径为 /home/server/app/jdk1.7.0_51 配置环境变量,编辑/etc/rc.local(注:/etc/profile需要用户登录才会执行,所以不能放在profile文件里),我用的是vim,需要apt-get install一下,也要以用vi,在文件末尾加上: JAVA_HOME=/home/server/app/jdk1.7.0_51 export JRE_HOME=$JAVA_HOME/jre export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH 保存后,重新加载一下环境变量 source /etc/rc.local 再java -version,可以看下是否设置成功. 8,安装mysql 解压源码跳过… 安装依赖: sudo apt-get install cmake libssl-dev libncurses5-dev gcc g++ chkconfig 编译,安装: cmake -DCMAKE_INSTALL_PREFIX=/home/server/app/mysql -DSYSCONFDIR=/home/server/config/mysql -DMYSQL_DATADIR=/home/server/data/mysqldata -DMYSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock -DEXTRA_CHARSETS=all make make install 建立用户组及初始化数据库: groupadd mysql useradd -r -g mysql mysql chown -R mysql:mysql /home/server/app/mysql cd /home/server/app/mysql/ ./scripts/mysql_install_db --user=mysql --ldata=/home/server/data/mysqldata/ chown -R root:mysql /home/server/app/mysql chown -R mysql:mysql /home/server/data/mysqldata 复制编辑配置文件: cp /home/server/app/mysql/my.cnf /home/server/config/mysql/my.cnf 编辑my.cnf: [mysqld] datadir=/home/server/data/mysqldata default-storage-engine=MyISAM pid-file=/home/server/data/mysqldata/mysql.pid 复制启动脚本,开机自启: cp support-files/mysql.server /etc/init.d/mysql chmod 755 /etc/init.d/mysql chkconfig mysql on 如果chkconfig时报/sbin/insserv: 没有那个文件或目录,做个软链接 ln -s /usr/lib/insserv/insserv /sbin/insserv 启动mysql: service mysql start或/etc/init.d/mysql start 修改默认root密码 mysqladmin -u root password "newpass" 9,安装Apache apache依赖apr,apr-util,下载地址http://apr.apache.org/,当前版本1.5.3,PCRE:http://sourceforge.net/projects/pcre/files/pcre/,当前版本8.34 安装 #apr tar zxvf apr-1.5.0.tar.gz cd apr-1.5.0/ ./configure make make install #apr-util tar zxvf apr-util-1.5.3.tar.gz cd apr-util-1.5.3/ ./configure --prefix=/usr/local/apr-util make make install #PCRE tar zxvf pcre-8.34.tar.gz cd pcre-8.34/ ./configure make make install #软链接,网上有文章指32位用第一行,64位用第二行,我在64位系统上只用第二行无效,后面启动apache会报错 ln -s /usr/local/lib/libpcre.so.1 /lib ln -s /usr/local/lib/libpcre.so.1 /lib64 编译安装Apache: tar zxvf httpd-2.4.7.tar.gz cd httpd-2.4.7 ./configure --prefix=/home/server/app/apache --sysconfdir=/home/server/config/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --enable-mpms-shared=all --with-mpm=event make make install 编辑配置文件/home/server/config/httpd/httpd.conf,顶上加上两行: PidFile "/var/run/httpd.pid" ServerName localhost:8080 再把Listen 80换成8080,因为80要留给nginx. 复制启动文件: cp /home/server/app/apache/bin/apachectl /etc/init.d/httpd 启动apache: /etc/init.d/httpd start 可以通过 http://ip:8080/查看安装结果 10,安装mongodb 2.4.9(绿色免安装,解压即可): tar zxvf mongodb-linux-x86_64-2.4.9.tgz -C /home/server/app/ 创建配置文件: /home/server/config/mongodb/mongodb.conf: dbpath=/home/server/data/mongodbdata fork=true auth=true logpath=/home/server/app/mongodb-linux-x86_64-2.4.9/log.log logappend=true journal=true quiet=true pidfilepath=/home/server/data/mongodbdata/mongo.pid 启动mongodb: /home/server/app/mongodb-linux-x86_64-2.4.9/bin/mongod -config /home/server/config/mongodb/mongodb.conf 11,安装php 5.5.9 先装依赖: apt-get install libxml2 libxml2-dev libbz2-dev libcurl4-gnutls-dev libiconv需要手动编译装,下载地址:https://www.gnu.org/software/libiconv/#TOCdownloading 当前1.14 wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz tar zxvf libiconv-1.14.tar.gz cd libiconv-1.14/ ./configure make make install 以下是PHP常用到的lib,建议都装了. zlib(http://www.zlib.net/) libpng(http://sourceforge.net/projects/libpng/) freetype(http://download.savannah.gnu.org/releases/freetype/) libJpeg(http://www.ijg.org/files/) gd(https://bitbucket.org/libgd/gd-libgd/downloads) mcrypt(http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/) 安装方法都一样: tar zxvf xxxx.tar.gz cd xxxx ./configure make make install 正式安装: tar zxvf php-5.5.9.tar.gz cd php-5.5.9 ./configure --prefix=/home/server/app/php --with-config-file-path=/home/server/config/php --enable-fpm --enable-mbstring --enable-ftp --with-bz2 --with-jpeg-dir --with-png-dir --with-freetype-dir --with-libxml-dir --with-xmlrpc --with-zlib-dir --with-curl --enable-calendar --enable-sockets --enable-exif --with-iconv=/usr/local/lib --with-gd --enable-gd-native-ttf --with-apxs2=/home/server/app/apache/bin/apxs --with-mcrypt --with-mysql=/home/server/app/mysql --with-mysqli=/home/server/app/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock make make install 复制配置文件 cp php.ini-production /home/server/config/php/php.ini mv /home/server/app/php/etc/php-fpm.conf.default /home/server/app/php/etc/php-fpm.conf 添加用户组: groupadd www useradd -g www -s /sbin/nologin -M www 设置php运行的用户: vi /home/server/app/php/etc/php-fpm.conf user = www group = www 上面的方法装好php后,会自动修改apache的配置文件/home/server/config/httpd/httpd.conf,添加php5_module. 编辑这个配置文件,修改以下配置 User www Group www DocumentRoot "/home/server/data/wwwroot" Directory也要跟着DocumentRoot改,DirectoryIndex 添加index.php. 上面是配置www根目录是apache运行的用户,还需要在 LoadModule php5_module modules/libphp5.so 下面添加一行以支持php解析: AddType application/x-httpd-php .php 保存后,因为刚刚已经启动了apache,所以要重启一下 service httpd restart 创建/home/server/data/wwwroot/index.php:

<?php
echo phpinfo();
?>

再访问http://ip:8080/应该就能看到phpinfo的输出了. php作为apache的模块来运行,所以只能看到apache进程,看不到php进程。 12,安装mongodb的php驱动 下载地址:https://github.com/mongodb/mongo-php-driver/archive/master.zip 安装autoconf: apt-get install autoconf 安装 unzip mongo-php-driver-master.zip cd mongo-php-driver-master export PHP_PREFIX="/home/server/app/php/" $PHP_PREFIX/bin/phpize ./configure -with-php-config=$PHP_PREFIX/bin/php-config make make install install成功后提示: Installing shared extensions: /home/server/app/php/lib/php/extensions/no-debug-zts-20121212/ 编辑/home/server/config/php/php.ini,加上 extension="/home/server/app/php/lib/php/extensions/no-debug-zts-20121212/mongo.so" 重启一下apache,再刷新phpinfo页面,可以看到mongodb. 13,安装nginx ./configure --user=www --group=www --prefix=/home/server/app/nginx --conf-path=/home/server/config/nginx/nginx.conf --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_gzip_static_module --with-http_random_index_module --with-http_sub_module --with-http_dav_module --with-http_ssl_module --with-pcre=../pcre-8.34 make make install –with-pcre后面是pcre的源码,虽然前面已经装过,但在启动nginx时还是可能会报错,提示error while loading shared libraries: libpcre.so.1,按网上其他文章的方法ln做链接也没效果,但编译时指定源码路径解决了这个问题。 编辑/home/server/config/nginx/nginx.conf: 把A下面几行前的#号删除,apache端口改为8080: location ~ \.php$ { proxy_pass http://127.0.0.1:8080; } 再把root路径改为/home/server/data/wwwroot 启动nginx: /home/server/app/nginx/sbin/nginx 访问http://ip,可以看到一样的phpinfo()页面,说明配置成功 接下来配置权限: chown -R www:www /home/server/data/wwwroot/ chown -R mysql:mysql /home/server/data/mysqldata/ mongodb没找到指定用户来运行的方法,以后补上。 14、安装pure-ftpd(http://www.pureftpd.org/project/pure-ftpd)和User manager for PureFTPd(http://machiel.generaal.net/index.php?subject=user\_manager\_pureftpd&button=download): #不拷这些文件,configure会提示找不到mysqlclient cp /home/server/app/mysql/lib/libmysqlclient* /usr/lib/ ./configure --prefix=/home/server/app/pure-ftpd --with-mysql=/home/server/app/mysql/ CFLAGS=-O2 --with-quotas --with-cookie --with-virtualhosts --with-diraliases --with-sysquotas --with-ratios --with-altlog --with-paranoidmsg --with-shadow --with-welcomemsg --with-throttling --with-uploadscript --with-language=simplified-chinese make make install 把user manager for PureFTPd解压到/home/server/data/wwwroot/ftp下,通过http://ip/ftp/install.php访问安装。 在step3时,会报错,Create table admin Failed! Create table users Failed!无法创建表,这应该是个bug,但一直没解决,估计也不会解决了(V2.1是2005年发布的,都快10年了) 需要自己手动执行sql创建两个表(可以用phpmyadmin,安装过于简单,不介绍): CREATE TABLE IF NOT EXISTS `admin` ( `Username` varchar(35) NOT NULL DEFAULT '', `Password` char(32) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT '', PRIMARY KEY (`Username`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; CREATE TABLE IF NOT EXISTS `users` ( `User` varchar(16) NOT NULL DEFAULT '', `Password` varchar(32) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT '', `Uid` int(11) NOT NULL DEFAULT '14', `Gid` int(11) NOT NULL DEFAULT '5', `Dir` varchar(128) NOT NULL DEFAULT '', `QuotaFiles` int(10) NOT NULL DEFAULT '500', `QuotaSize` int(10) NOT NULL DEFAULT '30', `ULBandwidth` int(10) NOT NULL DEFAULT '80', `DLBandwidth` int(10) NOT NULL DEFAULT '80', `Ipaddress` varchar(15) NOT NULL DEFAULT '*', `Comment` tinytext, `Status` enum('0','1') NOT NULL DEFAULT '1', `ULRatio` smallint(5) NOT NULL DEFAULT '1', `DLRatio` smallint(5) NOT NULL DEFAULT '1', PRIMARY KEY (`User`), UNIQUE KEY `User` (`User`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; 一路到step6,复制Step B的文件内容到/home/server/config/pure-ftpd/pureftpd-mysql.conf。装完删除install.php. 复制源代码的configuration-file/pure-config.pl 到/home/server/app/pure-ftpd/sbin/pure-config.pl 编辑,修改第12行${exec_prefix}/sbin/pure-ftpd,改成: /home/server/app/pure-ftpd/sbin/pure-ftpd 其实for里的随便哪行都行,也可以删除只留一行,这里是在这些位置里查找pure-ftpd的安装目录,只留一个对的肯定是最快的,有性能强迫症的同学可以只留一行。 复制configuration-file/pure-ftpd.conf到/home/server/config/pure-ftpd/pure-ftpd.conf,并编辑MySQLConfigFile一行, 去掉#号,修改路径为/home/server/config/pure-ftpd/pureftpd-mysql.conf。 启动Pure-FTPd:/home/server/app/pure-ftpd/sbin/pure-config.pl /home/server/config/pure-ftpd/pureftpd-mysql.conf 启动脚本/root/webserver: #!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH export LC_ALL=C # Check if user is root if [ $(id -u) != "0" ]; then printf "Error: You must be root to run this script!\n" exit 1 fi NGINXDAEMON=/home/server/app/nginx/sbin/nginx NGINXPIDFILE=/home/server/app/nginx/logs/nginx.pid MONGODBDAEMON=/home/server/app/mongodb-linux-x86_64-2.4.9/bin/mongod MONGODBCONF=/home/server/config/mongodb/mongodb.conf APACHEPID=/var/run/httpd.pid APACHEDEMON=/home/server/app/apache/bin/httpd MYSQLPID=/home/server/data/mysqldata/mysql.pid MONGOPID=/home/server/data/mongodbdata/mongo.pid PUREPIDFILE="/var/run/pure-ftpd.pid" PURECONFIGPL="/home/server/app/pure-ftpd/sbin/pure-config.pl" PURECONFIGFILE="/home/server/config/pure-ftpd/pure-ftpd.conf" function_start() { echo "Starting Nginx" if test -e "$NGINXPIDFILE";then nginx_pid=`cat "$NGINXPIDFILE"` #如果pid存在,且进程活着 if (kill -0 $nginx_pid 2>/dev/null);then echo "Nginx is Running" else $NGINXDAEMON fi else $NGINXDAEMON fi echo "Starting Mongodb" if test -e "$MONGOPID";then mongo_pid=`cat "$MONGOPID"` #如果pid存在,且进程活着 if (kill -0 $mongo_pid 2>/dev/null);then echo "Mongodb is Running" else $MONGODBDAEMON --config $MONGODBCONF >>/dev/null fi else $MONGODBDAEMON --config $MONGODBCONF >>/dev/null fi echo "Starting Apache" if test -e "$APACHEPID";then apache_pid=`cat "$APACHEPID"` #如果pid存在,且进程活着 if (kill -0 $apache_pid 2>/dev/null);then echo "Apache is Running" else $APACHEDEMON -k start fi else $APACHEDEMON -k start fi echo "Starting Mysql" if test -e "$MYSQLPID";then mysql_pid=`cat "$MYSQLPID"` #如果pid存在,且进程活着 if (kill -0 $mysql_pid 2>/dev/null);then echo "Mysql is Running" else service mysql start fi else service mysql start fi echo "Starting pureftpd" if test -e "$PUREPIDFILE";then pure_pid=`cat "$PUREPIDFILE"` #如果pid存在,且进程活着 if (kill -0 $pure_pid 2>/dev/null);then echo "pureftpd is Running" else $PURECONFIGPL $PURECONFIGFILE --daemonize >>/dev/null fi else $PURECONFIGPL $PURECONFIGFILE --daemonize >>/dev/null fi } function_stop() { echo "Stoping Nginx" if test -e "$NGINXPIDFILE";then nginx_pid=`cat "$NGINXPIDFILE"` #如果pid存在,且进程活着 if (kill -0 $nginx_pid 2>/dev/null);then kill $nginx_pid else echo "Nginx is not running"; fi else echo "Nginx is not running"; fi echo "Stoping Mongodb" if test -e "$MONGOPID";then mongo_pid=`cat "$MONGOPID"` #如果pid存在,且进程活着 if (kill -0 $mongo_pid 2>/dev/null);then killall mongod else echo "Mongodb is not running"; fi else echo "Mongodb is not running"; fi echo "Stoping Apache" if test -e "$APACHEPID";then apache_pid=`cat "$APACHEPID"` #如果pid存在,且进程活着 if (kill -0 $apache_pid 2>/dev/null);then $APACHEDEMON -k stop else echo "Apache is not running"; fi else echo "Apache is not running"; fi echo "Stoping Mysql" if test -e "$MYSQLPID";then mysql_pid=`cat "$MYSQLPID"` #如果pid存在,且进程活着 if (kill -0 $mysql_pid 2>/dev/null);then service mysql stop else echo "Mysql is not running"; fi else echo "Mysql is not running"; fi echo "Stoping pureftpd" if test -e "$PUREPIDFILE";then pure_pid=`cat "$PUREPIDFILE"` #如果pid存在,且进程活着 if (kill -0 $pure_pid 2>/dev/null);then kill $pure_pid else echo "pureftpd is not running"; fi else echo "pureftpd is not running"; fi } function_status() { if test -e "$NGINXPIDFILE";then nginx_pid=`cat "$NGINXPIDFILE"` #如果pid存在,且进程活着 if (kill -0 $nginx_pid 2>/dev/null);then echo "Nginx is running"; else echo "Nginx is not running"; fi else echo "Nginx is not running"; fi if test -e "$MONGOPID";then mongo_pid=`cat "$MONGOPID"` #如果pid存在,且进程活着 if (kill -0 $mongo_pid 2>/dev/null);then echo "Mongodb is running"; else echo "Mongodb is not running"; fi else echo "Mongodb is not running"; fi if test -e "$APACHEPID";then apache_pid=`cat "$APACHEPID"` #如果pid存在,且进程活着 if (kill -0 $apache_pid 2>/dev/null);then echo "Apache is running"; else echo "Apache is not running"; fi else echo "Apache is not running"; fi if test -e "$MYSQLPID";then mysql_pid=`cat "$MYSQLPID"` #如果pid存在,且进程活着 if (kill -0 $mysql_pid 2>/dev/null);then echo "Mysql is running"; else echo "Mysql is not running"; fi else echo "Mysql is not running"; fi if test -e "$PUREPIDFILE";then pure_pid=`cat "$PUREPIDFILE"` #如果pid存在,且进程活着 if (kill -0 $pure_pid 2>/dev/null);then echo "pureftpd is running"; else echo "pureftpd is not running"; fi else echo "pureftpd is not running"; fi } if [ "$1" = "start" ]; then function_start elif [ "$1" = "stop" ]; then function_stop elif [ "$1" = "restart" ]; then function_stop function_start elif [ "$1" = "status" ]; then function_status else printf "Usage: /root/webserver {start|stop|restart|status}\n" fi 添加开机启动: vi /etc/rc.local 添加 /root/webserver start 重启生效