Nginx is a free, high-performance HTTP-server, open source. Nginx is known for its stability, wide range of functions, simple configuration, and low resource consumption. In this article we will discuss how to install Nginx on a Fedora / CentOS with PHP version 5 (through PHP-FPM) and MySQL.
Article originally written for distributions Fedora version 14 and CentOS 6.5, and then adapted for CentOS 7.0. However, setting it to the version RHEL 7.0 / CentOS 7.0 is devoted to a separate article.
1. Preliminary remarks
In this article, we will, we will use 10.4.0.10 as the IP-address of the experimental machine. These settings in your case may be different, so replace them with your own, where you need it.
2. Connecting additional storage
Unfortunately, php-fpm is not available in the repositories (official storage systems) CentOS, but is available from RemiRPM, which itself depends on EPEL. You can enable both storage systems as follows (true for CentOS 6.5):
1 2 |
rpm --import https://fedoraproject.org/static/0608B895.txt rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm |
rpm –import http://rpms.famillecollet.com/RPM-GPG-KEY-remi
rpm -ivhhttp://rpms.famillecollet.com/enterprise/remi-release-6.rpm
Run yum with the following parameters:
1 2 3 |
install -y yum-priorities<span id="result_box" class="" lang="en"><span class="hps">Then</span> <span class="hps">edit</span> <span class="hps">/etc/yum.repos.d/epel.repo</span> <span class="hps">And add a</span> <span class="hps">line preference</span> <span class="hps">= 10</span> <span class="hps">to the section</span> <span class="hps">[epel]:</span></span> |
1 2 3 4 5 6 7 8 9 |
[epel] name=Extra Packages for Enterprise Linux 6 - $basearch #baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch failovermethod=priority enabled=1 priority=10 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 |
Then do a similar modification to the section [remi] /etc/yum.repos.d/remi.repo and change enabled 1:
1 2 3 4 5 6 7 8 9 |
[remi] name=Les RPM de remi pour Enterprise Linux $releasever - $basearch #baseurl=http://rpms.famillecollet.com/enterprise/$releasever/remi/$basearch/ mirrorlist=http://rpms.famillecollet.com/enterprise/$releasever/remi/mirror enabled=1 priority=10 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi failovermethod=priority |
3. Installing MySQL 5
First, install MySQL 5 (MariaDB), by running yum with the following parameters:
1 |
install -y mysql mysql-server |
Then create links to startup for MySQL (MySQL will run automatically at startup) and run the server MySQL:
1 |
systemctl enable mysqld.service |
For the system under CentOS earlier had to do the following:
1 2 |
chkconfig --levels 235 mysqld on service mysqld start |
Now check whether the work is permitted on the network. Proceed as follows:
1 |
netstat -tap | grep mysql |
If you see a message like this:
[root]# netstat -tap | grep mysql
tcp 0 0 *:mysql *:* LISTEN 1279/mysqld
Proceed as follows:
1 |
mysql_secure_installation |
To set the administrator password (otherwise someone may want to access your databases MySQL!):
1 |
mysql_secure_installation |
For further installation, you will need to enter your username and password, the other questions click Enter.
On that note, MySQL 5 installation is completed.
4. Installing Nginx
Nginx on CentOS as available from EPEL, and the official website of the developer to install yum run with the following parameters:
1 |
install -y nginx |
Then create a link to nginx startup and then run the program itself:
1 |
systemctl enable nginx.service |
For the system under CentOS earlier had to do the following:
1 2 |
chkconfig --levels 235 nginx on service nginx start |
You might see an error like: port 80 is already in use, the error will be as follows:
1 |
[root]# service nginx start] |
1 2 3 4 5 6 7 |
Starting nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] still could not bind() [FAILED] |
The error appears in the event that is already running another service that uses port 80, for example, apache. Stop and start the apache service NGINX:
1 2 3 4 5 |
apachectl stop yum remove httpd chkconfig --level 235 httpd off chkconfig --levels 235 nginx on service nginx start |
We introduce the IP-address of our server or host name in the browser (eg http://110.4.20.10) and move on to the start page nginx.
5. Installing PHP5
PHP5 can work with nginx through PHP-FPM (FastCGI Process Manager), which is an alternative PHP FastCGI, with additional features useful for sites of all sizes, especially for sites visited. Will install php-fpm with php-cli and some PHP5 modules, such as php-mysql, which is required to use MySQL in your PHP-scripts to run yum install with the following parameters:
1 |
install -y php-fpm php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-magickwand php-magpierss php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy |
APC is a free and open opcode kesher for PHP, which caches and optimizes the intermediate code PHP. APC is similar to other opcode-kesherami for PHP, such as: eAccelerator and Xcache. It is recommended to install any of these modules to speed up dynamic pages in PHP.
To install APC run yum with the following parameters:
1 |
install -y php-pecl-apc |
Then open /etc/php.ini and set cgi.fix_pathinfo=0:
[...]
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP's
; previousbehaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting
; of zero causes PHP to behave as before. Default is 1. You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://www.php.net/manual/en/ini.core.php#ini.cgi.fix-pathinfo
cgi.fix_pathinfo=0
[...]
If required, perform a detailed configuration of PHP.
In order to avoid errors in the time zone file /var/log/php-fpm/www-error.log, when triggered PHP script in your browser, we need to set the time zone (date.timezone) in /etc/php.ini:
1 2 3 4 5 6 7 |
[...] [Date ] ; Defines the default timezone used by the date functions ; http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone date.timezone = "Europe/Berlin" [...] |
Determine the timezone to our system can be as follows:
1 2 3 |
cat /etc/sysconfig/clock [root]# cat /etc/sysconfig/clock ZONE="Europe/Berlin" |
Then create a link on startup for php-fpm and run:
1 2 |
chkconfig --levels 235 php-fpm on service php-fpm start |
PHP-FPM is a process (with a script init / etc / init.d / php-fpm), which runs the FastCGI server on port 9000.
6. Configure nginx
Open the configuration file nginx /etc/nginx/nginx.conf:
The first thing (recommended for heavy high-end servers) can increase the number of running processes and set the maximum time maintaining the keepalive-connection:
1 2 3 4 5 |
[...] worker_processes 4; [...] keepalive_timeout 2; [...]; |
And you can trust the developers nginx and specify values for auto.
Virtual hosts are defined on the server in a directory /etc/nginx/conf.d. Edit the default vhost (it /etc/nginx/conf.d/default.conf) as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
[...] server {; listen 80; server_name _; #charset koi8-r; #access_log logs/host.access.log main; location / { root /usr/share/nginx/html; index index.php index.html index.htm; } error_page 404 /404.html; location = /404.html { root /usr/share/nginx/html; } # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root /usr/share/nginx/html; try_files $uri =404; fastcgi_pass127.0.0.1:9000; fastcgi_indexindex.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; includefastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~ /\.ht { deny all; } } |
server_name _; creates a standard repository vhost (if you define the host name).
In the section / part, add index.php to the line indkeksa root / usr / share / nginx / html; means that the document root directory is in / usr / share / nginx / html.
PHP is an important part of the location, a stanza: ~ \ .php $ {}. Remove ~ \ .php $ {} to turn on location. Change the root line in the document root of our site (for example / usr / share / nginx / html;). Also add strokutry_files $ uri = 404, to prevent vulnerabilities. Make sure you change the string on fastcgi_param fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name, otherwise PHP interpreting software will not find a PHP-script that is used in the browser. At $ document_root given by / usr / share / nginx / html, as this is our root directory.
PHP-FPM standard runs on port 9000 at the address 127.0.0.1, so ask nginx connect to 127.0.0.1:9000 with a string fastcgi_pass 127.0.0.1:9000 ;. You can also use PHP-FPM socket
Now save the file and restart nginx:
1 |
service nginx reload |
Let’s create a PHP file in the site root / usr / share / nginx / html.
1 2 3 |
<?php phpinfo(); ?> |
Then open the file created in the browser (eg http://110.41.0.10/info.php)
PHP5 is working through the FPM / FastCGI, as shown in line Server API. Bottom of the page provides information about the connected modules, including MySQL.
7. Using Unix-socket in PHP-FPM
By default, PHP-FPM uses port 9000 to the address 127.0.0.1. You can also use Unix socket PHP-FPM, thus avoiding the costs of TCP. Open the file and edit /etc/php-fpm.d/www.conf follows:
1 2 3 4 |
[...] ;listen = 127.0.0.1:9000 listen = /tmp/php5-fpm.sock [...] |
Restart PHP-FPM:
1 |
service php-fpm reload |
Now nginx configuration and replace string fastcgi_pass 127.0.0.1:9000; on fastcgi_passunix: /tmp/php5-fpm.sock; it will look as follows:
1 2 3 4 5 6 7 8 9 10 |
[...] location ~ \.php$ { root /usr/share/nginx/html; try_files $uri =404; fastcgi_passunix:/tmp/php5-fpm.sock; fastcgi_indexindex.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; includefastcgi_params; } [...] |
In the end then restart nginx:
1 |
service nginx reload |