CentOS7服务器搭建LAMP环境

本文涉及的产品
RDS AI 助手,专业版
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
RDS DuckDB + QuickBI 企业套餐,8核32GB + QuickBI 专业版
简介: CentOS7服务器搭建LAMP环境

CentOS7.4上搭建LAMP环境,这里以centos7.4为例;

工具/原料

  • centos系统一台
  • 安全组放行80,22端口
  • 关闭防火墙和SELinux

安装Apache方法/步骤


使用的例子:服务器版本内核。


01ebd755782e4c909dad0843d3544acf.jpeg

Xshell连接到您的服务器上,使系统处于最新状态执行以下命令,


查看centos版本 lsb_release -a


更新centos系统


yum -y update 3


利用yum命令安装Apache执行命令,


查看Apache版本httpd -v


yum -y install httpd4


启动httpd并且设置为它开机启动,


systemctl start httpd 【启动httpd命令】


systemctl enable httpd 【设置httpd开机启动】


systemctl restart httpd (重启)5


我们这里可通过俩个命令查看是否启动和开机启动;


systemctl status httpd 【查看是否启动命令】


systemctl is-enabled httpd 【查看是否开机启动(输出enabled已经成功)】


注意:后续检查数据库的方法也是大同小异作不在详细解释。


安装成功后,在浏览器地址栏输入你的服务器IP地址出现下图说明你的httpd已经成功安装,例如我这里:39.104.82.85


01ebd755782e4c909dad0843d3544acf.jpeg

查看Apache版本httpd -v


  1. 安装数据库Mariadb它是MySQL的一个分支几乎兼容mysql所有功能,执行下面命令;


yum -y install mariadb-server mariadb


  1. 启动mariadb并且设置为它开机启动;检查是否启动和开机启动


systemctl start mariadb


systemctl enable mariadb


systemctl status mariadb


systemctl is-enabled mariadb


systemctl restart mariadb(重启)


  1. 下面配置root密码和数据库的一些安全;执行命令,

mysql_secure_installation


In order to log into MariaDB to secure it, we'll need the current


password for the root user. If you've just installed MariaDB, and


you haven't set the root password yet, the password will be blank,


so you should just press enter here.


Enter current password for root (enter for none):


按回车


#这里询问我们是否设置root密码,输入y设置密码,y(设置); n(不设置)


Setting the root password ensures that nobody can log into the MariaDB


root user without the proper authorisation.


Set root password? [Y/n]


#输入密码再次确认密码回车


Set root password? [Y/n] y


New password:


Re-enter new password:


Password updated successfully!


Reloading privilege tables..


... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone


to log into MariaDB without having to have a user account created for


them. This is intended only for testing, and to make the installation


go a bit smoother. You should remove them before moving into a


production environment.


Remove anonymous users? [Y/n] y (询问是否移除匿名用户输入y回车)


... Success!


Normally, root should only be allowed to connect from 'localhost'. This


ensures that someone cannot guess at the root password from the network.


Disallow root login remotely? [Y/n] n(询问是否禁止远程root登陆我这里选择y禁止远程登录【如果您需要远程登录连接数据库可选择n】)


... skipping.


By default, MariaDB comes with a database named 'test' that anyone can


access. This is also intended only for testing, and should be removed


before moving into a production environment.


Remove test database and access to it? [Y/n] y(询问是否删除测试数据库【可选项随意y或n】这里我选择y)


Reloading the privilege tables will ensure that all changes made so far


will take effect immediately.


Reload privilege tables now? [Y/n] y (询问是否现在重新加载权限表选择y回车)


... Success!


Cleaning up...


All done! If you've completed all of the above steps, your MariaDB


installation should now be secure.


installation should now be secure.


Thanks for using MariaDB!


  1. 我们可以简单登录下数据库;


mysql -uroot -p


Enter password: 【输入密码回车即可登录进去】


Welcome to the MariaDB monitor. Commands end with ; or \g.

Your MariaDB connection id is 13


Server version: 5.5.56-MariaDB MariaDB Server


Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


MariaDB [(none)]> show databases; 【命令显示数据库列表】


MariaDB [(none)]> show databases; 【命令显示数据库列表】

+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |

4 rows in set (0.01 sec)


MariaDB [(none)]> quit 【退出数据库】


Bye


安装php7.2


查看PHP版本 php -v


1> 、安装源


安装php72w,是需要配置额外的yum源地址的,否则会报错不能找到相关软件包。


php高版本的yum源地址,有两部分,其中一部分是epel-release,另外一部分来自webtatic。如果跳过epel-release的话,安装webtatic的时候,会有错误爆出。所以,这里需要的命令是:

