配置SSH
參考這篇<BandwagonHost VPS配置記錄>
#本地主機操作
#創(chuàng)建ssh配置文件园蝠,方便登陸
mkdir ~/.ssh //如果沒有.ssh目錄的話, 用該命令創(chuàng)建
cd ~/.ssh
vim config
#語法如下
Host testhost_root //登錄別稱
HostName 8.8.8.8 //主機IP
Port 2222 //主機端口號
User root //主機用戶名
#創(chuàng)建ssh公鑰和私鑰
ssh-kenygen //如果本地沒有ssh密鑰的話, 用該命令
#遠(yuǎn)程服務(wù)器操作
#登入root帳號
ssh testhost_root
#更新系統(tǒng)
yum update
#創(chuàng)建普通帳號
useradd -d /home/test -s /bin/bash -m test //創(chuàng)建用戶
passwd test //為用戶設(shè)置密碼, 需要根據(jù)系統(tǒng)的提示輸入兩次密碼
#為普通賬號賦予“sudo”權(quán)限
chmod u+w /etc/sudoers //為該文件條件寫權(quán)限
vi /etc/sudoers //打開該文件
在"root ALL=(ALL) ALL"后面添加"test ALL=(ALL) ALL"
注意: 用tab分隔; test為用戶名
chmod u-w /etc/sudoers //刪除該文件的寫權(quán)限
#ssh配置
cd /etc/ssh/
cp sshd_config sshd_config_backup //備份sshd_config
##修改sshd_config
vi sshd_config
#去掉這幾項的#
HostKey /etc/ssh/ssh_host_rsa_key
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
wq保存
#重啟
systemctl restart sshd.service
#切換到test賬戶
su test
cd ~
mkdir .ssh //沒有則創(chuàng)建
#退出遠(yuǎn)程服務(wù)器
exit
#本地主機操作
#創(chuàng)建普通賬號的ssh配置
vim ~/.ssh/config
Host testhost_test //按自己的情況來
HostName 8.8.8.8
User test
#上傳公鑰
cd ~/.ssh
scp id_rsa.pub testhost_test:~/.ssh/authorized_keys
#遠(yuǎn)程服務(wù)器操作
#登陸test帳號
ssh testhost_test
#權(quán)限(每個都有設(shè)置, test為用戶名)
chown -R test:test /home/test
chmod 700 /home/test
chmod 700 /home/test/.ssh
chmod 644 /home/test/.ssh/authorized_keys //公鑰文件的所有權(quán)限
SSH登錄的警告提示處理方法
參考CentOS 下解決ssh登錄 locale 警告
-bash: warning: setlocale: LC_CTYPE: cannot change locale (en_US.UTF-8): No such file or directory
#遠(yuǎn)程服務(wù)器操作
ssh testhost_test
sudo tee /etc/environment <<- 'EOF'
LANG=en_US.utf-8
LC_ALL=
EOF
source /etc/environment
* 生成 en_US.UTF-8 locale文件 CentOS沒有l(wèi)ocale-gen命令*
sudo localedef -v -c -i en_US -f UTF-8 en_US.UTF-8
配置防火墻
#遠(yuǎn)程服務(wù)器操作
CentOS 7.0默認(rèn)使用的是firewall作為防火墻节腐,改為iptables防火墻
#切換到root賬戶
su
輸入root密碼
#關(guān)閉firewall
systemctl stop firewalld.service //停止firewall
systemctl disable firewalld.service //禁止firewall開機啟動
#安裝iptables防火墻
yum install iptables-services
#設(shè)置iptables規(guī)則
iptables -L -n //查看iptables現(xiàn)有規(guī)則
iptables -P INPUT ACCEPT //先允許所有,不然有可能會杯具
iptables -F //清空所有默認(rèn)規(guī)則
iptables -X //清空所有自定義規(guī)則
iptables -Z //所有計數(shù)器歸0
iptables -A INPUT -i lo -j ACCEPT //允許來自于lo接口的數(shù)據(jù)包(本地訪問)
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT //允許接受本機請求之后的返回數(shù)據(jù) RELATED,是為FTP設(shè)置的
-A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT //開放21端口(FTP)
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT //開放22端口
-A INPUT -m state --state NEW -m tcp -p tcp --dport 19000:19500 -j ACCEPT //開放19000到19500的端口, FTP被動模式需要(端口范圍需要在vsftp配置中設(shè)定)
iptables -A INPUT -p tcp --dport 80 -j ACCEPT //開放80端口(HTTP)
iptables -A INPUT -p tcp --dport 443 -j ACCEPT //開放443端口(HTTPS)
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT //允許ping
iptables -P INPUT DROP //其他入站一律丟棄
iptables -P OUTPUT ACCEPT //所有出站一律綠燈
iptables -P FORWARD DROP //所有轉(zhuǎn)發(fā)一律丟棄
service iptables save //保存上述規(guī)則
#開啟iptables服務(wù)
systemctl enable iptables.service //注冊iptables服務(wù)
systemctl start iptables.service //開啟服務(wù)
systemctl status iptables.service //查看狀態(tài)
#關(guān)閉SELINUX
vi /etc/selinux/config
SELINUXTYPE=targeted //注釋掉(加上#)
setenforce 0 //使配置立即生效
安裝篇
安裝Apache
yum install httpd //根據(jù)提示跋炕,輸入Y安裝即可成功安裝
systemctl start httpd.service //啟動apache
systemctl enable httpd.service //設(shè)置apache開機啟動
#備用
systemctl stop httpd.service //停止apache
systemctl restart httpd.service //重啟apache
安裝MariaDB
yum install mariadb mariadb-server //安裝mariaDB
systemctl start mariadb.service //開啟MariaDB
mysql_secure_installation //配置mariaDB
回車荡澎,根據(jù)提示輸入Y
輸入2次密碼掉奄,回車
根據(jù)提示一路輸入Y
最后出現(xiàn):Thanks for using MySQL!
systemctl restart mariadb.service //重啟MariaDB
systemctl enable mariadb.service //設(shè)置開機啟動
#備用
systemctl stop mariadb.service //停止MariaDB
安裝PHP
yum install php //根據(jù)提示輸入Y直到安裝完成
yum install php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash //安裝PHP組件路鹰,使PHP支持 MariaDB
systemctl restart mariadb.service //重啟MariaDB
systemctl restart httpd.service //重啟apache
配置篇
Apache配置
vi /etc/httpd/conf/httpd.conf //編輯
Options Indexes FollowSymLinks
修改為:Options Includes ExecCGI FollowSymLinks //允許服務(wù)器執(zhí)行CGI及SSI, 禁止列出目錄
#AddHandler cgi-script .cgi
修改為:AddHandler cgi-script .cgi .pl //允許擴展名為.pl的CGI腳本運行
AllowOverride None
修改為:AllowOverride All //允許.htaccess
DirectoryIndex index.html
修改為:DirectoryIndex index.html index.htm default.html default.htm index.php //設(shè)置默認(rèn)首頁文件狰晚,增加index.php
:wq //保存退出
systemctl restart httpd.service //重啟apache
rm -f /etc/httpd/conf.d/welcome.conf /var/www/error/noindex.html //刪除默認(rèn)測試頁
PHP
vi /etc/php.ini //編輯
date.timezone 設(shè)置為 PRC //時區(qū)為中國
disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname
//禁用以上PHP函數(shù),提高安全性. 如果某些程序需要用到這個函數(shù)狼纬,可以刪除羹呵,取消禁用。
short_open_tag = ON //支持php短標(biāo)簽
open_basedir = .:..:/tmp/ //設(shè)置表示允許訪問當(dāng)前目錄(即PHP腳本文件所在目錄) 上級目錄 /tmp/目錄,可以防止php木馬跨站,如果改了之后安裝程序有問題(例如:織夢內(nèi)容管理系統(tǒng))疗琉,可以注銷此行
:wq //保存退出
systemctl restart mariadb.service //重啟MariaDB
systemctl restart httpd.service //重啟apache
MariaDB
cp /etc/my.cnf /etc/my.cnf_backup //備份配置文件
cp /usr/share/mysql/my-huge.cnf /etc/my.cnf //導(dǎo)入配置文件
vi /etc/my.cnf //編輯
#指定默認(rèn)字符集為utf8
在[mysqld]下加入
character-set-server=utf8
skip-character-set-client-handshake
//忽略客戶端的字符集冈欢,使用服務(wù)器的設(shè)置
//如果選擇不用skip語句
需要在[client]下加入
default-character-set=utf8
注意: skip語句和[client]中的default, 兩種方式選一個就好
:wq
systemctl restart mariadb.service //重啟MariaDB
額外篇
PHP升級到5.6以上的版本
php -v //我這里顯示的版本是5.4.16
#配置yum源
yum install epel-release
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
#安裝php5.6
yum install --enablerepo=remi --enablerepo=remi-php56 php php-opcache php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof
#重啟服務(wù)
systemctl restart mariadb.service //重啟MariaDB
systemctl restart httpd.service //重啟apache
php -v //查看下版本
從PHP5.5開始PHP代碼緩存從APC變成了Zend OPcache
搭建FTP服務(wù)器
#遠(yuǎn)程服務(wù)器操作
yum install vsftpd //安裝
useradd -d /var/ftp/test -g ftp -s /sbin/nologin testuser //建立FTP用戶
-d命令是指定用戶主目錄,-g是指定用戶分組盈简,-s /sbin/nologin 是禁止用戶登陸系統(tǒng)凑耻,最后testuser是本次新建用戶的用戶名
passwd testuser //設(shè)置該用戶設(shè)置密碼
vi /etc/vsftpd/vsftpd.conf //配置vsftpd
anonymous_enable=NO //禁止匿名登錄
idle_session_timeout=600 //超時, 踢出服務(wù)器 ,單位秒
connect_from_port_20=NO //指定FTP使用20端口進行數(shù)據(jù)傳輸
pasv_enable=YES
//若設(shè)置為YES,則使用PASV工作模式柠贤;若設(shè)置為NO香浩,則使用PORT模式。默認(rèn)值為YES臼勉,即使用PASV工作模式邻吭。
pasv_max_port=19500
//在PASV工作模式下,數(shù)據(jù)連接可以使用的端口范圍的最大端口坚俗,0 表示任意端口。默認(rèn)值為0岸裙。
pasv_min_port=19000
//在PASV工作模式下猖败,數(shù)據(jù)連接可以使用的端口范圍的最小端口,0 表示任意端口降允。默認(rèn)值為0恩闻。
chroot_local_user=YES
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list
allow_writeable_chroot=YES
//chroot_list文件中列出的用戶,可以切換到其他目錄剧董;未在文件中列出的用戶幢尚,不能切換到其他目錄
:wq //保存退出
vi /etc/vsftpd/chroot_list //將剛注冊的用戶名寫入
:wq //保存退出
#修改文件夾的讀寫權(quán)限
chown ftp /var/ftp/test
chmod 777 /var/ftp/test
service vsftpd start //開啟vsftpd