CentOS7 yum lamp 虚拟主机配置 lamp各组件简单影响性能的参数调整--for 一定的环境需求

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: LAMP Server on CentOS 7 Updated Tuesday, January 13, 2015 by Joel Kruger This guide provides step-by-step instructions for installing a full-featured LAMP stack on a CentOS 7 system.

LAMP Server on CentOS 7

Updated Tuesday, January 13, 2015 by Joel Kruger

This guide provides step-by-step instructions for installing a full-featured LAMP stack on a CentOS 7 system.

In this guide, you will be instructed on setting up Apache, MariaDB, and PHP.

The CentOS 7 Project has elected to replace the init service management daemon with the newer, systemd daemon for this latest release. In addition, this release of CentOS ships with MariaDB repositories instead of MySQL. If you would prefer to use the more traditional MySQL instead, there are instructions provided below to use those repositories as well.

Throughout this guide we will offer several suggested values for specific configuration settings. Some of these values will be set by default. These settings are shown in the guide as a reference, in the event that you change these settings to suit your needs and then need to change them back.

Prerequisites

Your Linode should already be set up according to the instructions in our Getting Started guide, and it is suggested that security precautions be implemented. For assistance with this, please see our documentation: Securing Your Server

This guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo. If you’re not familiar with the sudo command, you can check our Users and Groups guide.

Set the Hostname

Before you begin installing and configuring the components described in this guide, please make sure you’ve followed our instructions for setting your hostname. Issue the following commands to make sure it is set properly:

1
2
hostname
hostname -f

The first command should show your short hostname, and the second should show your fully qualified domain name (FQDN).

Install and Configure the Apache Web Server

The Apache Web Server is a very popular choice for serving web pages. While many alternatives have appeared in the last few years, Apache remains a powerful option that we recommend for most uses.

  1. Update existing packages:

    1
    sudo yum update
    
  2. Install the current version of the Apache web server (in the 2.x series):

    1
    sudo yum install httpd
    
  3. The configuration for Apache is contained in the httpd.conf file, which is located at: /etc/httpd/conf/httpd.conf. We advise you to make a backup of this file into your home directory:

    1
    cp /etc/httpd/conf/httpd.conf ~/httpd.conf.backup
    

    By default, all files ending in the .conf extension in /etc/httpd and /etc/httpd/conf.d/ are treated as Apache configuration files, and we recommend placing your non-standard configuration options in files in these directories. Regardless how you choose to organize your configuration files, making regular backups of known working states is highly recommended.

  4. Edit the main Apache configuration file to adjust the resource use settings. The settings shown below are a good starting point for a Linode 1GB.

    /etc/httpd/conf/httpd.conf
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    KeepAlive Off
    
    ...
    
    <IfModule prefork.c>
    StartServers 2 MinSpareServers 6 MaxSpareServers 12 MaxClients 80 MaxRequestsPerChild 3000 </IfModule> 

Configure Name-based Virtual Hosts

There are different ways to set up virtual hosts; however, we recommend the method below. This configuration instructs Apache to listen on all IP addresses available to it.

  1. Create virtual host entries for each site that we need to host with this server. For this example we are using “example.com” and “example.org”.

    /etc/httpd/conf.d/vhost.conf
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    NameVirtualHost *:80
            
    <VirtualHost *:80>
        ServerAdmin webmaster@example.com ServerName example.com ServerAlias www.example.com DocumentRoot /srv/www/example.com/public_html/ ErrorLog /srv/www/example.com/logs/error.log CustomLog /srv/www/example.com/logs/access.log combined </VirtualHost> <VirtualHost *:80> ServerAdmin webmaster@example.org ServerName example.org ServerAlias www.example.org DocumentRoot /srv/www/example.org/public_html/ ErrorLog /srv/www/example.org/logs/error.log CustomLog /srv/www/example.org/logs/access.log combined </VirtualHost> 

    Notes regarding this example configuration:

    • All of the files for the sites that you host will be located in directories that exist underneath /srv/www. You can symbolically link these directories into other locations if you need them to exist elsewhere.
    • ErrorLog and CustomLog entries are suggested for more fine-grained logging, but are not required. If they are defined (as shown above), the logs directories must be created before you restart Apache.
  2. Before you can use the above configuration you’ll need to create the specified directories. For the above configuration, you can do this by issuing the following commands:

    1
    2
    3
    4
    5
    sudo mkdir -p /srv/www/example.com/public_html
    sudo mkdir /srv/www/example.com/logs
    
    sudo mkdir -p /srv/www/example.org/public_html
    sudo mkdir /srv/www/example.org/logs
    
  3. After you’ve set up your virtual hosts, you can issue the following commands to enable Apache to start on boot and run for the first time:

    1
    2
    sudo /bin/systemctl enable httpd.service
    sudo /bin/systemctl start httpd.service
    