yum install epel-release -y


rpm -Uvh mirror.webtatic.com/yum/el7/web…


2> 清除历史版本


为了防止centos上面发生php冲突,所以,这个命令还是先执行一下更好些。


yum -y remove php*


3、安装扩展包


事实上,这里面的对应扩展库很多,这里大家一定要注意cli和fpm这两个包,而其它的相关包就看您需要了。


yum -y install php72w php72w-cli php72w-fpm php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml php72w-xmlrpc php72w-mysql php72w-imap php72w-ldap php72w-odbc php72w-pear php72w-mcrypt


4、安装完成以后,启动服务


systemctl enable php-fpm.service


systemctl start php-fpm.service


  1. 安装成功重启HTTPD


systemctl restart httpd


5> 查看php 版本


php -v //输入命令


13.安装PHP,执行命令;


yum -y install php**(默认安装php5.4版本不建议13,14条)**


14.查看所有组件,执行命令;(默认安装php5.4版本不建议13,14条)


yum search 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


15.完成后我们可以新建一个PHP页面来查看我们安装的组件,执行命令;


vim /var/www/html/info.php


#打开后按键盘字母 i


#编辑为以下内容;


#编辑为以下内容;

#vim使用方法可自行百度,不作详细解释


  1. 最后重启的httpd服务,执行命令;

systemctl restart httpd


打开浏览器输入:39.104.82.85/info.php(即IP地址 / 和文件名)


#看到这个页面证明您的LAMP环境搭建httpd发布目录默认

在/var/www/html/


#您可以使用winscp上传您的页面到发布目录


  1. ###最后执行命令 rm -rf /var/www/html/info.php 删除你的这种测试文件###


注意事项


  • 切记删除这些文件:rm -rf /var/www/html/info.php

第三 同IP同服务器下多网站部署


使用FTP:FileZilla软件


  1. 单网站,默认项目目录:/var /www/html

多网站,其他项目目录:/var /www/项目1

/var/www/项目2

  1. /etc/httpd/conf/httpd.conf****修改文件尾部添加:

DocumentRoot /var/www/项目1


ServerName www.项目1.com


  Options FollowSymLinks   AllowOverride All  


DocumentRoot /var/www/项目2


ServerName www.项目2.cn


  Options FollowSymLinks   AllowOverride All  

<VirtualHost *:80>
    DocumentRoot /var/www/jqbs
    ServerName 9wuquan.cn
    ServerAlias *.9wuquan.cn
<Directory />
     Options FollowSymLinks
     AllowOverride All
</Directory>
</VirtualHost>
<VirtualHost *:443>
  DocumentRoot /var/www/jqbs
  ServerName 9wuquan.cn
  ServerAlias *.9wuquan.cn
  SSLEngine on
  SSLProtocol TLSv1 TLSv1.1 TLSv1.2
  SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5
<Directory />
      Options FollowSymLinks ExecCGI
      AllowOverride All
      Order allow,deny
      Allow from all
      Require all granted
</Directory>
SSLCertificateFile /etc/letsencrypt/live/9wuquan.cn/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/9wuquan.cn/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateChainFile /etc/letsencrypt/live/9wuquan.cn/chain.pem
</VirtualHost>

httpd.conf配置文件的修改主要有以下几项:


(2.安装apache的mod_ssl.so模块yum -y install mod_ssl)

LoadModule ssl_module modules/mod_ssl.so
LoadModule rewrite_module modules/mod_rewrite.so
  1. ServerSignature On => ServerSignature Off // 配置错误页不显示Apache版本
  2. Options Indexes FollowSymLinks => Options FollowSymLinks // 配置Apache不能通过目录层级进行文件访问
AllowOverride All
  1. AllowOverride None => AllowOverride All // 配置允许.htaccess
  2. DirectoryIndex index.html => DirectoryIndex index.html index.php // 配置Apache支持.php文件解析

最后,通过.htaccess进行301转向

<IfModule mod_rewrite.c>
  Options +FollowSymlinks -Multiviews
  RewriteEngine On
  RewriteCond   %{HTTPS} !=on
  RewriteRule   ^(.*)  https://%{SERVER_NAME}/$1 [L,R=302]
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]
</IfModule>
# 图片和Flash内容缓存一个月
<FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf)$">
Header set Cache-Control "max-age=2592000"
</FilesMatch>
<FilesMatch ".(ttf)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>

3, systemctl restart httpd

此时也是一级,二级域名都可以访问

第四 远程连接MySQL

1. 增加帐号。MySQL服务器root帐号被配置成了禁止远程登入,因此需要新增加一个MySQL帐号用于远程管理:

