分类 服务器技术 下的文章

镜像下载

centos官方 Aliyun Open Source Mirror Site 网易开源镜像站

虚拟化工具

免费的virtualbox,下载地址 KVM Xen

安装

记一次centos最小安装 centos7.0 的最小化安装

优化

分享一个centos6.*优化脚本 CentOS精简命令

后续

关闭防火墙和SELinux

# 关闭防火墙
service iptables stop 
chkconfig iptables off
chkconfig ip6tables off
# SELinux
vi /etc/selinux/config
# change
SELINUX=disabled
#网卡
------
vi /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
# change
ONBOOT=yes
NM_CONTROLLED=yes
# change
BOOTPROTO=dhcp
or
BOOTPROTO=static
# 加上
# 静态IP
IPADDR=192.168.1.107
# 设置子网
NETMASK=255.255.255.0
# 设置网关
GATEWAY=192.168.1.1
# 设置DNS
DNS1=114.114.114.114
IPV6INIT=no
USERCTL=no
------
service network restart
chkconfig network on
------
# 全局关闭IPv6
vi /etc/sysctl.conf
#加上
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
------
sysctl -p
reboot

crontab

yum install -y vixie-cron
yum install -y crontabs
service crond start

MySQL

yum install -y mysql-server
mysql_db_install
mysql_secure_installation
service mysqld start

ntpdate

yum install -y ntpdate rdate
ntpdate 210.72.145.44

nginx

nginx配置详解

PHP

编译PHP5.6

# 慎用
yum install -y zabbix zabbix-get zabbix-server zabbix-web-mysql zabbix-web zabbix-agent 

打开错误:

ini_set( 'display_errors', 'On' );

编译安装nginx后,没有将nginx配置为服务,则无法使用诸如service nginx restart的命令,下面我们看看如何将其配置为服务。

编写脚本

vi /etc/init.d/nginx

写入以下内容,并修改nginx路径

#!/bin/sh   
#   
# nginx - this script starts and stops the nginx daemon   
#   
# chkconfig: - 85 15   
# description: Nginx is an HTTP(S) server, HTTP(S) reverse   
# proxy and IMAP/POP3 proxy server   
# processname: nginx   
# chkconfig: 2345 90 91   
# description: nginx web server  
# processname: nginx  
# config: /opt/nginx/conf/nginx.conf  
# pidfile: /opt/nginx/nginx.pid  
  
# Source function library.  
. /etc/init.d/functions  
  
# Source networking configuration.  
. /etc/sysconfig/network  
  
  
if [ -f /etc/sysconfig/nginx ];then  
. /etc/sysconfig/nginx  
fi  
  
# Check that networking is up.   
[ "$NETWORKING" = "no" ] && exit 0  
  
nginx="#改为nginx二进制的路径"   
prog=$(basename $nginx)  
  
NGINX_CONF_FILE="/etc/nginx/nginx.conf"  
  
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx  
  
lockfile=/var/lock/subsys/nginx  
  
start() {   
[ -x $nginx ] || exit 5   
[ -f $NGINX_CONF_FILE ] || exit 6   
echo -n $"Starting $prog: "   
daemon $nginx #-c $NGINX_CONF_FILE   
retval=$?   
echo   
[ $retval -eq 0 ] && touch $lockfile   
return $retval   
}  
  
stop() {   
echo -n $"Stopping $prog: "   
killproc $prog -QUIT   
retval=$?   
echo   
[ $retval -eq 0 ] && rm -f $lockfile   
return $retval   
killall -9 nginx   
}  
  
restart() {   
configtest || return $?   
stop   
sleep 1   
start   
}  
  
reload() {   
configtest || return $?   
echo -n $"Reloading $prog: "   
killproc $nginx -HUP   
RETVAL=$?   
echo   
}  
  
force_reload() {   
restart   
}  
  
configtest() {   
$nginx -t #-c $NGINX_CONF_FILE   
}  
  
rh_status() {   
status $prog   
}  
  
rh_status_q() {   
rh_status >/dev/null 2>&1   
}  
  
case "$1" in   
start)   
    rh_status_q && exit 0   
    $1   
    ;;   
stop)   
rh_status_q || exit 0   
    $1   
    ;;   
restart)   
    $1   
    ;;   
test)   
    configtest   
    ;;   
reload)   
    rh_status_q || exit 7   
    $1   
    ;;   
force-reload)   
    force_reload   
    ;;   
status)   
    rh_status   
    ;;   
