简介:
使用yum方式安装LAMP(CentOS + Apache + Mariadb/MySQL + PHP)
安装环境及版本:
系统环境:CentOS Linux release 7.5.1804 (Core)
组件版本:yum安装
一、安装CentOS 操作系统
1.1 安装操作系统
在VMware上安装操作系统:CentOS Linux release 7.5.1804,具体安装方式参考《VMware创建系统(CentOS6.5)》
1.2 安装VMTools
参考《VMware安装VMTools(CentOS)》
1.3 快照
参考《VMware快照》
1.4 配置IP地址
参考《配置IP地址(CentOS)》
IP地址:192.168.4.100
1.5 配置主机名
参考《配置主机名(CentOS)》
hostname:lamp_small
1.6 关闭防火墙和禁用SELINUX
参考《关闭防火墙及SELINUX(CentOS)》
二、安装Apache
参考《Apache安装》中的yum安装方式
命令:yum -y install httpd
2.1 配置文件:vi /etc/httpd/conf/httpd.conf
修改域名
95 #ServerName www.example.com:80
将发布目录设置为/var/www/html
119 DocumentRoot "/var/www/html"
124 <Directory "/var/www">
125 AllowOverride None
126 # Allow open access:
127 Require all granted
128 </Directory>
129
130 # Further relax access to the default document root:
131 <Directory "/var/www/html">
在默认首页中增加PHP的支持首页index.php
163 <IfModule dir_module>
164 DirectoryIndex index.html index.php
165 </IfModule>
退出,并保存修改。
2.2 重启、自启动
重启:systemctl restart httpd
设置为自启动:systemctl enable httpd
2.3 验证
浏览器输入:http://192.168.47.100/
三、创建用户及目录
添加用户pyrk:useradd -g 48 -u 1204 pyrk(48为apache用户组)
开发用,避免使用root账号
[root@lamp_small ~]# useradd -g 48 -u 1204 pyrk
将用户pyrk和apapche配置同组,同权限
修改/etc/passwd:vi /etc/passwd
[root@lamp_small ~]# vi /etc/passwd
pyrk:x:48:48::/home/pyrk:/bin/bash
修改用户pyrk目录权限:chown apache:apache /home/pyrk
[root@lamp_small home]# ll /home
总用量 0
drwx------ 2 1204 apache 62 7月 4 16:42 pyrk
[root@lamp_small home]# chown apache:apache /home/pyrk
[root@lamp_small home]# ll
总用量 0
drwx------ 2 apache apache 62 7月 4 16:42 pyrk
创建日志目录:mkdir -p /var/www/logs
其中html目录为配置http.conf时,自动生成的发布目录
[root@lamp_small home]# ll /var/www
总用量 0
drwxr-xr-x 2 root root 6 4月 2 21:14 cgi-bin
drwxr-xr-x 2 root root 6 4月 2 21:14 html
drwxr-xr-x 2 root root 6 7月 4 16:46 logs
设置html和logs目录的访问权限
chown apache:apache /var/www/html/
chown apache:apache /var/www/logs/
[root@lamp_small home]# chown apache:apache /var/www/html/
[root@lamp_small home]# chown apache:apache /var/www/logs/
[root@lamp_small home]# ll /var/www
总用量 0
drwxr-xr-x 2 root root 6 4月 2 21:14 cgi-bin
drwxr-xr-x 2 apache apache 6 4月 2 21:14 html
drwxr-xr-x 2 apache apache 6 7月 4 16:46 logs
四、安装SSL
详细参考《SSL安装》
安装:[root@localhost ~]# yum -y install openssl openssl-devel mod_ssl
4.1 配置
配置文件:[root@lamp_small home]# vi /etc/httpd/conf.d/ssl.conf
启用根目录,并配置域名
59 DocumentRoot "/var/www/html"
60 ServerName www.example.com:443
配置微信小程序目前支持SSL协议v1.2
75 SSLProtocol -all +TLSv1.2
将Apapche的CA证书导入指定目录并配置,将crt放入/etc/pki/tls/certs/目录,将key放入/etc/pki/tls/private/目录
100 SSLCertificateFile /etc/pki/tls/certs/localhost.crt
101
102 # Server Private Key:
103 # If the key is not combined with the certificate, use this
104 # directive to point at the key file. Keep in mind that if
105 # you've both a RSA and a DSA private key you can configure
106 # both in parallel (to also allow the use of DSA ciphers, etc.)
107 SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
108
109 # Server Certificate Chain:
110 # Point SSLCertificateChainFile at a file containing the
111 # concatenation of PEM encoded CA certificates which form the
112 # certificate chain for the server certificate. Alternatively
113 # the referenced file can be the same as SSLCertificateFile
114 # when the CA certificates are directly appended to the server
115 # certificate for convinience.
116 #SSLCertificateChainFile /etc/pki/tls/certs/server-chain.crt
117
118 # Certificate Authority (CA):
119 # Set the CA certificate verification path where to find CA
120 # certificates for client authentication or alternatively one
121 # huge file containing all of them (file must be PEM encoded)
122 SSLCACertificateFile /etc/pki/tls/certs/ca-bundle.crt
保存并退出
4.2 验证
重启httpd服务:systemctl restart httpd
在浏览器输入:https://192.168.47.100/
五、安装Mariadb
详细操作参考《Mariadb安装》
命令:[root@lamp_small home]# yum -y install mariadb mariadb-server
5.1 启动Mariadb,并设置自启动
5.2 设置root密码(默认为空)
5.3 新建数据库用户,可以远程连接数据库
便于开发使用
5.4 设置root可以远程访问数据库
5.5 设置数据库编码格式utf-8
5.6 如果用户量大,还需要设置最大连接数
5.7 修改配置文件/etc/my.cnf
重启数据库
六、安装PHP7
详细操作参考《PHP安装》
修改安装源,便于安装PHP7
[root@localhost ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@localhost ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
安装,安装之前可以查看有哪些PHP7版本可用
yum search php7
yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-fpm php71w-gd php71w-mbstring php71w-mysqlnd php71w-opcache php71w-pdo php71w-xml
安装插件:yum install php71w-posix
6.1 配置
配置文件:/etc/php.ini
将PHP的错误日志目录修改到/var/ww/logs目录下
568 error_log = /var/www/logs/php_errors.log
将PHP闲置的上传文件大小最大修改为20MB
799 upload_max_filesize = 20M
设置时区为上海时区
877 date.timezone = Asiz/Shanghai
修改httpd的配置文件,支持PHP:/etc/httpd/conf/httpd.conf
283 AddType application/x-compress .Z
284 AddType application/x-gzip .gz .tgz
后面添加如下内容
AddType application/x-httpd-php-source .phps
AddType application/x-httpd-php .php
重启数据库:systemctl restart mariadb
重启Apahce:systemctl restart httpd
6.2 验证
在发布目录/var/www/html下,建httpd.conf指定的首页index.php文件
[root@localhost html]# cat index.php
<?php
phpinfo();
?>
使用https://192.168.47.100