时间:2023-07-14 06:51:01 | 来源:网站运营
时间:2023-07-14 06:51:01 来源:网站运营
在Centos7上安装apache并且建立虚拟主机:照搬英文,有问题的地方会标注:yum
package manager.httpd
package index to reflect the latest upstream changes:sudo yum update httpd
Once the packages are updated, install the Apache package:sudo yum install httpd
After confirming the installation, yum
will install Apache and all required dependencies.firewalld
on your server and you’ll need to open up port 80
to allow Apache to serve requests over HTTP. If you haven’t already done so, you can do this by enabling firewalld
’s http
service with the following command:sudo firewall-cmd --permanent --add-service=http
If you plan to configure Apache to serve content over HTTPS, you will also want to open up port 443
by enabling the https
service:sudo firewall-cmd --permanent --add-service=https
Next, reload the firewall to put these new rules into effect:sudo firewall-cmd --reload
After the firewall reloads, you are ready to start the service and check the web server.)sudo systemctl start httpd
Verify that the service is running with the following command:sudo systemctl status httpd
You will see an active
status when the service is running:OutputRedirecting to /bin/systemctl status httpd.service● httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled) Active: active (running) since Wed 2019-02-20 01:29:08 UTC; 5s ago Docs: man:httpd(8) man:apachectl(8) Main PID: 1290 (httpd) Status: "Processing requests..." CGroup: /system.slice/httpd.service ├─1290 /usr/sbin/httpd -DFOREGROUND ├─1291 /usr/sbin/httpd -DFOREGROUND ├─1292 /usr/sbin/httpd -DFOREGROUND ├─1293 /usr/sbin/httpd -DFOREGROUND ├─1294 /usr/sbin/httpd -DFOREGROUND └─1295 /usr/sbin/httpd -DFOREGROUND...
As you can see from this output, the service appears to have started successfully. However, the best way to test this is to request a page from Apache.hostname -I
Notice:上述命令对于弹性公网IP只会返回内网IPcurl
to request your IP from icanhazip.com
, which will give you your public IPv4 address as seen from another location on the internet:curl -4 icanhazip.com
When you have your server’s IP address, enter it into your browser’s address bar:http://your_server_ip
You’ll see the default CentOS 7 Apache web page:systemctl
commands to manage the service.sudo systemctl stop httpd
To start the web server when it is stopped, type:sudo systemctl start httpd
To stop and then start the service again, type:sudo systemctl restart httpd
If you are simply making configuration changes, Apache can often reload without dropping connections. To do this, use this command:sudo systemctl reload httpd
By default, Apache is configured to start automatically when the server boots. If this is not what you want, disable this behavior by typing:sudo systemctl disable httpd
To re-enable the service to start up at boot, type:sudo systemctl enable httpd
Apache will now start automatically when the server boots again.example.com
, but you should replace this with your own domain name. To learn more about setting up a domain name with DigitalOcean, see our Introduction to DigitalOcean DNS./var/www/html
directory. While this works well for a single site, it can become unwieldy if you are hosting multiple sites. Instead of modifying /var/www/html
, you will create a directory structure within /var/www
for the example.com
site, leaving /var/www/html
in place as the default directory to be served if a client request doesn’t match any other sites.html
directory for example.com
as follows, using the -p
flag to create any necessary parent directories:sudo mkdir -p /var/www/example.com/html
Create an additional directory to store log files for the site:sudo mkdir -p /var/www/example.com/log
Next, assign ownership of the html
directory with the $USER
environmental variable:sudo chown -R $USER:$USER /var/www/example.com/html
Make sure that your web root has the default permissions set:sudo chmod -R 755 /var/www
Next, create a sample index.html
page using vi
or your favorite editor:sudo vi /var/www/example.com/html/index.html
Press i
to switch to INSERT
mode and add the following sample HTML to the file:插入:/var/www/example.com/html/index.html<html> <head> <title>Welcome to Example.com!</title> </head> <body> <h1>Success! The example.com virtual host is working!</h1> </body></html>
Save and close the file by pressing ESC
, typing :wq
, and pressing ENTER
.sites-available
directory to store them in. You will also create the sites-enabled
directory that tells Apache that a virtual host is ready to serve to visitors. The sites-enabled
directory will hold symbolic links to virtual hosts that we want to publish. Create both directories with the following command:sudo mkdir /etc/httpd/sites-available /etc/httpd/sites-enabled
Next, you will tell Apache to look for virtual hosts in the sites-enabled
directory. To accomplish this, edit Apache’s main configuration file and add a line declaring an optional directory for additional configuration files:sudo vi /etc/httpd/conf/httpd.conf
Add this line to the end of the file:IncludeOptional sites-enabled/*.conf
Save and close the file when you are done adding that line. Now that you have your virtual host directories in place, you will create your virtual host file.sites-available
directory:sudo vi /etc/httpd/sites-available/example.com.conf
Add in the following configuration block, and change the example.com
domain to your domain name:<VirtualHost *:80> ServerName www.example.com ServerAlias example.com DocumentRoot /var/www/example.com/html ErrorLog /var/www/example.com/log/error.log CustomLog /var/www/example.com/log/requests.log combined</VirtualHost>
This will tell Apache where to find the root directly that holds the publicly accessible web documents. It also tells Apache where to store error and request logs for this particular site.sites-enabled
directory:sudo ln -s /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-enabled/example.com.conf
Your virtual host is now configured and ready to serve content. Before restarting the Apache service, let’s make sure that SELinux has the correct policies in place for your virtual hosts.httpd_unified
boolean. While this approach is more convenient, it will not give you the same level of control as an approach that focuses on a file or directory policy.sudo setsebool -P httpd_unified 1
Notice:上述命令可能会返回setsebool: SELinux is disabled的错误,这是因为在某些服务器的初始设定为disable,需要改变设置,具体看链接:https://zhuanlan.zhihu.com/p/161413585setsebool
command changes SELinux boolean values. The -P
flag will update the boot-time value, making this change persist across reboots. httpd_unified
is the boolean that will tell SELinux to treat all Apache processes as the same type, so you enabled it with a value of 1
./var/www/example.com/log
directory will give you more control over your Apache policies, but may also require more maintenance. Since this option is not universally setting policies, you will need to manually set the context type for any new log directories specified in your virtual host configurations./var/www/example.com/log
directory:sudo ls -dZ /var/www/example.com/log/
Outputdrwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/example.com/log/
The current context is httpd_sys_content_t
, which tells SELinux that the Apache process can only read files created in this directory. In this tutorial, you will change the context type of the /var/www/example.com/log
directory to httpd_log_t
. This type will allow Apache to generate and append to web application log files:sudo semanage fcontext -a -t httpd_log_t "/var/www/example.com/log(/.*)?"
Notice:semanage is an SELinux (Security-Enhanced Linux) management tool that is used to configure specific elements without making any adjustments to or reassembling from policy sources. Semanage consist of mapping from Linux username to SELinux user identities and it also includes mapping security context for numerous kind of objects like interface, network port, etc.
I was wondering how to fix this error and I am not able to find what package provides semanage command. After a bit of research, I came to know that you need to use yum provides the option to find out the package that provides the queried file called /usr/sbin/semanage.
In this short quick article, we will explain how to install necessary packages for getting semanage command using the yum command.
# yum provides /usr/sbin/semanage
From the above sample output, you can see that we need to installpolicycoreutils-python-utils-2.8-16.1.el8.noarchpackage to use the semanage command.
# yum install policycoreutils-python-utils
注意并不一定你缺少的就是policycoreutils-python-utils,要看第一张图缺少的是什么再安装,如下图Next, use the
restorecon
command to apply these changes and have them persist across reboots:sudo restorecon -R -v /var/www/example.com/log
The -R
flag runs this command recursively, meaning it will update any existing files to use the new context. The -v
flag will print the context changes the command made. You will see the following output confirming the changes:Outputrestorecon reset /var/www/example.com/log context unconfined_u:object_r:httpd_sys_content_t:s0->unconfined_u:object_r:httpd_log_t:s0
You can list the contexts once more to see the changes:sudo ls -dZ /var/www/example.com/log/
The output reflects the updated context type:Outputdrwxr-xr-x. root root unconfined_u:object_r:httpd_log_t:s0 /var/www/example.com/log
Now that the /var/www/example.com/log
directory is using the httpd_log_t
type, you are ready to test your virtual host configuration./var/www/example.com/log
directory. You can now successfully restart the Apache service:sudo systemctl restart httpd
List the contents of the /var/www/example.com/log
directory to see if Apache created the log files:ls -lZ /var/www/example.com/log
You’ll see that Apache was able to create the error.log
and requests.log
files specified in the virtual host configuration:Output-rw-r--r--. 1 root root 0 Feb 26 22:54 error.log-rw-r--r--. 1 root root 0 Feb 26 22:54 requests.log
Now that you have your virtual host set up and SELinux permissions updated, Apache will now serve your domain name. You can test this by navigating to http://example.com
, where you should see something like this:关键词:建立,虚拟,主机,并且,安装