What is Apache?
Apache is the most popular web server on the Internet. More than half of the world's websites use Apache as a server. It is also an industrial grade Web server.
In this article, I mainly introduce how to install the Apache HTTP server. As long as you follow the steps in this tutorial, the installation will be successful. It should be noted that if you only install Apache, but not php, please delete the contents contained in FilesMatch in the configuration file, otherwise an error will be reported and PHP will not be found.
If you need to install PHP/MySQL database, this tutorial is very suitable for you. For the installation of PHP and MySQL, please read steps 11 and 12.
Note: This tutorial is applicable to the production environment. Apache is PHP based on FPM/FastCGI parsing.
System environment: Centos 7.4 Apache 2.4.29
1. Update the system
$ yum update && yum upgrade -y
2. Install expansion packs and dependent packs
$ yum install epel-release -y $ yum install gcc gcc-c++ openssl openssl-devel libtool expat-devel zlib-devel python-devel -y
3. Install pcre
$ wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.41.tar.gz $ tar zxvf pcre-8.41.tar.gz $ cd pcre-8.41 $ ./ configure $ make -j8 && make install
4. Install nghttp2
$ wget //github.com/nghttp2/nghttp2/releases/download/v1.27.0/nghttp2-1.27.0.tar.gz $ tar zxvf nghttp2-1.27.0.tar.gz $ cd nghttp2-1.27.0 $ ./ configure $ make -j8 && make install $ echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf $ ldconfig
5. Install Apache httpd
Create users and groups and download httpd
apr
apr-util
Installation package:
$ groupadd www $ useradd -g www www $ cd /tmp $ wget //mirrors.shuosc.org/apache//httpd/httpd-2.4.29.tar.gz $ wget //mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.6.3.tar.gz $ wget //mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-util-1.6.1.tar.gz
Decompress related packages:
$ tar xvf httpd-2.4.29.tar.gz $ tar xvf apr-1.6.3.tar.gz $ tar xvf apr-util-1.6.1.tar.gz
take apr-1.6.3
And apr-util-1.6.1
Move to httpd-2.4.29/srclib
Directory. The operation is as follows:
$ cd httpd-2.4.29 $ cp -R ../ apr-1.6.3 srclib/apr $ cp -R ../ apr-util-1.6.1 srclib/apr-util
be careful: No version number is allowed for the move, otherwise the library cannot be found during compilation and installation.
Start installation:
$ ./ configure \ --prefix=/usr/local/apache \ --enable-mods-shared=most \ --enable-headers \ --enable-mime-magic \ --enable-proxy \ --enable-so \ --enable-rewrite \ --with-ssl \ --with-nghttp2 \ --enable-ssl \ --enable-deflate \ --with-pcre \ --with-included-apr \ --with-apr-util \ --enable-mpms-shared=all \ --with-mpm=prefork \ --enable-remoteip \ --enable-http2 \ --enable-dav \ --enable-expires \ --enable-static-support \ --enable-suexec \ --enable-modules=all \
$ make -j8 && make install
Installation is complete.
6. Configure httpd.conf
$ cd /usr/local/apache/conf $ vim httpd.conf
In addition to the default configuration, please cancel the following comments and open related modules:
LoadModule ext_filter_module modules/mod_ext_filter.so LoadModule deflate_module modules/mod_deflate.so LoadModule expires_module modules/mod_expires.so LoadModule remoteip_module modules/mod_remoteip.so LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_connect_module modules/mod_proxy_connect.so LoadModule proxy_ftp_module modules/mod_proxy_ftp.so LoadModule proxy_http_module modules/mod_proxy_http.so LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so LoadModule proxy_scgi_module modules/mod_proxy_scgi.so LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so LoadModule proxy_ajp_module modules/mod_proxy_ajp.so LoadModule proxy_balancer_module modules/mod_proxy_balancer.so LoadModule proxy_express_module modules/mod_proxy_express.so LoadModule slotmem_shm_module modules/mod_slotmem_shm.so LoadModule ssl_module modules/mod_ssl.so LoadModule http2_module modules/mod_http2.so LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so LoadModule lbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.so LoadModule lbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.so LoadModule lbmethod_heartbeat_module modules/mod_lbmethod_heartbeat.so LoadModule suexec_module modules/mod_suexec.so LoadModule rewrite_module modules/mod_rewrite.so
Add the following at the end of the module:
<IfModule http2_module> ProtocolsHonorOrder On Protocols h2 http/1.1 </IfModule>
Modify the following parameter configurations
Set user groups to:
User www Group www
Default value:
#ServerName www.example.com:80
Revised as:
ServerName 0.0.0.0:80
be careful: If you create a virtual host based on a port, add a port in a new line, and then specify the port in the virtual host configuration file.
All:
AllowOverride None
Revised as:
AllowOverride All
In the following content location:
AddType application/x-compress . Z AddType application/x-gzip .gz .tgz
add to:
AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps
Cancel the following comments and add Apache support for python:
AddHandler cgi-script .cgi .py
Cancel the following comment:
Include conf/extra/httpd-mpm.conf Include conf/extra/httpd-vhosts.conf Include conf/extra/httpd-default.conf
The following content is added at the end of the file
Open GZIP:
<IfModule mod_headers.c> AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript <FilesMatch "\.(js|css|html|htm|png|jpg|swf|pdf|shtml|xml|flv|gif|ico|jpeg)$"> RequestHeader edit "If-None-Match" "^(.*)-gzip(.*)$" "$1$2" Header edit "ETag" "^(.*)-gzip(.*)$" "$1$2" </FilesMatch> DeflateCompressionLevel 6 SetOutputFilter DEFLATE </IfModule>
To set Apache security:
ProtocolsHonorOrder On PidFile /usr/local/apache/logs/httpd.pid ServerTokens ProductOnly ServerSignature Off
To import a virtual host configuration file:
IncludeOptional conf/vhost/*.conf
7. Create directory and virtual host configuration file
$ mkdir -p /usr/local/apache/conf/vhost $ mkdir -p /data/apps/web/renwolecom $ vim /usr/local/apache/conf/vhost/renwolecom.conf
Insert the following:
<VirtualHost *:80> ServerAdmin webmaster@example.com DocumentRoot "/data/apps/web/renwolecom" ServerName www.renwole.com ServerAlias renwole.com errorDocument 404 /404.html ErrorLog "/usr/local/apache/logs/renwolecom-error_log" CustomLog "/usr/local/apache/logs/renwolecom-access_log" combined <FilesMatch \.php$> SetHandler "proxy:unix:/tmp/php-cgi.sock| fcgi://localhost " </FilesMatch> <Directory "/data/apps/web/renwolecom"> SetOutputFilter DEFLATE Options FollowSymLinks AllowOverride All Require all granted DirectoryIndex index.php index.html index.htm default.php default.html default.htm </Directory> </VirtualHost>
Please modify the above content according to the actual path of your website.
Note: If there is no domain name, please change the domain name binding part to 127.0.0.1
, on http.conf
IP+port access can be achieved by adding other ports in. Remember to pass ports through the firewall.
be careful: It is recommended to empty httpd-vhosts.conf
Or do not open the reference, or there will be a warning when restarting the httpd service, but the use will not be affected. The main reason is to find the website directory path in the default configuration.
8. Create system unit startup file
$ vim /usr/lib/systemd/system/httpd.service
Add the following:
[Unit] Documentation=man:systemd-sysv-generator(8) SourcePath=/usr/local/apache/bin/apachectl Description=LSB: starts Apache Web Server Before=runlevel2.target Before=runlevel3.target Before=runlevel4.target Before=runlevel5.target Before=shutdown.target After=all.target After=network-online.target Conflicts=shutdown.target [Service] Type=forking Restart=no TimeoutSec=5min IgnoreSIGPIPE=no KillMode=process GuessMainPID=no RemainAfterExit=yes ExecStart=/usr/local/apache/bin/apachectl start ExecStop=/usr/local/apache/bin/apachectl stop
9. Add auto start/start/stop/restart
$ systemctl enable httpd $ systemctl start httpd $ systemctl stop httpd $ systemctl restart httpd
10. Set Firewall
$ firewall-cmd --permanent --zone=public --add-service=http $ firewall-cmd --permanent --zone=public --add-service=https $ firewall-cmd --reload
Then you can use the domain name or IP to access your website.
11. Deploy PHP
See《 Centos 7 source code compilation and installation php7.1 production 》
12. Deploy MySQL
See《 Centos 7 binary installation configuration MariaDB (MySQL) database 》
The installation and configuration of the Apache HTTP server has ended. If any error occurs during the installation and configuration process, please check /usr/local/apache/logs/
The error log below, so that you can quickly solve the problems encountered.
If you have any other suggestions, please leave a message.