Summary
From Starting from scratch with Linux, about setting up an Apache server and LAMP. For more information on web servers in general, please refer to “Web Server Overview“.
Apace Server
The Apache HTTP Server has the highest market share as a web server used on Linux.
There are several versions of Apache. The most recent operating systems (CentOS7 and CentOS8) use the 2.4 version. Different version systems may have different settings and functions, but if they are the same series, there should be no major differences.
Apache Installation
To install Apache on Linux systems, you can download the latest version from the Apache website and install it, but since the RPM package is not advocated, you need to compile the source code, manage security and version control, and integrate other software. This is complicated by the need to compile the source code, manage security and version control, and integrate with other software. The package name of Apache is httpd.
>sudo yum -y install httpd
(Omitted)
Installed:
httpd.x86_64:2.4.6-4.0.e17.centos
Dependency Installed
httpd-tools.x86_64 0:1.4.6-40e17.centos. …
Complete!
Apache httpd is installed by default on macs. To check, type the following command in the terminal to see the path where httpd is installed.
> which httpd
/usr/sbin/httpd
In addition, check the path to “apachectl”, the controller for starting and stopping Apache httpd, below.
> which apachectl
/usr/sbin/apachectl
The version can be checked below.
> /usr/sbin/httpd -version
Server version: Apache/2.4.54 (Unix)
Server built: Aug 18 2022 20:00:37
We can confirm that version 2.4.54 is installed.
It is also possible to install it separately by “brew install httpd” using home bew.
For Windows, go to the download page from the Apache website mentioned above and select “Files for Microsoft Windows” to install.
Running Apache
The top directory for web publishing is called the document root. By default, the document root is the “/var/www/html” directory. (On a mac, it is “/Library/WebServer/Documents/”), which means that the file “/var/www/html/index.html” can be created and accessed from the outside as “http://サーバー名/index.html”.
Under the document root, root user privileges are required to write files.
The Apache configuration file is located in “/etc/httpd/conf/httpd.conf” (or “/etc/apache2/httpd.conf” for mac).
The contents (in part) are as follows
ServerRoot: The top of the directory tree under which the server's
# configuration, error, and log files are kept.
#
# Do not add a slash at the end of the directory path. If you point
# ServerRoot at a non-local disk, be sure to specify a local disk on the
# Mutex directive, if file-based mutexes are used. If you wish to share the
# same ServerRoot for multiple httpd daemons, you will need to change at
# least PidFile.
#
ServerRoot "/usr"
Lines beginning with “#” are not settings but comments with explanations.
First, there is a “directive set value” as a setting. The main directives are as follows.
Launch Apache
Apache starts up as follows on Linux.
> sudo systemctl start httpd.service
Configure firewall settings for Linux Apache, which by default does not allow web access.
> sudo firewall-cmd -permanent --add-service=http
success
> sudo firewall-cmd --reload
success
You can now access the site from a Web browser. Enter “http://VPSのIPアドレス/” in your browser and the following test page will appear.
Create an HTML file in the document root as follows
# /var/www/html/htmltest.html
<html>
<head>
<title>Test Page</title>
</head>
<body>
<p>Apache test page</p>
</body>
<html>
If you access “http://VPSのIPアドレス/htmltest.html” with a web browser, you will see the following
In the case of mac, it is as follows.
> sudo apachectl start
If the operation is successful, you can see the following when you view “http://localhost” in your browser.
Then, the system can be used by configuring “password authentication,” “access log,” and other settings in the configuration file section.
LAMP
To run a web application on a server, Apache alone is not enough; a database is required to store the data exchanged on the web. environment), and a database (database management system).
XAMPP and MAMP are tools for installing all of these together, and were previously introduced in “How to start MAMP and media wiki and how to use them easily“.
A database is a collection of data stored according to certain rules for easy retrieval and management. The software that manages the database is a database management system (DBMS: DataBase Management System), which includes commercial products such as Oracle Databse and Microsoft SQL Server, as well as well-known open source products such as MySQL, MariaDB and PostgreSQL are well known as DBMSs, along with commercial products such as Oracle Databse and Microsoft SQL Server.
In the Linux world, MySQL has been used worldwide, and MariaDB, which is installed in CentOS, is a database management system that has been developed by branching out from MySQL.
Various programming languages are used to create web applications, but a scripting language called Lightweight Language (LL) is commonly used. Programming languages can be broadly classified into two types: compiler-type languages, which compile source code into executable form, and interpreter-type languages, which can be executed as source code. Interpreter-type languages are sometimes called scripting languages. Among scripting languages, PHP, Pyhton, Perl, Ruby, etc. are commonly used for creating Web applications.
Installing MariaDB
This section describes the installation of MariaDB. 2 packages are installed using yum command for Linux.
> sudo yum -y install mariadb-server mariadb
The above will install MariaDB itself and MariaDB client software. Next, modify the configuration file /etc/my.cnf to handle Japanese correctly.
> sudo nano /etc/my.cnf
Access the file using nano, an editor for unix. Add a fourth line to the following section and save the file.
………
[mysql]
datadir=/var/lib/mysql
socket=/vqr/lib/mysql/mysql.sock
character-set-server=utf8. ← Add this line
Start mariaDB.
> sudo ststemctl start mariadb
> (sudo systemctl enable mariadb. ←Automatic startup at system startup)
Initial configuration of mariaDB. The initial configuration command mysql_secure_installation can be run to interactively configure the initial settings.
>sudo mysql_secure_installation
Enter “yes” basically, and enter the MariaDB admin password in the “New pasword:” and “Re-enter new password” fields.
For mac, install by “brew install mariadb” of homebrew, and set the password by “sudo mysql_secure_installation”.
Installing PHP
PHP is installed with the php package, which is the main body of PHP, the php-mbstring package to handle multibyte characters such as Japanese, the php-gd package to handle image libraries in PHP, and the php-mysql package to integrate PHP with MariaDB/MySQL.
For linux, do the following
> sudo yum -y install php php-mbstring php-gd php-mysql
You can verify that PHP is installed by running “php -version”.
Since PHP was installed after Apache, restart Apache so that it can work with PHP.
sudo systemctl restart httpd.service
Verify that the PHP program works. Create a test file as /var/www/html/phptest.php
sudo nano /var/www/html/phptest.php
Fill in the following code
<?php echo phi9nfo(); ?>
Access “http://VPSのIPアドレス/phptest.php” from a web browser, and if the PHP information is displayed, it is working. After confirming that it is working, delete the file.
sudo rm /var/www/html/phptest.php
For mac, use hombrew and install with “brew install php@8.0” and pass through the path (write the following in a file such as zshrc)
> echo 'export PATH="/opt/homebrew/opt/php@8.0/bin:$PATH"' >> ~/.zshrc
> echo 'export PATH="/opt/homebrew/opt/php@8.0/sbin:$PATH"' >> ~/.zshrc
After executing the above command, restart the shell to read the path, and if the version of PHP is output by “php –version”, the path has passed.
After the path is passed, add the following code to the profile of the shell (zshrc).
> export LDFLAGS="-L/opt/homebrew/opt/php@8.0/lib"
> export CPPFLAGS="-I/opt/homebrew/opt/php@8.0/include"
After that, you can use the MariaDB/MySQL client command to connect to MAriDB as an administrator of MAriaD, and create a database with “CREATE DATABASE” to use the database.
コメント