condrestart|try-restart)   
    rh_status_q || exit 0   
    ;;   
*)   
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|test}"   
exit 2   
esac

给予执行权

chmod a+x /etc/init.d/nginx

添加并打开服务

chkconfig --add nginx
chkconfig nginx on

测试

service nginx start
service nginx stop 
service nginx reload

转自:

http://binyan17.iteye.com/blog/1688308

自从使用mac以后,ssh连接总是超时成了老高的一个心病啊,只要一小会儿不动电脑,远程就失去相应,或者干脆直接提示broken pipe,然后断掉。现在终于有了算是能接受的解决办法。

方法一:直截了当砍掉法

~.,是的,你没有看错!这个组合命令就是主动断开ssh的命令,不行自己问问man。

方法二:修改配置文件

服务器和客户端每隔一定的时间会发一个KeepAlive请求,避免因为路由器或线路的限制而断线,例子中的60s可以因具体的环境改变。

针对服务器端

修改/etc/ssh/sshd_config,修改ServerAliveInterval一项,而ServerAliveCountMax表示如果失败重试的次数

ServerAliveInterval 60
ServerAliveCountMax 3

针对客户端

vi /etc/ssh_config
ClientAliveInterval 60
ClientAliveCountMax 3

scp命令经常用在主机之间拷贝文件用,但是如果目标机器的sshd的端口不是默认的22,怎么办呢?

scp -P 1111 [email protected]:/home/xxx

这样就可以配合ssh-keygen -t rsa命令将生成的pub重命名为authorized_keys,并拷贝至目标机器的~/.ssh/文件夹下,这样以后就可以无密码登录ssh了。

安装

yum install -y vsftpd

安全配置

vi /etc/vsftpd/vsftpd.conf
#add
listen_port= XXXX
anonymous_enable=NO
local_enable=NO
chroot_local_user=YES
chroot_list_enable=NO

日志配置

xferlog_enable=YES
xferlog_std_format=YES 
xferlog_file=/var/log/xferlog   
dual_log_enable=YES 
vsftpd_log_file=/var/log/vsftpd.log  

添加虚拟用户

vi /etc/vsftpd/user_list.txt
#add
roooooooter
random_passwd
#生成认证文件
db_load -T -t hash -f /etc/vsftpd/user_list.txt /etc/vsftpd/user.db
#删除明文
rm -f vi /etc/vsftpd/user_list.txt
#编辑vsftpd的PAM认证文件
#删除其他行,加入下面两行
#pam_userdb.so 为认证库文件 db即上文user.db
vi /etc/pam.d/vsftpd
auth required pam_userdb.so db=/etc/vsftpd/user
account required pam_userdb.so db=/etc/vsftpd/user
#设定FTP用户映射(注意权限)
useradd –d /home/xxx –s /sbin/nologin xxx
chmod 700 /home/xxx
#开启虚拟用户
vi /etc/vsftpd/vsftpd.conf
#开启虚拟用户
guest_enable=YES
#FTP虚拟用户对应的系统用户
guest_username=xxx
#PAM认证文件
pam_service_name=user
#虚拟用户配置文件
user_config_dir=/etc/vsftpd/vsftpd_user_conf

配置虚拟用户

mkdir -p /etc/vsftpd/vsftpd_user_conf
#xxx为虚拟用户名
vi /etc/vsftpd/vsftpd_user_conf/xxx
#设置权限
local_root=/home/xxx
write_enable=YES #开放markwang的写权限
anon_world_readable_only=NO #开放markwang的下载权限
anon_upload_enable=YES #开放markwang的上传权限
anon_mkdir_write_enable=YES #开放markwang创建目录的权限
anon_other_write_enable=YES #开放markwang删除和重命名的权限

部分配置说明

virtual_use_local_privs

当virtual_use_local_privs=YES时,虚拟用户和本地用户有相同的权限;
当virtual_use_local_privs=NO时,虚拟用户和匿名用户有相同的权限,默认是NO。
 
当virtual_use_local_privs=YES,write_enable=YES时,虚拟用户具有写权限(上传、下载、删除、重命名)。
 
当virtual_use_local_privs=NO,write_enable=YES,anon_world_readable_only=YES,
anon_upload_enable=YES时,虚拟用户不能浏览目录,只能上传文件,无其他权限。
 
当virtual_use_local_privs=NO,write_enable=YES,anon_world_readable_only=NO,
anon_upload_enable=NO时,虚拟用户只能下载文件,无其他权限。
 
