开启Apache的PATHINFO模式以支持ThinkPHP

前言

刚开始运行一个基于Think PHP开发的网站源码时(第一次接触ThinkPHP),在本地访问时一直出现404错误,检查地址也没有错误。后来查了很多资料后才发现,Think PHP的默认URL模式是PATHINFO模式,而我本地的Apache没有开启这个模式(ಥ _ ಥ),所以一直报错。在这里记录一下如何开启Apache的PATHINFO模式。

注释:本地服务器使用的是WAMPServer集成安装环境,安装路径为E:Wamp。

正文

第一步:在Apache安装目录的conf文件夹下的httpd.conf文件116行里找到:

#LoadModule rewrite_module modules/mod_rewrite.so

将前面的#去掉,改为:

LoadModule rewrite_module modules/mod_rewrite.so

注:我的路径是:E:WampwampbinapacheApache2.2.21conf,httpd.conf这个文件在其它目录也有,注意只有改动这个目录的文件才有效。

第二步:还是在该文件里进行修改,在118行找到:

Options FollowSymLinks

AllowOverride None

Order deny,allow

Deny from all

AcceptPathInfo On    //加入这行代码就OK了

之后在本地浏览器可以正常访问,不出现404错误了。

UBUNTU 16.04 APACHE2开启HTTP2支持

UBUNTU 16.04默认源里的APACHE2没有HTTP2模块,如果启用HTTP2模块一般会报错找不到模块.

a2enmod http2
ERROR: Module http2 does not exist!

所以如果要启用HTTP2模块,需要从源码从头编一个APACHE2.不过编译好的HTTP2模块可以直接拷贝出来使用,所以已经安装过APACHE2了也不需要担心.
首先将源码源加入源列表之中:

vi /etc/apt/sources.list

添加以下:

deb-src http://archive.ubuntu.com/ubuntu/ xenial main universe restricted multiverse
deb-src http://security.ubuntu.com/ubuntu xenial-security main universe restricted multiverse
deb-src http://archive.ubuntu.com/ubuntu/ xenial-updates main universe restricted multiverse

然后刷新源,如果有需要更新的包,顺便升下级:

apt update
apt upgrade

然后安装依赖:

apt install curl devscripts build-essential fakeroot libnghttp2-dev

创建一个工作的编译目录:

cd ~
mkdir apache2
cd apache2

从源里下载APACHE2的源码然后编译:

apt source apache2
apt build-dep apache2
cd apache2-2.4.18
apt install curl devscripts build-essential fakeroot
fakeroot debian/rules binary

将编译的好的HTTP2模块拷贝到APACHE2模块目录中:

cp debian/apache2-bin/usr/lib/apache2/modules/mod_http2.so /usr/lib/apache2/modules/

然后为HTTP2模块编写一个配置文件:

vim /etc/apache2/mods-available/http2.load

在其中写入以下:

LoadModule http2_module /usr/lib/apache2/modules/mod_http2.so

<IfModule http2_module>  
LogLevel http2:info  
</IfModule>

最后启用HTTP2模块:

service apache2 restart
a2enmod http2
service apache2 restart

编辑网站配置文件,加入HTTP2协议:

vim /etc/apache2/sites-enabled/default-ssl.conf

在默认配置文件的SERVERNAME下添加一行:

Protocols h2 http/1.1

如果没有默认配置文件,可参考以下配置文件:

<IfModule mod_ssl.c>  
 <VirtualHost _default_:443>
 ServerAdmin webmaster@localhost
 ServerName your-domain.com
 Protocols h2 http/1.1
 DocumentRoot /var/www/html

 ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined

 SSLEngine on

 SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
 SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key

 <FilesMatch ".(cgi|shtml|phtml|php)$">
 SSLOptions +StdEnvVars
 </FilesMatch>
 <Directory /usr/lib/cgi-bin>
 SSLOptions +StdEnvVars
 </Directory>

 </VirtualHost>
</IfModule>

然后还需要将HTTP2协议加入APACHE2配置文件中:

vim /etc/apache2/apache2.conf

加入以下行:

Protocols h2 http/1.1

最后重启APACHE2

service apache2 restart

CentOS 7.2安装LAMP(apache mariadb php)搭建WordPress

导语

WordPress是一个以PHP和MySQL为平台的自由开源的博客软件和内容管理系统。WordPress具有插件架构和模板系统。Alexa排行前100万的网站中有超过16.7%的网站使用WordPress。