Assuming that you have configured the DNS for your domain to point to your Linode’s IP address, virtual hosting for your domain should now work. Remember that you can create as many virtual hosts as you require.

Any time you change an option in your vhost.conf file, or any other Apache configuration file, remember to reload the configuration with the following command:

1
sudo /bin/systemctl reload httpd.service

Install and Configure MariaDB Database Server

MariaDB is a relational database management system (RDBMS) and ships by default in CentOS 7. MariaDB is a popular component in contemporary web development tool-chains, and is used to store data for many popular applications, including Wordpress and Drupal.

If you prefer to use the MySQL branded database in CentOS 7, you will need to add the required repositories by issuing the following command:

1
sudo yum install http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

Install MariaDB

  1. Install the MariaDB-server package:

    1
    sudo yum install mariadb-server
    
  2. CentOS 7 provides version 10.1.1 of MariaDB. Before you can use MariaDB some configuration is required. If you want to run MariaDB by default when the system boots, execute the following command:

    1
    sudo /bin/systemctl enable mariadb.service
    
  3. Now you can start the MariaDB daemon (mariadb) with the following command:

    1
    sudo /bin/systemctl start mariadb.service
    
  4. At this point, MariaDB should be ready to configure and run. While you shouldn’t need to change the configuration file, note that it is located at /etc/my.cnf for future reference. The default values should be fine for a Linode 1GB, but if you decide to adjust them you should first make a backup copy:

    1
    cp /etc/my.cnf ~/my.cnf.backup
    

Configure MariaDB and Set Up MariaDB databases

  1. After installing MariaDB, it’s recommended that you run mysql_secure_installation, a program that helps secure MariaDB. While running mysql_secure_installation, you will be presented with the opportunity to change the MariaDB root password, remove anonymous user accounts, disable root logins outside of localhost, and remove test databases and reload privileges. It is recommended that you answer yes to these options. Run the following command to execute the program:

    1
    mysql_secure_installation
    
  2. Next, we’ll create a database and grant your users permissions to use databases. First, log in to MariaDB:

    1
    mysql -u root -p
    

    Enter MariaDB’s root password, and you’ll be presented with a prompt where you can issue SQL statements to interact with the database.

    In the example below, example_database_name is the name of the database, example_user is used as the username, and example_password is used as the password for the root account on this newly established database. You should replace all three of these parameters to reflect your unique circumstances. It is recommended to create both a root and one additional user account within your database.

  3. To create a new database and grant your users permissions on it, issue the following commands. Note that the semi-colons (;) at the end of the lines are crucial for ending the commands:

    1
    2
    create database example_database_name;
    grant all on example_database_name.* to 'example_user'@'localhost' identified by 'example_password';
    
  4. Database user names and passwords are only used by scripts connecting to the database, and that database user account names need not (and perhaps should not) represent actual user accounts on the system. If you need to create additional users in the database you just created, simply run the command below, substituting the new user name and password where appropriate:

    1
    grant all on example_database_name.* to 'example_user2'@'localhost' identified by 'example_password2';
    
  5. With that completed, you’ve successfully configured MariaDB. To exit the MariaDB database administration utility issue the following command:

    1
    quit
    

With Apache and MariaDB installed you are now ready to move on to installing PHP to provide scripting support for your web pages.

Installing and Configuring PHP

PHP makes it possible to produce dynamic and interactive pages using your own scripts and web development frameworks. Furthermore, many popular web applications like WordPress are written in PHP. If you want to be able to develop your websites using PHP, you must first install it.

  1. CentOS includes packages for installing PHP from the terminal. Issue the following command:

    1
    sudo yum install php php-pear
    
  2. Once PHP5 is installed we’ll need to tune the configuration file located in /etc/php.ini to enable more descriptive errors, logging, and better performance. These modifications provide a good starting point if you’re unfamiliar with PHP configuration. Make sure that the following values are set, and relevant lines are uncommented (comments are lines beginning with a semi-colon [;]):

    /etc/php.ini
    1
    2
    3
    4
    5
    6
    7
    error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR
    display_errors = Off log_errors = On error_log = /var/log/php/error.log max_execution_time = 30 memory_limit = 128M max_input_time = 30 
  3. Create the log directory for PHP and give the Apache user ownership:

    1
    2
    sudo mkdir /var/log/php
    sudo chown apache /var/log/php
    
  4. If you need support for MariaDB or MySQL in PHP, then install the php5-mysql package with the following command:

    1
    sudo yum install php-mysql
    
  5. After making changes to PHP, restart Apache by issuing the following command:

    1
    sudo /bin/systemctl reload httpd
    

