C C++ and Java Program in Linux and Lamp Server Installation
7.How to install GCC (C/C++) Compiler in Linux Create a sample C program then compile and execute the code.
[email protected]:~$ sudo apt install gcc
[email protected]:~$ sudo apt install build-essential
[email protected]:~$ gcc –version
C Program
Example-1:
Write the following code using any text editor and save the file with the extension ‘.c’.
#include <stdio.h>
int main()
{
printf(“Learning C in Linux Kernal”);
}
To compile and execute the code. The source file name is first.c and executable filename is first here.
$ gcc first.c -o first
$ ./first
Learning C in Linux Kernal
Example-2:
#include <stdio.h>
int main()
{
char name[100];
int age;
printf(“Enter your Name: “);
scanf(“%s”,name);
printf(“Enter your Age: “);
scanf(“%d”,&age);
printf(“Hello, %s,You are %d years old”, name, age);
}
Example-3:
#include <stdio.h>
int main(int argc,char* argv[]){
printf(“Total number of Arguments = %d\n”,argc);
printf(“Argument No. 1 = %s\n”,argv[0]);
printf(“Argument No. 2 = %s\n”,argv[1]);
printf(“Argument No. 3 = %s\n”,argv[2]);
}
8. Create a sample C++ program then compile and execute the code.
Example-1:
Write the following code using any text editor and save the file with the extension ‘.cpp’
[email protected]:~$ sudo nano hello.cpp
#include <iostream>
using namespace std;
int main()
{
cout<<“Hello World!” <<endl;
return 0;
}
[email protected]:~$ g++ hello.cpp
[email protected]:~$ ./a.out
Hello World!
[email protected]:~$ g++ hello.cpp -o hello
[email protected]:~$./hello
Hello World!
Example-2:
#include <bits/stdc++.h>
using namespace std;
class Gyancs
{
// Access specifier
public:
// Data Members
string csname;
// Member Functions()
void printname()
{
cout<< “Csname is: “<<csname;
}
};
int main() {
// Declare an object of class Gyancs
Gyancs obj1;
// accessing data member
obj1.csname = “Open Source”;
// accessing member function
obj1.printname();
return0;
}
Output:
Csname is: Open Source
9. How to install Java in Linux Creating a sample java Program.
[email protected]:~$ sudo apt install openjdk-8-jdk
[email protected]:~$ sudo nano /etc/environment
JAVA_HOME=”/usr/lib/jvm/java-1.8.0-openjdk-amd64/bin”
Java Programming in Linux
Example-1:
Write the following code using any text editor and save the file with the extension ‘.java’
[email protected]:~$ sudo nano Simple.java
class Simple{
public static void main(String args[]){
System.out.println(“Hello Java”);
}
}
[email protected]:~$ javac Simple.java
[email protected]:~$ java Simple
Hello Java
Example-2:
[email protected]:~$ sudo nano Student.java
public class Student {
public static void main(String[] args) {
int x=10;
int y=12;
if(x+y> 20)
{
System.out.println(“x+y is Greater Than 20”);
}
}
}
[email protected]:~$ javac Student.java
[email protected]:~$ java Student
x+y is Greater Than 20
10.How To Install Linux, Apache, MySQL, PHP (LAMP)
Step 1: Install Apache
[email protected]:~$ sudo apt-get update
[email protected]:~$ sudo apt-get install apache2
Step 2: Install MySQL
[email protected]:~$ sudo apt-get install mysql-server
Finish up by running the MySQL set up script:
[email protected]:~$ sudo /usr/bin/mysql_secure_installation
Press y|Y for Yes, any other key for No: yes
There are three levels of password validation policy:
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:
The prompt will ask you for your current root password.
Type it in.0
Enter current password for root (enter for none):
OK, successfully used password, moving on…
Step 3: Install PHP
[email protected]:~$ sudo apt-get install php
may also be useful to add php to the directory index, to serve the relevant php index files:
[email protected]:~$ sudo nano /etc/apache2/mods-enabled/dir.conf
Add index.php to the beginning of index files. The page should now look like this:
<IfModulemod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
Note:– Green place is add index.php, Strikethrough is remove it there.
PHP Modules
[email protected]:~$ apt-cache search php
Step 4: RESULTS — See PHP on your Server
To set this up, first create a new file:
[email protected]:~$ sudo nano /var/www/html/info.php
Add in the following line:
<?php
phpinfo();
?>
Then Save and Exit.
Restart apache so that all of the changes take effect:
[email protected]:~$ sudo service apache2 restart
How to Find your Server’s IP address
[email protected]:~$ sudo ifconfig
The address you want to visit will be:
http://your_server_IP_address/info.php
http://192.168.7.81/
http://192.168.7.81/info.php
Start the Command-line tool MySql and select a database:
[email protected]:~$sudo mysql -u root -p
[sudo] password for user:
Enter password:
mysql>
or
[email protected]:~$sudo mysql -u root
[sudo] password for user:
mysql>SHOW DATABASES;
mysql>use mysql;
mysql>show tables;
To create and populate the example table, use these statements:
mysql>CREATE TABLE item (
article INT UNSIGNED DEFAULT’0000‘NOT NULL,
dealer CHAR(20)DEFAULT”NOT NULL,
price DECIMAL(16,2)DEFAULT’0.00‘NOT NULL,
PRIMARYKEY(article, dealer));
mysql>DESCRIBE item;
mysql>INSERT INTO item VALUES
(1,’A’,3.45),(1,’B’,3.99),(2,’A’,10.99),(3,’B’,1.45),
(3,’C’,1.69),(3,’D’,1.25),(4,’D’,19.95);
After issuing the statements, the table should have the following contents:
mysql>SELECT*FROM item ORDERBY article;
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 1 | A | 3.45 |
| 1 | B | 3.99 |
| 2 | A | 10.99 |
| 3 | B | 1.45 |
| 3 | C | 1.69 |
| 3 | D | 1.25 |
| 4 | D | 19.95 |
+---------+--------+-------+
Theory Question:
- What is the difference between Linux and Unix?
- What are the Features of the Linux Operating System?
- What is Linux Kernel?
- How many types of Shells are there in Linux? explain.
- Explain File Permissions types in Linux?
- What are the symbolic links?
- Explain about chmod command?
- Where is password file located in Linux and how can you improve the security of password file?
- How do you create a new user account and set the password for a user from a shell prompt in Linux?
- What are the different types of Kernels? Explain