一.LAMP环境搭建

LAMP即Linux+Apache+MySQL(MariaDB)+PHP,由于其开源免费,所以是目前非常受欢迎的一组网站解决方案,本文也将采用此方案。

1. Linux选择

笔者选用的是CentOS 7.2这个版本,CentOS实质为无支持版的RHEl,稳定性还是比较高的,当然也可以选择其它发行版,只是后续的操作略有不同,本文将只介绍在CentOS下的操作(系统的安装和本文关系不大,故不再赘述)。

2. 安装Apache

直接yum安装

# yum -y install httpd

安装完成后,启动服务

# systemctl start httpd.service

设置为开机启动

# systemctl enable httpd.service

之后可以在浏览器输入localhost进行测试,由于笔者没有安装GUI,只能在客户端浏览器进行测试,在这之前需要打开80和443端口

首先开启防火墙并设置为开机启动

# systemctl start firewalld.service
# systemctl enable firewalld.service

开启端口(80和443)

# firewall-cmd --permanent --zone=public --add-service=http
# firewall-cmd --permanent --zone=public --add-service=https

重启防火墙

# firewall-cmd --reload

查询已开启端口

# irewall-cmd --list-ports

未分类

客户端测试
未分类

3. 安装并配置数据库(Mariadb)

由于甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,所以在CentOS 7中弃用了MySQL,改为支持Mariadb(MySQL的一个分支,与之完全兼容),所以本文也将使用该数据库。

同样还是利用yum命令进行安装,并且配置开机启动

# yum -y install mariadb-server mariadb
# systemctl start mariadb.service
# systemctl enable mariadb.service

配置root密码

# mysql_secure_installation

配置过程中会有几个选项,大家根据自己的需要进行配置就好了

未分类

使用root用户登录mysql

# mysql -u root -p

为WordPress创建数据库

# CREATE DATABASE wordpress;

查看创建结果

未分类

为该数据库创建用户

# CREATE USER username@localhost IDENTIFIED BY 'password';

说明:username – 你将创建的用户名, host – 指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost, 如果想让该用户可以从任意远程主机登陆,可以使用通配符%. password – 该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器.

更改用户权限,保存并退出

# GRANT ALL PRIVILEGES ON wordpress.* TO wordpressuser@localhost;
# FLUSH PRIVILEGES;
# exit

重启服务

# systemctl restart mariadb.service

4. 安装PHP及相关组件

首先安装PHP

# yum -y install php

安装相关组件

# yum -y install php-mysql php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel

安装完成之后我们可以新建一个php页面进行测试

# vim /var/www/html/info.php

编辑为以下内容

未分类

重启httpd服务

# systemctl restart httpd.service

打开网址 http://x.x.x.x/info.php 进行查看(x.x.x.x为vps的ip地址)

未分类
php测试页

二. WordPress安装与配置

安装wget(下载) unzip(解压) net-tools(网络管理)

# yum -y install wget unzip net-tools

下载WordPress

# wget http://wordpress.org/latest.zip

未分类

解压文件,并将其复制到/var/www/html/目录下

# unzip -q latest.zip
# cp -rf wordpress/* /var/www/html/

未分类

编辑配置文件

# cd /var/www/html
# cp wp-config-sample.php wp-config.php
# vim wp-config.php

将其修改为以下格式(其中wordpress为数据库名称,wordpressuser为数据库用户名,10293847为数据库密码)

未分类

保存后退出,重启相关服务

# systemctl restart httpd.service
# systemctl restart mariadb.service

三. WordPress的个人设置

完成以上配置之后,便可以输入http://yourserverip/来访问你的博客了。

  • 选择语言

未分类

  • 填写个人信息

未分类

  • 安装完成

未分类

  • 登录到仪表盘

未分类

至此WordPress已经搭建完成,之后可根据自己的需要进行其它配置。

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

未分类

准备篇

CentOS 7.0系统安装配置图解教程

http://www.osyunwei.com/archives/7829.html

1、配置防火墙,开启80端口、3306端口

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

  • 关闭firewall:
systemctl stop firewalld.service #停止firewall

systemctl disable firewalld.service #禁止firewall开机启动
  • 安装iptables防火墙
yum install iptables-services #安装

vi /etc/sysconfig/iptables #编辑防火墙配置文件

# Firewall configuration written by system-config-firewall