You should now have a fully functioning LAMP stack on your Linode! From here, you can serve up tons of content to your followers on the internet. If you are interested in setting up a WordPress site on your new LAMP stack, please consider reviewing this tutorial: Manage Web Content with WordPress.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
22天前
|
缓存 Ubuntu Linux
Linux中yum、rpm、apt-get、wget的区别,yum、rpm、apt-get常用命令,CentOS、Ubuntu中安装wget
通过本文,我们详细了解了 `yum`、`rpm`、`apt-get`和 `wget`的区别、常用命令以及在CentOS和Ubuntu中安装 `wget`的方法。`yum`和 `apt-get`是高层次的包管理器,分别用于RPM系和Debian系发行版,能够自动解决依赖问题;而 `rpm`是低层次的包管理工具,适合处理单个包;`wget`则是一个功能强大的下载工具,适用于各种下载任务。在实际使用中,根据系统类型和任务需求选择合适的工具,可以大大提高工作效率和系统管理的便利性。
116 25
|
2月前
|
缓存 Linux
CentOS-6的iso下载地址镜像yum源
通过上述步骤,您可以成功下载CentOS 6的ISO镜像文件,并配置适用于CentOS 6的YUM源。尽管CentOS 6已经停止更新,但使用这些镜像和YUM源配置,可以继续在需要的环境中使用和维护CentOS 6系统。
669 20
|
2月前
|
Linux
CentOs9 yum源出现404问题
CentOs9-stream yum源出现404问题
|
3月前
|
缓存 Linux
解决CentOS 7停止更新后yum源失效问题【图文教程】
以上步骤完成后,你的 CentOS 7 系统就会使用阿里云的 yum 源,更换yum以后就可以正常使用啦。
2671 2
|
5月前
|
Linux 虚拟化 开发者
一键将CentOs的yum源更换为国内阿里yum源
一键将CentOs的yum源更换为国内阿里yum源
7761 8
|
5月前
|
缓存 Linux 编译器
【C++】CentOS环境搭建-安装log4cplus日志组件包及报错解决方案
通过上述步骤,您应该能够在CentOS环境中成功安装并使用log4cplus日志组件。面对任何安装或使用过程中出现的问题,仔细检查错误信息,对照提供的解决方案进行调整,通常都能找到合适的解决之道。log4cplus的强大功能将为您的项目提供灵活、高效的日志管理方案,助力软件开发与维护。
172 0
|
6月前
|
Linux
centos使用阿里的yum源
centos使用阿里的yum源
|
7月前
|
缓存 Linux 网络安全
解决 CentOS 7 官方 yum 仓库无法使用的最佳实践
【8月更文挑战第18天】若 CentOS 7 的官方 YUM 仓库无法使用,可按以下步骤解决: 1. **检查网络连接**: - 确认服务器能正常上网,可通过访问外部网站或网络诊断测试。 - 检查防火墙设置,避免其阻挡 YUM 的网络访问。 2. **检查 YUM 配置**: - 核实 `/etc/yum.repos.d/` 下的 `CentOS-Base.repo` 文件中仓库地址正确无误。 - 确认配置文件内的 `enabled` 选项设为 `1` 以启用仓库。
2817 0
|
Linux 虚拟化
CentOS 7.X配置连接网络
应用场景 Linux虚拟机,系统安装完毕后,无法连接网络,由于是最小化安装,很多命令无法直接yum安装,无法连接外网wget下载资源等等,造成很大的不便,因此需要进行配置连接外网! 操作指南 1. 开启VMware NAT Service 右击“计算机”,选择“管理”,在“服务和应用程序”中,选择“服务”,在右边找到“VMware NAT Service”服务,进行开启。
1206 0
|
Linux 虚拟化 网络协议
CentOS 6.X配置连接网络
应用场景 Linux虚拟机,系统安装完毕后,无法连接网络,由于是最小化安装,很多命令无法直接yum安装,无法连接外网wget下载资源等等,造成很大的不便,因此需要进行配置连接外网! 操作指南 1. 开启VMware NAT Service 右击“计算机”,选择“管理”,在“服务和应用程序”中,选择“服务”,在右边找到“VMware NAT Service”服务,进行开启。
850 0

热门文章

最新文章