linux 部署WEB LAMP环境 LAMP理论知识 程序安装和配置 https站点部署

LAMP所需软件

httpd (提供 Apache 主程式)
mysql (MySQL 客户端程式)
mysql-server (MySQL 服务器程序)
php (PHP 主程式含给 apache 使用的模块)
php-devel (PHP 的发展工具,这个与 PHP 外挂的加速软体有关)
php-mysql (提供给 PHP 程序读取 MySQL 资料库的模块)

Apache软件结构

/etc/httpd/conf/httpd.conf         # 主要配置文件
/etc/httpd/conf.d/*.conf         #主配置文件包含的子配置文件
/usr/lib64/httpd/modules/, /etc/httpd/modules/    #Apache支持的模块默认放置的位置
/var/www/html/        #Apache首页放置的目录
/var/www/error/        #浏览器上显示的错误信息配置文件放置的位置
/var/www/icons/        #这个目录提供 Apache 预设给予的一些小图示,通过输入‘http://localhost/icons/’ 显示。
/var/www/cgi-bin/    #预设给一些可执行的 CGI网页程序放置的目录;当你输入‘http://localhost/cgi-bin/’ 时所显示的资料所在。
/var/log/httpd/        #预设的httpd访问日志
/usr/sbin/apachectl        #Apache的执行程序启动的脚本文件
/usr/sbin/httpd        #二进制的Apache程序文件
/usr/bin/htpasswd    #用来设置需要输入密码的网页的密码

Mysql软件结构

/etc/my.cnf        #mysql的主配置文件
/var/lib/mysql/    #这个目录是 MySQL 数据库文件放置的位置!记得及时备份。

PHP软件结构

/etc/httpd/conf.d/php.conf        #Apache部分使用PHP模块的配置参数
/etc/php.ini        #PHP的主要配置文件
/usr/lib64/httpd/modules/libphp5.so    #PHP软件提供给Apache的模块程序,这也是我们能否在 Apache 网页上面设计 PHP 程序语言的东西。
/etc/php.d/mysql.ini, /usr/lib64/php/modules/mysql.so    #PHP支持mysql的相关配置文件和模块
/usr/bin/phpize, /usr/include/php/        #如果你未来想要安装类似 PHP 加速器以让浏览速度加快的话,那么这个档案与目录就得要存在,
#否则加速器软体可无法编译成功喔!这两个资料也是 php-devel 软体所提供的啦!

程序安装和配置

yum -y install httpd mysql mysql-server php php-mysql

Apache配置

[root@www ~]# vim /etc/httpd/conf/httpd.conf
ServerTokens OS
# 这个项目在仅告知用户端我们服务器的版本与作业系统而已,不需要更动他。

ServerRoot "/etc/httpd"
# 服务器设定的最顶层目录,有点类似 chroot 那种感觉。包括 logs, modules
# 等等的资料都应该要放置到此目录底下 (若未宣告成绝对路径时)

PidFile run/httpd.pid
# 放置 PID 的档案,可方便 Apache 软体的管理啦!只有相对路径吧!
# 考虑 ServerRoot 设定值,所以档案在 /etc/httpd/run/httpd.pid !

Timeout 60
# 不论接收或传送,当持续连线等待超过 60 秒则该次连线就中断。
# 一般来说,此数值在 300 秒左右即可,不需要修改这个原始值啦。

KeepAlive On    <==最好将预设的 Off 改为 On 啦!
# 是否允许持续性的连线,亦即一个 TCP 连线可以具有多个档案资料传送的要求。
# 举例来说,如果你的网页内含很多图档,那么这一次连线就会将所有的资料送完,
# 而不必每个图档都需要进行一次 TCP 连线。预设为 Off 请改为 On 较佳。

MaxKeepAliveRequests 500  <==可以将原本的 100 改为 500 或更高
# 与上个设定值 KeepAlive 有关,当 KeepAlive 设定为 On 时,则这个数值可决定
# 该次连线能够传输的最大传输数量。为了增进效能则可以改大一点!0 代表不限制。

KeepAliveTimeout 15
# 在允许 KeepAlive 的条件下,则该次连线在最后一次传输后等待延迟的秒数。
# 当超过上述秒数则该连线将中断。设定 15 差不多啦!如果设定太高 (等待时间较长),
# 在较忙碌的系统上面将会有较多的 Apache 程序占用资源,可能有效能方面的困扰。

#底下两个 perfork, worker 与内存管理有关,默认使用的prefork。
#/usr/sbin/httpd:使用 prefork 模组;
#/usr/sbin/httpd.worker:使用 worker 模组。
<IfModule prefork.c>   
StartServers       8   <==启动 httpd 时,唤醒几个 PID 来处理服务的意思
MinSpareServers    5   <==最小的预备使用的 PID 数量
MaxSpareServers   20   <==最大的预备使用的 PID 数量
ServerLimit      256   <==服务器的限制
MaxClients       256   <==最多可以容许多少个用户端同时连线到 httpd 的意思!
MaxRequestsPerChild  4000
</IfModule>

<IfModule worker.c>
StartServers         4
MaxClients         300
MinSpareThreads     25
MaxSpareThreads     75
ThreadsPerChild     25
MaxRequestsPerChild  0
</IfModule>
Listen 80        #监听的端口

Include conf.d/*.conf
# 因为这一行,所以放置到 /etc/httpd/conf.d/*.conf 的设定都会被读入!

User apache
Group apache
# 前面提到的 prework, worker 等模组所启动的 process 之拥有者与群组设定。
# 这个设定很重要,因为未来你提供的网页档案能不能被浏览都与这个身份有关啊!

ServerAdmin wang_zengyi@126.com  <==改成你自己的 email 吧
# 系统管理员的 email,当网站出现问题时,错误讯息会显示的联络信箱(错误回报)。

ServerName www.centos.jet    <==自行设定好自己的主机名称较佳!
# 设定主机名称,这个值如果没有指定的话,预设会以 hostname 的输出为依据。
# 千万记得,你填入的这个主机名称要找的到 IP !(DNS 或 /etc/hosts)

UseCanonicalName Off
# 是否使用标准主机名称?如果你的主机有多个主机名称,若这个设定为 On,
# 那么 Apache 只接受上头 servername 指定的主机名称连线而已。请使用 Off。

AddDefaultCharset UTF-8
#服务器强制使用UTF-8编码给客户端的浏览器使用。
DocumentRoot "/var/www/html"  <==可以改成你放置首页的目录!
# 这个设定值规范了 WWW 服务器主网页所放置的‘目录’,虽然设定值内容可以变更,

PHP配置

#了解即可,这个配置文件不需要任何的修改。
[root@www ~]# cd /etc/httpd/conf.d
[root@www conf.d]# ll *.conf
-rw-r--r--. 1 root root 674 Jun 25 15:30 php.conf      <==提供 PHP 模组的设定
-rw-r--r--. 1 root root 299 May 21  2009 welcome.conf  <==提供预设的首页欢迎讯息
#了解即可,这个配置文件不需要任何的修改。
[root@www conf.d]# vim /etc/httpd/conf.d/php.conf
<IfModule prefork.c>  <==根据不同的 PID 模式给予不同的 PHP 运作模组
  LoadModule php5_module modules/libphp5.so
</IfModule>
<IfModule worker.c>
  LoadModule php5_module modules/libphp5-zts.so
</IfModule>
AddHandler php5-script .php  <==所以副档名一定要是 .php 结尾!
AddType text/html .php       <==.php 结尾的档案是纯文字档
DirectoryIndex index.php     <==首页档名增加 index.php 喔!
#AddType application/x-httpd-php-source .phps <==特殊的用法!
#PHP安全方面的限制
[root@www ~]# vim /etc/php.ini
register_globals = Off
# 这个项目请确定为 Off (预设就是 Off),因为如果设定为 On 时,
# 虽然程序执行比较不容易出状况,但是很容易不小心就被攻击。

log_errors = On
ignore_repeated_errors = On  <==这个设定值调整一下 (因预设为 Off)
ignore_repeated_source = On  <==这个设定值调整一下
# 这三个设定项目可以决定是否要将 PHP 程序的错误记录起来,
# 建议将重复的错误资料忽略掉,否则在很忙碌的系统上,
# 这些错误资料将可能造成你的用户访问日志暴增,导致效能不佳 (或宕机)

display_errors = Off
display_startup_errors = Off
# 当你的程序发生问题时,是否要在浏览器上头显示相关的错误信息 (包括部分程序代码)
# 强烈的建议设定为 Off 。不过如果是尚未开放的 WWW 服务器,为了你的 debug
# 容易,可以暂时的将他设定为 On ,如此一来你的程序问题会在浏览器上面
# 直接显示出来,你不需要进入 /var/log/httpd/error_log 登录当中查阅。
# 但程序调试完成后,记得将此设定值改为 Off !重要重要!
# PHP提供的上传容量限制
[root@www ~]# vim /etc/php.ini
post_max_size = 20M       <==大约在 729 行左右
file_uploads = On         <==一定要是 On 才行 (预设值)
upload_max_filesize = 16M <==大约在 878 行左右
memory_limit = 128M       <==PHP 可用记忆体容量也能修订!

启动Apache服务

[root@www ~]# /etc/init.d/httpd start       <==立刻启动!
[root@www ~]# /etc/init.d/httpd configtest  <==测试设定档语法
[root@www ~]# chkconfig httpd on            <==开机启动 WWW 啦!
#另外一种启动方法
[root@www ~]# /usr/sbin/apachectl start  <==启动啦!
[root@www ~]# /usr/sbin/apachectl stop   <==关闭 WWW 啦!
#测试PHP安装是否正确,首先编辑如下文件内容:
[root@www ~]# vim /var/www/html/phpinfo.php
<?php  phpinfo ();  ?>
#然后访问:http://$IP/phpinfo.php 

mysql配置

[root@www ~]# /etc/init.d/mysqld start
[root@www ~]# chkconfig mysqld on
# 如果是初次启动,萤幕会显示一些讯息且 /var/lib/mysql 会建立资料库。

[root@www ~]# netstat -tulnp | grep 'mysql'
Proto Recv-Q Send-Q Local Address  Foreign Address   State   PID/Program name
tcp        0      0 0.0.0.0:3306   0.0.0.0:*         LISTEN  2726/mysqld

# 底下在测试看能否以手动的方式连上 MySQL 资料库!
[root@www ~]# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.1.52 Source distribution
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql> exit
Bye
[root@www ~]# mysqladmin -u root password 'your.password'
# 从此以后 MySQL 的 root 帐号就需要密码了!如下所示:
[root@www ~]# mysql -u root -p
Enter password:  <==你必须要在这里输入刚刚建立的密码!
mysql> exit
#不同的管理者使用不同的数据库
[root@www ~]# mysql -u root -p
Enter password:  <==如前所述,你必须要输入密码嘛!
mysql> create database jet;  <==注意每个指令后面都要加上分号 (;)
Query OK, 1 row affected (0.01 sec)
mysql> grant all privileges on jetdb.* to jetuser@localhost 
identified by 'vbirdpw' ;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              | <==用来记录 MySQL 帐号、主机等重要资讯的主要资料库!
| test               | 
| jet            | <==我们刚刚建立的资料库在此
+--------------------+
4 rows in set (0.00 sec)
mysql> use mysql;
mysql> select * from user where user = 'jetuser';
# 上面两个指令在查询系统有没有 jetuser 这个帐号,若有出现一堆东西,
# 那就是查询到该帐号了!这样就建置妥当了!
mysql> exit
#mysql的性能调优

[root@www ~]# vim /etc/my.cnf
[mysqld]
default-storage-engine=innodb
# 关于目录资料与语系的设定等等;
default-character-set   = utf8   <==每个人的编码都不相同,不要随意跟我一样
port                    = 3306
skip-locking
# 关于内存的设定,注意,内存占用的简单计算方式为:
# key_buffer + (sort_buffer + read_buffer ) * max_connection
# 且总量不可高于实际的实体记忆体量!所以,我底下的资料应该是 OK 的
# 128 + (2+2)*150 = 728MB
key_buffer              = 128M
sort_buffer_size        = 2M
read_buffer_size        = 2M
join_buffer_size        = 2M
max_connections         = 150
max_connect_errors      = 10
read_rnd_buffer_size    = 4M
max_allowed_packet      = 4M
table_cache             = 1024
myisam_sort_buffer_size = 32M
thread_cache            = 16
query_cache_size        = 16M
tmp_table_size          = 64M
# 由连线到确定断线的时间,原本是 28800 (sec) ,约 8 小时,我将他改为 20 分钟!
wait_timeout            = 1200
thread_concurrency      = 8
innodb_data_file_path = ibdata1:10M:autoextend
innodb_buffer_pool_size = 128M
innodb_additional_mem_pool_size = 32M
innodb_thread_concurrency = 16
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

https站点部署

制作自制凭证

# 1. 先到 /etc/pki/tls/certs 去建立一把给 Apache 使用的私钥档案:
[root@www ~]# cd /etc/pki/tls/certs
[root@www certs]# make jet.key
umask 77 ; /usr/bin/openssl genrsa -aes128 2048 > jet.key  <==其实是这个指令
Generating RSA private key, 2048 bit long modulus
.................................................................+++
...............................+++
e is 65537 (0x10001)
Enter pass phrase:  <==这里输入这把私钥的密码,需要多于四位!
Verifying - Enter pass phrase:  <==再一次!

# 2. 将刚刚建立的档案中,里面的密码取消掉!不要有密码存在!
[root@www certs]# mv jet.key jet.key.raw
[root@www certs]# openssl rsa -in jet.key.raw -out jet.key
Enter pass phrase for jet.key.raw: <==输入刚刚的密码!
writing RSA key
[root@www certs]# rm -f jet.key.raw  <==旧的私钥档移除
[root@www certs]# chmod 400 jet.key  <==权限一定是 400 才行!

# 3. 建置所需要的最终凭证档!
[root@www certs]# make jet.crt SERIAL=2020032601
umask 77 ; /usr/bin/openssl req -utf8 -new -key jet.key -x509 -days 365 
-out jet.crt -set_serial 2020032601  <==可以加入日期序号
You are about to be asked to enter information that will be incorporated
into your certificate request.
-----
Country Name (2 letter code) [XX]:BJ
State or Province Name (full name) []:BeiJing
Locality Name (eg, city) [Default City]:BeiJing
Organization Name (eg, company) [Default Company Ltd]:TEST
Organizational Unit Name (eg, section) []:TEST
Common Name (eg, your name or your server's hostname) []:www.centos.jet
Email Address []:jet@www.centos.jet

[root@www certs]# ll jet*
-rw-------. 1 root root 1419 2020-03-26 15:24 vbird.crt  <==最终凭证档!
-r--------. 1 root root 1679 2020-03-26 15:22 vbird.key  <==系统私钥档
# 修改SSL配置,使用自制的私钥和证书
[root@www ~]# vim /etc/httpd/conf.d/ssl.conf
SSLCertificateFile /etc/pki/tls/certs/jet.crt    <==约在 105 行
SSLCertificateKeyFile /etc/pki/tls/certs/jet.key <==约在 112 行
[root@www ~]# /etc/init.d/httpd restart
# 将http与https相分离
# 1. 处理目录与预设的首页 index.html 档案:
[root@www ~]# mkdir /var/www/https
[root@www ~]# echo "This is https' home" > /var/www/https/index.html

# 2. 开始处理 ssl.conf 的内容!
[root@www ~]# vim /etc/httpd/conf.d/ssl.conf
Listen 443                      <==预设的监听端口!不建议修改!
<VirtualHost _default_:443>     <==就是虚拟主机的设定!
DocumentRoot "/var/www/https"   <==约84行,拿掉注解改掉目录名称
ServerName *:443                <==拿掉注解,并将主机名称设定为 *
SSLEngine on                    <==有支持 SSL 的意思!
SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW
SSLCertificateFile /etc/pki/tls/certs/jet.crt
SSLCertificateKeyFile /etc/pki/tls/certs/jet.key
</VirtualHost>

[root@www ~]# /etc/init.d/httpd restart