[root@centos-server ~]$ mysql --host localhost --user root --password mysql

mysql> CREATE USER 'owen'@'%' IDENTIFIED BY 'owen';

mysql> GRANT ALL PRIVILEGES ON *.* TO 'owen'@'%' WITH GRANT OPTION;


2.在阿里云上开启3306端口

01ebd755782e4c909dad0843d3544acf.jpeg

01ebd755782e4c909dad0843d3544acf.jpeg

01ebd755782e4c909dad0843d3544acf.jpeg

3.使用数据库软件在本地使用owen账号远程连接mysql


相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
目录
相关文章
|
11月前
|
安全 Linux Shell
使用SCP命令在CentOS 7上向目标服务器传输文件
以上步骤是在CentOS 7系统上使用SCP命令进行文件传输的基础,操作简洁,易于理解。务必在执行命令前确认好各项参数,尤其是目录路径和文件名,以避免不必要的传输错误。
986 17
|
11月前
|
Linux 编译器 开发工具
在CentOS环境下升级GCC编译器的指南
总结:本文提供了一种方法来升级CentOS的GCC编译器,通过使用CentOS的软件集合和开发者工具集工具,可以比较平滑地进行升级。在整个过程中无需从源代码编译,这样既省去了复杂的编译过程,也避免了可能出现的与系统库不兼容的风险。请注意,使用第三方仓库可能会带来系统稳定性和安全性上的潜在影响。所有操作都应谨慎进行,并确保有相应的数据备份。
1340 19
|
10月前
|
人工智能 缓存 监控
构建高效MCP客户端:应对多服务器环境的完整指南
本文深入探讨了在多服务器环境下构建高效、可靠的Model Context Protocol(MCP)客户端的关键技术与最佳实践。内容涵盖MCP基础架构、连接管理、请求路由、容错机制、会话管理、性能监控及安全认证等核心设计,提供了完整的实现类与部署配置示例,助力开发者构建高性能MCP客户端,提升AI模型与工具集成的效率与稳定性。
|
10月前
|
Ubuntu 安全 小程序
服务器版本的CentOS和Ubuntu哪个更适合你?
但是以上的比较并不说明Ubuntu是不稳定的或者是不安全的,只是以上比较过程中,在稳定性方面Ubuntu稍微逊色了一点。由于Ubuntu在个人桌面电脑的使用率远远高于CentOS,用Ubuntu搭建服务器,如果遇到什么问题,寻找解决方案相对比较容易,这让Ubuntu在选择方面更优于CentOS。如果你是一个初学者,那么毫无疑问Ubuntu是更适合的选择。如果你正在经营自己的公司,在这两者之间,CentOS会更好一些。
|
11月前
|
安全 关系型数据库 网络安全
安全加固:启动PostgreSQL 14服务器SSL加密的方法指南在CentOS 7环境中
通过上述步骤,你可以为PostgreSQL数据库服务器设置SSL加密,从而增加数据在传输中的安全性。确保维持证书的有效性,并且定期更新和管理密钥,以防止未授权访问。
493 0
|
8月前
|
弹性计算 运维 安全
阿里云轻量应用服务器与云服务器ECS啥区别?新手帮助教程
阿里云轻量应用服务器适合个人开发者搭建博客、测试环境等低流量场景,操作简单、成本低;ECS适用于企业级高负载业务,功能强大、灵活可扩展。二者在性能、网络、镜像及运维管理上差异显著,用户应根据实际需求选择。
687 10
|
8月前
|
弹性计算 ice
阿里云4核8g服务器多少钱一年?1个月和1小时价格,省钱购买方法分享
阿里云4核8G服务器价格因实例类型而异,经济型e实例约159元/月,计算型c9i约371元/月,按小时计费最低0.45元。实际购买享折扣,1年最高可省至1578元,附主流ECS实例及CPU型号参考。
781 8
|
8月前
|
运维 安全 Ubuntu
阿里云渠道商:服务器操作系统怎么选?
阿里云提供丰富操作系统镜像,涵盖Windows与主流Linux发行版。选型需综合技术兼容性、运维成本、安全稳定等因素。推荐Alibaba Cloud Linux、Ubuntu等用于Web与容器场景,Windows Server支撑.NET应用。建议优先选用LTS版本并进行测试验证,通过标准化镜像管理提升部署效率与一致性。
|
8月前
|
存储 监控 安全
阿里云渠道商:云服务器价格有什么变动?
阿里云带宽与存储费用呈基础资源降价、增值服务差异化趋势。企业应结合业务特点,通过阶梯计价、智能分层、弹性带宽等策略优化成本,借助云监控与预算预警机制,实现高效、可控的云资源管理。