当virtual_use_local_privs=NO,write_enable=YES,anon_world_readable_only=NO,
anon_upload_enable=YES时,虚拟用户只能上传和下载文件,无其他权限。
 
当virtual_use_local_privs=NO,write_enable=YES,anon_world_readable_only=NO,
anon_mkdir_write_enable=YES时,虚拟用户只能下载文件和创建文件夹,无其他权限。
 
当virtual_use_local_privs=NO,write_enable=YES,anon_world_readable_only=NO,
anon_other_write_enable=YES时,虚拟用户只能下载、删除和重命名文件,无其他权限。

其他

anonymous_enable=YES            允许匿名登录local_enable=YES                  允许本地用户登录 
write_enable=YES                      开放本地用户写权限 
local_umask=022                        设置本地用户生成文件的掩码为022 
#anon_upload_enable=YES          此项设置允许匿名用户上传文件 
#anon_mkdir_write_enable=YES  开启匿名用户的写和创建目录的权限 
dirmessage_enable=YES            当切换到目录时,显示该目录下的.message隐藏文件的内容 
xferlog_enable=YES                    激活上传和下载日志 
connect_from_port_20=YES        启用FTP数据端口的连接请求 
#chown_uploads=YES                是否具有上传权限.  用户由chown_username参数指定。 
#chown_username=whoever        指定拥有上传文件权限的用户。此参数与chown_uploads联用。 
#xferlog_file=/var/log/vsftpd.log 
xferlog_std_format=YES              使用标准的ftpd xferlog日志格式 
#idle_session_timeout=600          此设置将在用户会话空闲10分钟后被中断 
#data_connection_timeout=120    将在数据连接空闲2分钟后被中断 
#ascii_upload_enable=YES        启用上传的ASCII传输方式 
#ascii_download_enable=YES    启用下载的ASCII传输方式 
#ftpd_banner=Welcome to blah FTP service 设置用户连接服务器后显示消息 
#deny_email_enable=NO  此参数默认值为NO。当值为YES时,拒绝使用banned_email_file参数指定文件中所列出的e-mail地址用户登录。 
#banned_email_file=/etc/vsftpd.banned_emails 指定包含拒绝的e-mail地址的文件. 
#chroot_list_enable=YES    设置本地用户登录后不能切换到自家目录以外的别的目录 
#chroot_list_file=/etc/vsftpd.chroot_list 
#ls_recurse_enable=YES 
pam_service_name=vsftpd 设置PAM认证服务的配置文件名称,该文件存放在/etc/pam.d/ 
userlist_enable=YES    此项配置/etc/vsftpd.user_list中指定的用户也不能访问服务器,若添加userlist_deny=No,则仅仅/etc /vsftpd.user_list文件中的用户可以访问,其他用户都不可以访问服务器。如过 userlist_enable=NO,userlist_deny=YES,则指定使文件/etc/vsftpd.user_list中指定的用户不可以访问服务器,其他本地用户可以访问服务器。 
listen=YES              指明VSFTPD以独立运行方式启动 
tcp_wrappers=YES        在VSFTPD中使用TCP_Wrappers远程访问控制机制,默认值为YES

参考

http://www.cnblogs.com/welkinwalker/archive/2010/04/14/1711880.html http://yuanbin.blog.51cto.com/363003/129071

删除自带web、数据库、php

yum remove httpd* php* mysql-server mysql* php-mysql -y

删除不需要的程序

yum remove Deployment_Guide-en-US finger cups-libs cups ypbind bluez-libs desktop-file-utils ppp rp-pppoe wireless-tools irda-utils sendmail* samba* talk-server finger-server bind* xinetd nfs-utils nfs-utils-lib rdate fetchmail eject ksh mkbootdisk mtools syslinux tcsh startup-notification talk apmd rmt dump setserial portmap yp-tools -y
yum groupremove "Mail Server" "Games and Entertainment" "X Window System" "X Software Development" "Development Libraries" "Dialup Networking Support" "Games and Entertainment" "Sound and Video" "Graphics" "Editors" "Text-based Internet" "GNOME Desktop Environment" "GNOME Software Development" -y
yum -y groupremove "FTP Server" "PostgreSQL Database client" "PostgreSQL Database server" "MySQL Database server" "MySQL Database client" "Web Server" "Office Suite and Productivity" "E-mail server" "Ruby Support" "Printing client"

关闭selinux

setenforce 0
sed -i 's/^SELINUX=.*$/SELINUX=disabled/' /etc/selinux/config

