1. 程式人生 > >如何更改linux(centos)下的Apache http端口號

如何更改linux(centos)下的Apache http端口號

eterm oot man ORC imp selinux策略 nsa audit content

# vi /etc/httpd/conf/httpd.conf 文件


修改兩個地方
#Listen 12.34.56.78:80
Listen 80
#把80改為你設置的端口,我設置端口為82
Listen 82

NameVirtualHost *:80
#把80改為你設置的端口,我設置端口為82
NameVirtualHost *:82

啟動Apache
# service httpd start 發現啟動失敗

解決:(轉自)http://blog.itechol.com/space-33-do-blog-id-5114.html

Google 一下,發現原來是 SELinux 安全機制的作用。

(很難搞,如果很多牽扯的權限的事情找不到原因,就可以分析是否是它的作用)

解決方法如下:

查看selinux狀態:

[root@cacti-test httpd]# sestatus

SELinux status: enabled

SELinuxfs mount: /selinux

Current mode: enforcing

Mode from config file: enforcing

Policy version: 21

Policy from config file: targeted

或者用

[root@cacti-test httpd]# getenforce

Enforcing

關閉selinux狀態:(使用無啟重啟系統的方法)

詳見:http://blog.itechol.com/space-33-do-blog-id-5088.html

[root@cacti-test httpd]# setenforce 0 關閉命令

[root@cacti-test httpd]# getenforce 重新查看selinux狀態

Permissive

嘗試再次啟動apache

[root@cacti-test httpd]# service httpd start

Starting httpd: httpd: Could not reliably determine the server‘s fully qualified domain name, using 127.0.0.1 for ServerName

[ OK ] 成功!!

附: selinux 管理命令semanage詳解

semanage使用詳解


NAME

semanage - SELinux Policy Management tool

SYNOPSIS

Output local customizations:導出selinux當前策略
semanage [ -S store ] -o [ output_file | - ]

Input local customizations:導入selinux策略
semanage [ -S store ] -i [ input_file | - ]

Manage booleans. Booleans allow the administrator to modify the confinement of processes based on his configuration.:管理一些進程、服務的開關、配置等等,全是開關兩個狀態
semanage boolean [-S store] -{d|m|l|n|D} -[-on|-off|1|0] -F boolean | boolean_file

Manage SELinux confined users (Roles and levels for an SELinux user)

semanage user [-S store] -{a|d|m|l|n|D} [-LrRP] selinux_name

Manage login mappings between linux users and SELinux confined users:將linux已存在的用戶user映射到登陸保護

semanage login [-S store] -{a|d|m|l|n|D} [-sr] login_name | %groupname

-a:添加

-d:刪除

-m:修改

-l:列舉

-n:不打印說明頭

-D:全部刪除

例子:semanage login -a -s unconfined_u leowang

Manage network port type definitions:管理網絡端口
semanage port [-S store] -{a|d|m|l|n|D} [-tr] [-p proto] port | port_range

-t:類型

-r:角色

例子:semanage port -a -t http_port_t -p tcp 81

Manage network interface type definitions
semanage interface [-S store] -{a|d|m|l|n|D} [-tr] interface_spec

Manage network node type definitions
semanage node [-S store] -{a|d|m|l|n|D} [-tr] [ -p protocol ] [-M netmask] address

Manage file context mapping definitions:管理文件安全上下文的映射

-f:文件

-s:用戶

-t:類型

r:角色
semanage fcontext [-S store] -{a|d|m|l|n|D} [-frst] file_spec
semanage fcontext [-S store] -{a|d|m|l|n|D} -e replacement target
例子:semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?" //新建一條規則,指定/web目錄及其下的所有文件的擴展屬性為httpd_sys_content_t

Manage processes type enforcement mode
semanage permissive [-S store] -{a|d|l|n|D} type

Disable/Enable dontaudit rules in policy
semanage dontaudit [-S store] [ on | off ]

Execute multiple commands within a single transaction.
semanage [-S store] -i command-file

查看一下預定義

#semanage port -l

http_cache_port_t tcp 3128, 8080, 8118, 11211, 10001-10010
http_cache_port_t udp 3130, 11211
http_port_t tcp 80, 443, 488, 8008, 8009, 8443

soundd_port_t tcp 8000, 9433, 16001

原來8000 已經被預定義占用了,所有不能使用8000端口。

# semanage port -a -t http_port_t -p tcp 81

為Http 服務增加一個端口 81 ,同時將httpd 的端口改成 81 ,啟動成功

Centos selinux

如何更改linux(centos)下的Apache http端口號