# Manual customization of this file is not recommended.

*filter

:INPUT ACCEPT [0:0]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [0:0]

-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

-A INPUT -p icmp -j ACCEPT

-A INPUT -i lo -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

-A INPUT -j REJECT --reject-with icmp-host-prohibited

-A FORWARD -j REJECT --reject-with icmp-host-prohibited

COMMIT

:wq! #保存退出

systemctl restart iptables.service #最后重启防火墙使配置生效

systemctl enable iptables.service #设置防火墙开机启动

2、关闭SELINUX

vi /etc/selinux/config

#SELINUX=enforcing #注释掉

#SELINUXTYPE=targeted #注释掉

SELINUX=disabled #增加

:wq! #保存退出

setenforce 0 #使配置立即生效

安装篇

1、安装Apache

未分类

yum install httpd #根据提示,输入Y安装即可成功安装

systemctl start httpd.service #启动apache

systemctl stop httpd.service #停止apache

systemctl restart httpd.service #重启apache

systemctl enable httpd.service #设置apache开机启动

在客户端浏览器中打开服务器IP地址,会出现下面的界面,说明apache安装成功

未分类

2、安装MariaDB

CentOS 7.0中,已经使用MariaDB替代了MySQL数据库

  • 安装MariaDB

未分类

yum install mariadb mariadb-server #询问是否要安装,输入Y即可自动安装,直到安装完成

systemctl start mariadb.service #启动MariaDB

systemctl stop mariadb.service #停止MariaDB

systemctl restart mariadb.service #重启MariaDB

systemctl enable mariadb.service #设置开机启动

cp /usr/share/mysql/my-huge.cnf /etc/my.cnf #拷贝配置文件(注意:如果/etc目录下面默认有一个my.cnf,直接覆盖即可)
  • 为root账户设置密码

未分类

mysql_secure_installation

回车,根据提示输入Y

输入2次密码,回车

根据提示一路输入Y

最后出现:Thanks for using MySQL!

MariaDB密码设置完成,重新启动 MariaDB:

systemctl restart mariadb.service #重启MariaDB

3、安装PHP

  • 安装PHP

未分类

yum install php #根据提示输入Y直到安装完成
  • 安装PHP组件,使PHP支持 MariaDB
yum install php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash

#这里选择以上安装包进行安装,根据提示输入Y回车

systemctl restart mariadb.service #重启MariaDB

systemctl restart httpd.service #重启apache

配置篇

1、Apache配置

vi /etc/httpd/conf/httpd.conf #编辑文件

ServerSignature On  #添加,在错误页中显示Apache的版本,Off为不显示

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)

AddDefaultCharset UTF-8 #修改为:AddDefaultCharset GB2312 (添加GB2312为默认编码)

#Options Indexes FollowSymLinks   #修改为 Options FollowSymLinks(不在浏览器上显示树状目录结构)

DirectoryIndex index.html   #修改为:DirectoryIndex index.html index.htm Default.html Default.htm index.php(设置默认首页文件,增加index.php)

MaxKeepAliveRequests 500  #添加MaxKeepAliveRequests 500 (增加同时连接数)

:wq! #保存退出

systemctl restart httpd.service #重启apache

rm -f /etc/httpd/conf.d/welcome.conf /var/www/error/noindex.html #删除默认测试页

2、php配置

vi /etc/php.ini #编辑

date.timezone = PRC #把前面的分号去掉,改为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可以禁用的函数,如果某些程序需要用到这个函数,可以删除,取消禁用。

expose_php = Off #禁止显示php版本的信息

short_open_tag = ON #支持php短标签

open_basedir = .:/tmp/  #设置表示允许访问当前目录(即PHP脚本文件所在之目录)和/tmp/目录,可以防止php木马跨站,如果改了之后安装程序有问题(例如:织梦内容管理系统),可以注销此行,或者直接写上程序的目录/data/www.osyunwei.com/:/tmp/

:wq! #保存退出

systemctl restart mariadb.service #重启MariaDB

systemctl restart httpd.service #重启apache

测试篇

cd /var/www/html

vi index.php #输入下面内容

<?php

phpinfo();

?>

:wq! #保存退出

在客户端浏览器输入服务器IP地址,可以看到如下图所示相关的配置信息!

未分类

注意:apache默认的程序目录是/var/www/html

权限设置:chown apache.apache -R /var/www/html