对系统进行一些优化

sed -i 's/^id:.*$/id:3:initdefault:/' /etc/inittab
init q
[ -z "`cat ~/.bashrc | grep ^PS1`" ] && echo 'PS1="[e[37;40m][[e[32;40m]u[e[37;40m]@h [e[35;40m]W[e[0m]]\$ "' >> ~/.bashrc
sed -i 's/^HISTSIZE=.*$/HISTSIZE=10/' /etc/profile
[ -z "`cat ~/.bashrc | grep history-timestamp`" ] && echo "export PROMPT_COMMAND='{ msg=$(history 1 | { read x y; echo $y; });user=$(whoami); echo $(date "+%Y-%m-%d %H:%M:%S"):$user:`pwd`/:$msg ---- $(who am i); } >> /tmp/`hostname`.`whoami`.history-timestamp'" >> ~/.bashrc
[ -z "`cat /etc/security/limits.conf | grep 'nproc 65535'`" ] && cat >> /etc/security/limits.conf <> /etc/rc.local
[ "$(hostname -i | awk '{print $1}')" != "127.0.0.1" ] && sed -i "s@^127.0.0.1(.*)@127.0.0.1   `hostname` 1@" /etc/hosts
[ -z "`cat /etc/pam.d/system-auth | grep 'pam_tally2.so'`" ] && sed -i '4a auth        required      pam_tally2.so deny=5 unlock_time=180' /etc/pam.d/system-auth

输入vi自动打开vim

[ -z "`cat ~/.bashrc | grep 'alias vi='`" ] && sed -i "s@alias mv=(.*)@alias mv=1nalias vi=vim@" ~/.bashrc && echo 'syntax on' >> /etc/vimrc

升级系统

yum -y update
yum clean all

美化命令行,其实在之前优化那里就美化了,这里只是让它生效而已

. /etc/profile
. ~/.bashrc

删除不需要的服务,这里自行考虑,反正我是只保留几个的~

service modules_dep stop
chkconfig modules_dep off
chkconfig --del modules_dep
service netconsole stop
chkconfig netconsole off
chkconfig --del netconsole
service netfs stop
chkconfig netfs off
chkconfig --del netfs
service nscd stop
chkconfig nscd off
chkconfig --del nscd
service quota_nld stop
chkconfig quota_nld off
chkconfig --del quota_nld
service rdisc stop
chkconfig rdisc off
chkconfig --del rdisc
service restorecon stopd
chkconfig restorecond off
chkconfig --del restorecond
service saslauthd stop
chkconfig saslauthd off
chkconfig --del saslauthd
service snmpd stop
chkconfig snmpd off
chkconfig --del snmpd
service snmptrapd stop
chkconfig snmptrapd off
chkconfig --del snmptrapd
service gpm stop
chkconfig gpm off
chkconfig --del gpm
service iscsi stop
chkconfig iscsi off
chkconfig --del iscsi
service iscsid stop
chkconfig iscsid off
chkconfig --del iscsid
service lm_sensors stop
chkconfig lm_sensors off
chkconfig --del lm_sensors
service lvm2-monitor stop
chkconfig lvm2-monitor off
chkconfig --del lvm2-monitor
service mcstrans stop
chkconfig mcstrans off
chkconfig --del mcstrans
service messagebus stop
chkconfig messagebus off
chkconfig --del messagebus
service multipathd stop
chkconfig multipathd off
chkconfig --del multipathd
service netconsole stop
chkconfig netconsole off
chkconfig --del netconsole
service netfs stop
chkconfig netfs off
chkconfig --del netfs
service netplugd stop
chkconfig netplugd off
chkconfig --del netplugd
service nscd stop
chkconfig nscd off
chkconfig --del nscd
service rawdevices stop
chkconfig rawdevices off
chkconfig --del rawdevices
service rdisc stop
chkconfig rdisc off
chkconfig --del rdisc
service restorecond stop
chkconfig restorecond off
chkconfig --del restorecond
service ntpd stop
chkconfig ntpd off
chkconfig --del ntpd
service ip6tables stop
chkconfig ip6tables off
chkconfig --del ip6tables
service ntpdate stop
chkconfig ntpdate off
chkconfig --del ntpdate
service portreserve stop
chkconfig portreserve off
chkconfig --del portreserve
service udev-post stop
chkconfig udev-post off
chkconfig --del udev-post
service exim stop
chkconfig exim off
chkconfig --del exim

转自http://shuang.ca/centos-optimizer/