美文网首页
CentOS 7.0 配置LAMP环境记实

CentOS 7.0 配置LAMP环境记实

作者: Herbertzz | 来源:发表于2016-11-17 16:32 被阅读47次

配置SSH

参考这篇<BandwagonHost VPS配置记录>

#本地主机操作

#创建ssh配置文件,方便登陆
mkdir ~/.ssh //如果没有.ssh目录的话, 用该命令创建
cd ~/.ssh
vim config 

#语法如下 
Host testhost_root  //登录别称
  HostName 8.8.8.8  //主机IP
  Port 2222  //主机端口号
  User root  //主机用户名

#创建ssh公钥和私钥
ssh-kenygen  //如果本地没有ssh密钥的话, 用该命令
#远程服务器操作

#登入root帐号
ssh testhost_root

#更新系统
yum update

#创建普通帐号
useradd -d /home/test -s /bin/bash -m test  //创建用户 
passwd test   //为用户设置密码, 需要根据系统的提示输入两次密码

#为普通账号赋予“sudo”权限 
chmod u+w /etc/sudoers  //为该文件条件写权限
vi /etc/sudoers  //打开该文件
  在"root  ALL=(ALL)  ALL"后面添加"test  ALL=(ALL)  ALL"
  注意: 用tab分隔; test为用户名
chmod u-w /etc/sudoers  //删除该文件的写权限

#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 //没有则创建

#退出远程服务器
exit
#本地主机操作

#创建普通账号的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
#远程服务器操作

#登陆test帐号
ssh testhost_test

#权限(每个都有设置, test为用户名)
chown -R test:test /home/test
chmod 700 /home/test
chmod 700 /home/test/.ssh
chmod 644 /home/test/.ssh/authorized_keys //公钥文件的所有权限

SSH登录的警告提示处理方法

参考CentOS 下解决ssh登录 locale 警告
-bash: warning: setlocale: LC_CTYPE: cannot change locale (en_US.UTF-8): No such file or directory

#远程服务器操作

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没有locale-gen命令*
sudo localedef -v -c -i en_US -f UTF-8 en_US.UTF-8

配置防火墙

#远程服务器操作

CentOS 7.0默认使用的是firewall作为防火墙,改为iptables防火墙

#切换到root账户
su
输入root密码

#关闭firewall
systemctl stop firewalld.service //停止firewall
systemctl disable firewalld.service //禁止firewall开机启动

#安装iptables防火墙
yum install iptables-services

#设置iptables规则
iptables -L -n //查看iptables现有规则
iptables -P INPUT ACCEPT //先允许所有,不然有可能会杯具
iptables -F //清空所有默认规则 
iptables -X //清空所有自定义规则 
iptables -Z //所有计数器归0 
iptables -A INPUT -i lo -j ACCEPT //允许来自于lo接口的数据包(本地访问) 
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT //允许接受本机请求之后的返回数据 RELATED,是为FTP设置的
-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配置中设定)
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 //所有转发一律丢弃
service iptables save //保存上述规则 

#开启iptables服务
systemctl enable iptables.service //注册iptables服务 
systemctl start iptables.service //开启服务 
systemctl status iptables.service //查看状态

#关闭SELINUX
vi /etc/selinux/config
SELINUXTYPE=targeted //注释掉(加上#)
setenforce 0 //使配置立即生效

安装篇

参考 CentOS 7.0系统安装配置LAMP服务器(Apache+PHP+MariaDB)

安装Apache

yum install httpd //根据提示,输入Y安装即可成功安装
systemctl start httpd.service //启动apache
systemctl enable httpd.service //设置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
  回车,根据提示输入Y
  输入2次密码,回车
  根据提示一路输入Y
  最后出现:Thanks for using MySQL!
systemctl restart mariadb.service //重启MariaDB
systemctl enable mariadb.service //设置开机启动

#备用
systemctl stop mariadb.service //停止MariaDB

安装PHP

yum install php //根据提示输入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  //允许服务器执行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  //设置默认首页文件,增加index.php

:wq //保存退出
systemctl restart httpd.service //重启apache
rm -f /etc/httpd/conf.d/welcome.conf /var/www/error/noindex.html //删除默认测试页

PHP

vi /etc/php.ini //编辑
date.timezone 设置为 PRC //时区为中国

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函数,提高安全性. 如果某些程序需要用到这个函数,可以删除,取消禁用。
short_open_tag = ON //支持php短标签
open_basedir = .:..:/tmp/  //设置表示允许访问当前目录(即PHP脚本文件所在目录) 上级目录 /tmp/目录,可以防止php木马跨站,如果改了之后安装程序有问题(例如:织梦内容管理系统),可以注销此行

: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 //导入配置文件
vi /etc/my.cnf  //编辑

#指定默认字符集为utf8
  在[mysqld]下加入
    character-set-server=utf8
    skip-character-set-client-handshake 
    //忽略客户端的字符集,使用服务器的设置
  //如果选择不用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

#重启服务
systemctl restart mariadb.service //重启MariaDB
systemctl restart httpd.service //重启apache

php -v //查看下版本
从PHP5.5开始PHP代码缓存从APC变成了Zend OPcache

搭建FTP服务器

CentOS_Linux搭建ftp服务器详细教程

#远程服务器操作

yum install vsftpd //安装
useradd -d /var/ftp/test -g ftp -s /sbin/nologin testuser //建立FTP用户
-d命令是指定用户主目录,-g是指定用户分组,-s /sbin/nologin 是禁止用户登陆系统,最后testuser是本次新建用户的用户名

passwd testuser //设置该用户设置密码
vi /etc/vsftpd/vsftpd.conf //配置vsftpd
  anonymous_enable=NO //禁止匿名登录
  idle_session_timeout=600 //超时, 踢出服务器 ,单位秒

  connect_from_port_20=NO //指定FTP使用20端口进行数据传输
  pasv_enable=YES
  //若设置为YES,则使用PASV工作模式;若设置为NO,则使用PORT模式。默认值为YES,即使用PASV工作模式。
  pasv_max_port=19500
  //在PASV工作模式下,数据连接可以使用的端口范围的最大端口,0 表示任意端口。默认值为0。
  pasv_min_port=19000
  //在PASV工作模式下,数据连接可以使用的端口范围的最小端口,0 表示任意端口。默认值为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 //保存退出

#修改文件夹的读写权限
chown ftp /var/ftp/test
chmod 777 /var/ftp/test

service vsftpd start //开启vsftpd

相关文章

网友评论

      本文标题:CentOS 7.0 配置LAMP环境记实

      本文链接:https://www.haomeiwen.com/subject/xrldpttx.html