至此,CentOS 7.0安装配置LAMP服务器(Apache+PHP+MariaDB)教程完成!

配置Apache开启gzip压缩传输

开启模块

打开httpd.conf后,先将下面两行配置前面的#号去掉,这样apache就会启用这两个模块,其中mod_deflate是压缩模块,就是对要传输到客户端的代码进行gzip压缩;mod_headers模块的作用是告诉浏览器页面使用了gzip压缩,如果不开启mod_headers那么浏览器就会对gzip压缩过的页面进行下载,而无法正常显示。

LoadModule deflate_module modules/mod_deflate.so
LoadModule headers_module modules/mod_headers.so

代码

在httpd.conf中加入以下代码,可以加到任何空白地方,不了解apache的朋友,如果担心加错地方,就放到http.conf文件的最后一行,如果是虚拟服务器可以写.htaccess文件里面,然后放在项目下即可。

<IfModule mod_deflate.c>
    SetOutputFilter DEFLATE
    SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI .(?:pdf|mov|avi|mp3|mp4|rm)$ no-gzip dont-vary
    AddOutputFilterByType DEFLATE text/*
    AddOutputFilterByType DEFLATE application/ms* application/vnd* application/postscript application/javascript application/x-javascript
    AddOutputFilterByType DEFLATE application/x-httpd-php application/x-httpd-fastphp
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4.0[678] no-gzip
    BrowserMatch bMSIE !no-gzip !gzip-only-text/html
</IfModule>

检测配置文件

未分类

重启服务即可

未分类

Apache环境下http强制跳转https的方法

在启用了 https 之后,还要保证之前的 http 端口可以打开,http 的 80 端口是有两个网址的,所以这就导致需要把原来的带 www 和不带 www 的域名同时指定一个 https 网址上面,需要做两个 Apache 的301重定向,这个其实是很简单的,最简单的做法是直接在 .htaccess 文件中添加两个 301 即可,如下所示:

RewriteCond %{http_host} ^91linux.org [nc] 
RewriteRule ^(.*)?$ https://www.91linux.org/$1 [R=301,L] 

RewriteCond %{SERVER_PORT} !^443$ 
RewriteRule ^(.*)?$ https://www.91linux.org/$1 [R=301,L]

第一个 301 很自然就是不带 www 的跳转到新的 https 上面了,而下面的301重定向则是判断如果端口不是80的话,则进行重定向,这样的话,带www和不带www的域名就一起跳转到 https 一个网址上面了,当然这种全站做301的方法是比较暴力的,通常情况下我们只要把主域名做个301就可以了,我这里是因为启用了原来的两个域名。

这是其它的 Apache http 跳转到 https 的方法,仅供参考:

方法一

RewriteEngine On 
RewriteBase / 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.91linux.org/$1 [R=301,L] 
#这样跳转的好处是独立IP主机也支持,访问ip能自动跳转到https

方法二

RewriteEngine on 
RewriteCond %{SERVER_PORT} !^443$ 
RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [R=301,L] 
#整站跳转

方法三

RewriteEngine on 
RewriteBase /yourfolder 
RewriteCond %{SERVER_PORT} !^443$ 
#RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [R=301,L] 
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L] 
#以上至针对某个目录跳转, yourfolder就是目录名

方法4

redirect 301 /你的网页 https://你的主机+网页 
#至针对某个网页跳转

方法5

RewriteEngine on 
RewriteCond %{SERVER_PORT} !^443$ 
RewriteCond %{REQUEST_URI} !^/tz.php 
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R]

解释:

  • %{SERVER_PORT} —— 访问端口
  • %{REQUEST_URI} —— 比如如果url是 http://localhost/tz.php,则是指 /tz.php
  • %{SERVER_NAME} —— 比如如果url是 http://localhost/tz.php,则是指 localhost

以上规则的意思是,如果访问的url的端口不是443,且访问页面不是tz.php,则应用RewriteRule这条规则。

这样便实现了:访问了 http://localhost/index.php 或者 http://localhost/admin/index.php 等页面的时候会自动跳转到 https://localhost/index.php 或者 https://localhost/admin/index.php,但是访问 http://localhost/tz.php 的时候就不会做任何跳转,也就是说 http://localhost/tz.php 和 https://localhost/tz.php 两个地址都可以访问。

安装Apache2 libapache2-mod-wsgi部署Django应用

首先安装apache:

sudo apt-get update
sudo apt-get install apache2

安装完成后,启动apache:

sudo service apache2 start

然后新建Django项目的配置文件:

cd /etc/apache2/sites-available
vim 001-project.conf

这里需要注意,现在的apache服务器的配置文件的后缀是.conf,不能写成.config,否则apache会找不到对应的配置文件。

然后是编写对应的配置文件,这里贴上一个例子,对照着写就可以了:

<VirtualHost *:80>
ServerName 127.0.0.1   # 如果有域名,这里就填写对应的域名。127.0.0.1对应的是本地

RewriteEngine On
RewriteRule ^/(d-media|media|examples|screenshots)($|(/(.*))) /app/project/$0 [L]

DocumentRoot /app/project/

Alias /static/ /app/project/static/  # 如果有静态文件,则需要填写这句

<Directory /app/project>
Order Allow,Deny   
allow From All  # 这里允许所有人访问,也可以设置成有条件的访问,例如只能允许某些IP访问等等,具体设置这里不多说了
Options Indexes FollowSymLinks
</Directory>

WSGIProcessGroup project
WSGIApplicationGroup %{GLOBAL}

WSGIDaemonProcess lawyer_site python-path=/app/project:/app/ENV/project/lib/python2.7/site-packages user=ubuntu  group=ubuntu inactivity-timeout=3600
WSGIScriptAlias / /app/project/project/wsgi.py
# python-path是对应的环境,我这里使用了虚拟环境virtualenv。如果不是虚拟环境,则找到对应的路径即可。

CustomLog /app/project/logs/access.log combined
ErrorLog /app/project/logs/error.log
</VirtualHost>

编辑完apache的配置文件之后,需要在sites-enabled文件夹中设置软连接:

cd ../sites-enabled/
sudo ln -s ../sites-available/001-project.conf

同时,需要在项目目录中新建一个日志文件夹,里面存放的是Django项目运行在apache服务器上的日志:

mkdir /app/project/logs

到这里,整个部署就算是完成了,我们重启apache服务器:

sudo service apache2 restart

查看apache服务器的运行状态:

sudo service apache2 status

下面是我在配置apache的时候遇到的一些问题:

1、遇到Invalid command ‘RewriteEngine’:

sudo a2enmod rewrite
sudo service apache2 restart

2、遇到Invalid command ‘WSGIProcessGroup’:

sudo a2enmod wsgi

3、如果显示ERROR: Module mod-wsgi does not exist!:

sudo apt-get install libapache2-mod-wsgi
sudo a2enmod wsgi
sudo service apache2 restart

如何把Angular 2部署到Apache服务器

问题

我想把Angular 2应用部署到Apache服务器。已经按照网上的几篇教程部署,都不成功。服务器上安装了npm和ng。
在nutshell中,做了如下操作:
1. 完整克隆整个Angular项目到服务器上
2. 使用npm install安装依赖
3. 执行ng build –prod命令,生成了dist目录
4. 更改apache根目录到/var/www/html/dist目录
5. 启用mod_rewrite,重启apache并在dist目录添加.htaccess文件,内容如下:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.html [L]
</IfModule>

不过仅是主页domain.com正常,其它页面像domain.com/login,domain.com/register等抛出了404错误。甚至是domain.com/index.html/login也是一样。
这个应用是我的本地系统使用ng serve时是正常的。这是哪里错了?

最佳答案

在/etc/apache2/sites-enabled/000-default.conf添加如下内容并重启apache

<Directory "/var/www/html/dist">
  AllowOverride All
</Directory>

CentOS 7.0安装LAMP服务器(PHP+MariaDB+Apache)

1、关闭firewall:

systemctl stop firew
alld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动

2、安装iptables防火墙(#可不安装)

yum install iptables-services #安装
vi /etc/sysconfig/iptables #编辑防火墙配置文件
//配置文件:
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
//:wq! #保存退出

关闭 SELINUX

vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! #保存退出
setenforce 0 #使配置立即生效

一、Apache安装

yum install httpd #根据提示,输入Y安装即可成功安装
systemctl start httpd.service #启动apache
systemctl stop httpd.service #停止apache
systemctl restart httpd.service #重启apache
systemctl enable httpd.service #设置apache开机启动

二、安装MariaDB

yum install mariadb mariadb-server 
//#询问是否要安装,输入Y即可自动安装,直到安装完成
systemctl start mariadb.service #启动MariaDB
systemctl stop mariadb.service #停止MariaDB
systemctl restart mariadb.service #重启MariaDB
systemctl enable mariadb.service #设置开机启动
cp /usr/share/mysql/my-huge.cnf /etc/my.cnf 
//拷贝配置文件(注意:如果/etc目录下面默认有一个my.cnf,直接覆盖即可)

设置密码

mysql_secure_installation
systemctl restart mariadb.service

三、安装PHP

//主程序
yum install php
//安装模块
yum install php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash
systemctl restart mariadb.service #重启MariaDB
systemctl restart httpd.service #重启apache

四、安装phpMyAdmin

//主程序
sudo yum install phpmyadmin php-mcrypt
//修改配置文件
vi /etc/httpd/conf.d/phpMyAdmin.conf 

<Directory /usr/share/phpMyAdmin/>
  AddDefaultCharset UTF-8

  <IfModule mod_authz_core.c>
   # Apache 2.4
   <RequireAny>
    #Require ip 127.0.0.1
    #Require ip ::1
    Require all granted
   </RequireAny>
  </IfModule>
  <IfModule !mod_authz_core.c>
   Order Deny,Allow
   Deny from All
   Allow from 127.0.0.1
   Allow from ::1
  </IfModule>
</Directory>

<Directory /usr/share/phpMyAdmin/setup/>
  <IfModule mod_authz_core.c>
   # Apache 2.4
   <RequireAny>
    #Require ip 127.0.0.1
    #Require ip ::1
    Require all granted
   </RequireAny>
  </IfModule>
  <IfModule !mod_authz_core.c>
   Order Deny,Allow
   Deny from All
   Allow from 127.0.0.1
   Allow from ::1
  </IfModule>
</Directory>

systemctl restart httpd #重启httpd

Apache如何安装配置证书?

格式说明

首先 Apache 使用的证书是 .pem 格式的。什么是 .pem 格式,就是以 —–BEGIN xxx—– 开头的文件,如:

-----BEGIN CERTIFICATE-----
MIID6jCCAtKgAwIBAgIBFDANBgkqhkiG9w0BAQsFADB7MQswCQYDVQQGEwJDTjER  
MA8GA1UECAwIU2hhbmdoYWkxDzANBgNVBAoMBmRlZXB6ejEPMA0GA1UECwwGZGVl  
cHp6MRMwEQYDVQQDDApkZWVwenouY29tMSIwIAYJKoZIhvcNAQkBFhNkZWVwenou  
... ...
-----END CERTIFICATE-----

当然现在 openssl 新版签发的证书,在 —–BEGIN xxx—– 之前还有一段信息,我们这里就不举例了。

所以,当我门从证书服务商那里下载证书的时候需要注意证书的格式问题。选择 Apache 进行下载。

一般的,证书服务商会提供三个文件:

example.com.ca.crt  
example.com.crt  
example.com.key  
  • example.com.ca.crt,CA 中间证书。
  • example.com.crt,你的服务器证书文件。
  • example.com.key,你的私钥。

还有可能是如下文件:

example.com.crt  
example.com.key  
  • example.com.key 你的私钥文。
  • example.com.crt 是你的证书文件或证书链文件。用文本编辑器打开,如果看到两张上面 pem 内容,即站点证书+CA中间证书。你需要将它们分别存为两个文件,如 example.com.ca.crt 和 example.com.crt。如果不是,请联系证书服务商或到 这里 补全证书链。

安装证书

首先,确保 ssl 模块已经加载进 apache 了:

$ a2enmod ssl

如果你看到了 Module ssl already enabled 这样的信息就说明你成功了。如果你看到了 Enabling module ssl,那么你还需要用下面的命令重启apache:

$ service apache2 restart

最后像下面这样修改你的虚拟主机文件(通常在/etc/apache2/sites-enabled 下):

DocumentRoot /var/www/html/  
ServerName example.com  
SSLEngine on  
SSLCertificateFile /usr/local/ssl/crt/example.com.crt  
SSLCertificateKeyFile /usr/local/ssl/example.com.key  
SSLCACertificateFile /usr/local/ssl/example.com.ca.crt  

你现在应该可以用 https://example.com(注意使用 https 而不是 http)来访问你的网站了,并可以看到SSL的进度条了(通常在你浏览器中用一把锁来表示)。